2using System.Collections.Generic;
5using System.Threading.Tasks;
7using Microsoft.Extensions.Logging;
45 readonly ILogger<FileTransferService>
logger;
88 ILogger<FileTransferService>
logger)
93 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
118 toAwait = Task.CompletedTask;
126 ArgumentNullException.ThrowIfNull(downloadProvider);
127 ObjectDisposedException.ThrowIf(
disposed,
this);
129 logger.LogDebug(
"Creating download ticket for path {filePath}", downloadProvider.
FilePath);
139 logger.LogTrace(
"Expired download ticket {ticket}...", ticket);
142 logger.LogTrace(
"Created download ticket {ticket}", ticket);
153 ObjectDisposedException.ThrowIf(
disposed,
this);
155 logger.LogDebug(
"Creating upload ticket...");
171 logger.LogTrace(
"Expired upload ticket {ticket}...", ticket);
175 uploadTicket.Expire();
178 logger.LogTrace(
"Created upload ticket {ticket}", ticket);
186 ArgumentNullException.ThrowIfNull(ticketResponse);
187 ObjectDisposedException.ThrowIf(
disposed,
this);
189 var ticket = ticketResponse.FileTicket ??
throw new InvalidOperationException(
"ticketResponse must have FileTicket!");
195 logger.LogTrace(
"Download ticket {ticket} not found!", ticket);
203 if (errorCode.HasValue)
205 logger.LogDebug(
"Download ticket {ticket} failed activation!", ticket);
213 stream = await downloadProvider.
StreamProvider(cancellationToken);
217 catch (IOException ex)
223 AdditionalData = ex.ToString(),
229 logger.LogTrace(
"Ticket {ticket} downloading...", ticket);
234 await stream.DisposeAsync();
242 ArgumentNullException.ThrowIfNull(ticketResponse);
243 ObjectDisposedException.ThrowIf(
disposed,
this);
245 var ticket = ticketResponse.FileTicket ??
throw new InvalidOperationException(
"ticketResponse must have FileTicket!");
251 logger.LogTrace(
"Upload ticket {ticket} not found!", ticket);
258 return await uploadProvider.
Completion(stream, cancellationToken);
268 async Task ExpireAsync()
273 await oldExpireTask.WaitAsync(
disposeCts.Token);
275 var now = DateTimeOffset.UtcNow;
Represents an error message returned by the server.
Response for when file transfers are necessary.
Represents a file on disk to be downloaded.
bool ShareWrite
If the file read stream should be allowed to share writes. If this is set, the entire file will be bu...
string FilePath
The full path to the file on disk to download.
Func< CancellationToken, Task< Stream > >? StreamProvider
A Func<T, TResult> to specially provide a Task<TResult> returning the Stream of the file download....
Func< ErrorCode?> ActivationCallback
A Func<TResult> to run before providing the download. If it returns a non-null ErrorCode,...
Implementation of the file transfer service.
readonly object synchronizationLock
lock object used to update expireTask.
readonly ILogger< FileTransferService > logger
The ILogger for the FileTransferService.
async ValueTask< ErrorMessageResponse?> SetUploadStream(FileTicketResponse ticketResponse, Stream stream, CancellationToken cancellationToken)
Sets the Stream for a given ticketResponse associated with a pending upload.A ValueTask<TResult> res...
readonly Dictionary< string, FileDownloadProvider > downloadTickets
Dictionary<TKey, TValue> of FileTicketResponse.FileTickets to FileDownloadProviders.
IFileUploadTicket CreateUpload(FileUploadStreamKind streamKind)
Create a IFileUploadTicket.A new IFileUploadTicket.
const int TicketValidityMinutes
Number of minutes before transfer ticket expire.
readonly Dictionary< string, FileUploadProvider > uploadTickets
Dictionary<TKey, TValue> of FileTicketResponse.FileTickets to upload Stream TaskCompletionSource<TRes...
FileTransferService(ICryptographySuite cryptographySuite, IIOManager ioManager, IAsyncDelayer asyncDelayer, ILogger< FileTransferService > logger)
Initializes a new instance of the FileTransferService class.
readonly IIOManager ioManager
The IIOManager for the FileTransferService.
Task expireTask
Combined Task of all QueueExpiry(Action) calls.
void QueueExpiry(Action expireAction)
Queue an expireAction to run after TicketValidityMinutes.
readonly IAsyncDelayer asyncDelayer
The IAsyncDelayer for the FileTransferService.
async ValueTask< Tuple< Stream?, ErrorMessageResponse?> > RetrieveDownloadStream(FileTicketResponse ticketResponse, CancellationToken cancellationToken)
Gets the the Stream for a given ticketResponse associated with a pending download....
readonly ICryptographySuite cryptographySuite
The ICryptographySuite for the FileTransferService.
bool disposed
If the FileTransferService is disposed.
readonly CancellationTokenSource disposeCts
CancellationTokenSource that is triggered when IAsyncDisposable.DisposeAsync is called.
async ValueTask DisposeAsync()
FileTicketResponse CreateDownload(FileDownloadProvider downloadProvider)
Create a FileTicketResponse for a download.A new FileTicketResponse for a download.
async ValueTask< ErrorMessageResponse?> Completion(Stream stream, CancellationToken cancellationToken)
Resolve the stream for the FileUploadProvider and awaits the upload.
Interface for using filesystems.
FileStream GetFileStream(string path, bool shareWrite)
Gets the Stream for a given file path .
Contains various cryptographic functions.
string GetSecureString()
Generates a 40-length secure ascii string.
Reads and writes to Streams associated with FileTicketResponses.
Service for temporarily storing files to be downloaded or uploaded.
A FileTicketResponse that waits for a pending upload.
For waiting asynchronously.
ValueTask Delay(TimeSpan timeSpan, CancellationToken cancellationToken)
Create a Task that completes after a given timeSpan .
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
FileUploadStreamKind
Determines the type of global::System.IO.Stream returned from IFileUploadTicket's created from IFileT...