Initial
This commit is contained in:
24
MareSynchronos/WebAPI/Files/Models/DownloadFileTransfer.cs
Normal file
24
MareSynchronos/WebAPI/Files/Models/DownloadFileTransfer.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using MareSynchronos.API.Dto.Files;
|
||||
|
||||
namespace MareSynchronos.WebAPI.Files.Models;
|
||||
|
||||
public class DownloadFileTransfer : FileTransfer
|
||||
{
|
||||
public DownloadFileTransfer(DownloadFileDto dto) : base(dto)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool CanBeTransferred => Dto.FileExists && !Dto.IsForbidden && Dto.Size > 0;
|
||||
public Uri DownloadUri => new(Dto.Url);
|
||||
public override long Total
|
||||
{
|
||||
set
|
||||
{
|
||||
// nothing to set
|
||||
}
|
||||
get => Dto.Size;
|
||||
}
|
||||
|
||||
public long TotalRaw => 0; // XXX
|
||||
private DownloadFileDto Dto => (DownloadFileDto)TransferDto;
|
||||
}
|
10
MareSynchronos/WebAPI/Files/Models/DownloadStatus.cs
Normal file
10
MareSynchronos/WebAPI/Files/Models/DownloadStatus.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace MareSynchronos.WebAPI.Files.Models;
|
||||
|
||||
public enum DownloadStatus
|
||||
{
|
||||
Initializing,
|
||||
WaitingForSlot,
|
||||
WaitingForQueue,
|
||||
Downloading,
|
||||
Decompressing
|
||||
}
|
10
MareSynchronos/WebAPI/Files/Models/FileDownloadStatus.cs
Normal file
10
MareSynchronos/WebAPI/Files/Models/FileDownloadStatus.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace MareSynchronos.WebAPI.Files.Models;
|
||||
|
||||
public class FileDownloadStatus
|
||||
{
|
||||
public DownloadStatus DownloadStatus { get; set; }
|
||||
public long TotalBytes { get; set; }
|
||||
public int TotalFiles { get; set; }
|
||||
public long TransferredBytes { get; set; }
|
||||
public int TransferredFiles { get; set; }
|
||||
}
|
27
MareSynchronos/WebAPI/Files/Models/FileTransfer.cs
Normal file
27
MareSynchronos/WebAPI/Files/Models/FileTransfer.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using MareSynchronos.API.Dto.Files;
|
||||
|
||||
namespace MareSynchronos.WebAPI.Files.Models;
|
||||
|
||||
public abstract class FileTransfer
|
||||
{
|
||||
protected readonly ITransferFileDto TransferDto;
|
||||
|
||||
protected FileTransfer(ITransferFileDto transferDto)
|
||||
{
|
||||
TransferDto = transferDto;
|
||||
}
|
||||
|
||||
public virtual bool CanBeTransferred => !TransferDto.IsForbidden && (TransferDto is not DownloadFileDto dto || dto.FileExists);
|
||||
public string ForbiddenBy => TransferDto.ForbiddenBy;
|
||||
public string Hash => TransferDto.Hash;
|
||||
public bool IsForbidden => TransferDto.IsForbidden;
|
||||
public bool IsInTransfer => Transferred != Total && Transferred > 0;
|
||||
public bool IsTransferred => Transferred == Total;
|
||||
public abstract long Total { get; set; }
|
||||
public long Transferred { get; set; } = 0;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Hash;
|
||||
}
|
||||
}
|
@@ -0,0 +1,93 @@
|
||||
using System.Net;
|
||||
|
||||
namespace MareSynchronos.WebAPI.Files.Models;
|
||||
|
||||
public class ProgressableStreamContent : StreamContent
|
||||
{
|
||||
private const int _defaultBufferSize = 4096;
|
||||
private readonly int _bufferSize;
|
||||
private readonly IProgress<UploadProgress>? _progress;
|
||||
private readonly Stream _streamToWrite;
|
||||
private bool _contentConsumed;
|
||||
|
||||
public ProgressableStreamContent(Stream streamToWrite, IProgress<UploadProgress>? downloader)
|
||||
: this(streamToWrite, _defaultBufferSize, downloader)
|
||||
{
|
||||
}
|
||||
|
||||
public ProgressableStreamContent(Stream streamToWrite, int bufferSize, IProgress<UploadProgress>? progress)
|
||||
: base(streamToWrite, bufferSize)
|
||||
{
|
||||
if (streamToWrite == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(streamToWrite));
|
||||
}
|
||||
|
||||
if (bufferSize <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(bufferSize));
|
||||
}
|
||||
|
||||
_streamToWrite = streamToWrite;
|
||||
_bufferSize = bufferSize;
|
||||
_progress = progress;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_streamToWrite.Dispose();
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context)
|
||||
{
|
||||
PrepareContent();
|
||||
|
||||
var buffer = new byte[_bufferSize];
|
||||
var size = _streamToWrite.Length;
|
||||
var uploaded = 0;
|
||||
|
||||
using (_streamToWrite)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var length = await _streamToWrite.ReadAsync(buffer).ConfigureAwait(false);
|
||||
if (length <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
uploaded += length;
|
||||
_progress?.Report(new UploadProgress(uploaded, size));
|
||||
await stream.WriteAsync(buffer.AsMemory(0, length)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool TryComputeLength(out long length)
|
||||
{
|
||||
length = _streamToWrite.Length;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void PrepareContent()
|
||||
{
|
||||
if (_contentConsumed)
|
||||
{
|
||||
if (_streamToWrite.CanSeek)
|
||||
{
|
||||
_streamToWrite.Position = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("The stream has already been read.");
|
||||
}
|
||||
}
|
||||
|
||||
_contentConsumed = true;
|
||||
}
|
||||
}
|
13
MareSynchronos/WebAPI/Files/Models/UploadFileTransfer.cs
Normal file
13
MareSynchronos/WebAPI/Files/Models/UploadFileTransfer.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using MareSynchronos.API.Dto.Files;
|
||||
|
||||
namespace MareSynchronos.WebAPI.Files.Models;
|
||||
|
||||
public class UploadFileTransfer : FileTransfer
|
||||
{
|
||||
public UploadFileTransfer(UploadFileDto dto) : base(dto)
|
||||
{
|
||||
}
|
||||
|
||||
public string LocalFile { get; set; } = string.Empty;
|
||||
public override long Total { get; set; }
|
||||
}
|
3
MareSynchronos/WebAPI/Files/Models/UploadProgress.cs
Normal file
3
MareSynchronos/WebAPI/Files/Models/UploadProgress.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace MareSynchronos.WebAPI.Files.Models;
|
||||
|
||||
public record UploadProgress(long Uploaded, long Size);
|
Reference in New Issue
Block a user