using System.Net; using System.Net.Http.Headers; namespace SteUp.Shared.Core.Helpers; /// /// Contenuto HTTP letto dal file durante l'invio, senza caricarlo in RAM. Il file viene riaperto /// a ogni serializzazione, così regge anche un eventuale nuovo tentativo del client /// (es. dopo il refresh del token). /// public sealed class FileHttpContent : HttpContent { private const int BufferSize = 81920; private readonly string _path; public FileHttpContent(string path, string mediaType) { _path = path; Headers.ContentType = new MediaTypeHeaderValue(mediaType); } protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => SerializeToStreamAsync(stream, context, CancellationToken.None); protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) { await using var file = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, useAsync: true); await file.CopyToAsync(stream, BufferSize, cancellationToken); } protected override bool TryComputeLength(out long length) { length = new FileInfo(_path).Length; return true; } }