2using System.Collections.Generic;
5using System.Threading.Tasks;
7using Microsoft.Extensions.Logging;
46 readonly ILogger<FileTransferService>
logger;
84 ILogger<FileTransferService>
logger)
89 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
113 toAwait = Task.CompletedTask;
121 ArgumentNullException.ThrowIfNull(downloadProvider);
123 logger.LogDebug(
"Creating download ticket for path {filePath}", downloadProvider.
FilePath);
133 logger.LogTrace(
"Expired download ticket {ticket}...", ticketResult.FileTicket);
136 logger.LogTrace(
"Created download ticket {ticket}", ticketResult.FileTicket);
144 logger.LogDebug(
"Creating upload ticket...");
148 uploadTickets.Add(uploadTicket.Ticket.FileTicket, uploadTicket);
154 logger.LogTrace(
"Expired upload ticket {ticket}...", uploadTicket.Ticket.FileTicket);
158 uploadTicket.Expire();
161 logger.LogTrace(
"Created upload ticket {ticket}", uploadTicket.Ticket.FileTicket);
169 ArgumentNullException.ThrowIfNull(ticket);
184 if (errorCode.HasValue)
186 logger.LogDebug(
"Download ticket {ticket} failed activation!", ticket.
FileTicket);
194 stream = await downloadProvider.
StreamProvider(cancellationToken);
198 catch (IOException ex)
204 AdditionalData = ex.ToString(),
215 await stream.DisposeAsync();
223 ArgumentNullException.ThrowIfNull(ticket);
237 return await uploadProvider.
Completion(stream, cancellationToken);
256 async Task ExpireAsync()
261 await oldExpireTask.WithToken(
disposeCts.Token);
263 var now = DateTimeOffset.UtcNow;
Represents an error message returned by the server.
Response for when file transfers are necessary.
virtual ? string FileTicket
The ticket to use to access the Routes.Transfer controller.
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.
async Task< Tuple< Stream, ErrorMessageResponse > > RetrieveDownloadStream(FileTicketResponse ticket, CancellationToken cancellationToken)
Gets the the Stream for a given ticket associated with a pending download. A Tuple<T1,...
readonly object synchronizationLock
lock object used to update expireTask.
readonly ILogger< FileTransferService > logger
The ILogger for the FileTransferService.
readonly Dictionary< string, FileDownloadProvider > downloadTickets
Dictionary<TKey, TValue> of FileTicketResponse.FileTickets to FileDownloadProviders.
async Task< ErrorMessageResponse > SetUploadStream(FileTicketResponse ticket, Stream stream, CancellationToken cancellationToken)
Sets the Stream for a given ticket associated with a pending upload. null if the upload completed su...
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.
FileTicketResponse CreateTicket()
Creates a new FileTicketResponse.
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.
readonly ICryptographySuite cryptographySuite
The ICryptographySuite for the FileTransferService.
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 Task< 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.
Task 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...