2024-10-29 08:16:31 +00:00
|
|
|
using System;
|
2024-09-22 05:27:52 +00:00
|
|
|
using System.Net;
|
2024-10-29 08:16:31 +00:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
2024-09-22 05:27:52 +00:00
|
|
|
|
|
|
|
namespace Common;
|
|
|
|
|
|
|
|
public class ProgressStreamContent(Stream stream_, IProgress<double> progress)
|
2024-10-29 10:08:34 +00:00
|
|
|
: StreamContent(stream_, 5 * 1024 * 1024)
|
2024-09-22 05:27:52 +00:00
|
|
|
{
|
|
|
|
private readonly Stream FileStream = stream_;
|
2024-10-29 10:08:34 +00:00
|
|
|
private readonly int BufferSize = 5 * 1024 * 1024;
|
2024-09-22 05:27:52 +00:00
|
|
|
private readonly IProgress<double> Progress = progress;
|
|
|
|
|
|
|
|
protected override async Task SerializeToStreamAsync(
|
|
|
|
Stream stream,
|
|
|
|
TransportContext? context = null
|
|
|
|
)
|
|
|
|
{
|
|
|
|
var buffer = new byte[BufferSize];
|
|
|
|
long totalBytesRead = 0;
|
|
|
|
long totalBytes = FileStream.Length;
|
|
|
|
int bytesRead;
|
|
|
|
|
|
|
|
while ((bytesRead = await FileStream.ReadAsync(buffer.AsMemory())) != 0)
|
|
|
|
{
|
|
|
|
await stream.WriteAsync(buffer.AsMemory(0, bytesRead));
|
|
|
|
totalBytesRead += bytesRead;
|
|
|
|
Progress.Report((double)totalBytesRead / totalBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|