| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Forms; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents the result of writing a part to storage. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed record KrPartWriteResult |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets the path to the stored part. |
| | | 12 | | /// </summary> |
| | | 13 | | public required string TempPath { get; init; } |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the length of the stored part in bytes. |
| | | 17 | | /// </summary> |
| | | 18 | | public long Length { get; init; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets the SHA-256 hash of the stored part, if computed. |
| | | 22 | | /// </summary> |
| | | 23 | | public string? Sha256 { get; init; } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Defines a streaming sink for multipart parts. |
| | | 28 | | /// </summary> |
| | | 29 | | public interface IKrPartSink |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// Writes the part content to the sink. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="source">The source stream for the part.</param> |
| | | 35 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 36 | | /// <returns>The write result.</returns> |
| | | 37 | | Task<KrPartWriteResult> WriteAsync(Stream source, CancellationToken cancellationToken); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Stores part contents on disk. |
| | | 42 | | /// </summary> |
| | 3 | 43 | | public sealed class KrDiskPartSink(string destinationPath, bool computeSha256, string? fileName = null) : IKrPartSink |
| | | 44 | | { |
| | 3 | 45 | | private readonly string _destinationPath = destinationPath; |
| | 3 | 46 | | private readonly bool _computeSha256 = computeSha256; |
| | 3 | 47 | | private readonly string? _fileName = fileName; |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public async Task<KrPartWriteResult> WriteAsync(Stream source, CancellationToken cancellationToken) |
| | | 51 | | { |
| | 3 | 52 | | _ = Directory.CreateDirectory(_destinationPath); |
| | 3 | 53 | | var fileName = string.IsNullOrWhiteSpace(_fileName) |
| | 3 | 54 | | ? Path.GetRandomFileName() |
| | 3 | 55 | | : _fileName; |
| | 3 | 56 | | var uniqueName = string.IsNullOrWhiteSpace(_fileName) |
| | 3 | 57 | | ? fileName |
| | 3 | 58 | | : $"{Path.GetFileNameWithoutExtension(fileName)}-{Guid.NewGuid():N}{Path.GetExtension(fileName)}"; |
| | 3 | 59 | | var tempPath = Path.Combine(_destinationPath, uniqueName); |
| | | 60 | | |
| | 3 | 61 | | await using var fileStream = new FileStream(tempPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, 8192 |
| | | 62 | | |
| | 3 | 63 | | using var hasher = _computeSha256 ? IncrementalHash.CreateHash(HashAlgorithmName.SHA256) : null; |
| | 3 | 64 | | var buffer = new byte[81920]; |
| | 3 | 65 | | long total = 0; |
| | | 66 | | int read; |
| | 6 | 67 | | while ((read = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0) |
| | | 68 | | { |
| | 3 | 69 | | await fileStream.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false); |
| | 3 | 70 | | total += read; |
| | 3 | 71 | | hasher?.AppendData(buffer, 0, read); |
| | | 72 | | } |
| | | 73 | | |
| | 3 | 74 | | var sha = hasher is null ? null : Convert.ToHexString(hasher.GetHashAndReset()).ToLowerInvariant(); |
| | | 75 | | |
| | 3 | 76 | | return new KrPartWriteResult |
| | 3 | 77 | | { |
| | 3 | 78 | | TempPath = tempPath, |
| | 3 | 79 | | Length = total, |
| | 3 | 80 | | Sha256 = sha |
| | 3 | 81 | | }; |
| | 3 | 82 | | } |
| | | 83 | | } |