4using System.Threading.Tasks;
6using Microsoft.AspNetCore.WebUtilities;
52 Ticket = ticket ??
throw new ArgumentNullException(nameof(ticket));
55 streamTcs =
new TaskCompletionSource<Stream?>();
65 return ValueTask.CompletedTask;
83 public async ValueTask<ErrorMessageResponse?>
Completion(
Stream stream, CancellationToken cancellationToken)
85 ArgumentNullException.ThrowIfNull(stream);
90 Stream? bufferedStream =
null;
100 await bufferedStream.DrainAsync(cancellationToken);
105 throw new InvalidOperationException($
"Invalid FileUploadStreamKind: {streamKind}");
110 if (bufferedStream !=
null)
111 await bufferedStream.DisposeAsync();
116 await
using (bufferedStream)
118 streamTcs.TrySetResult(bufferedStream ?? stream);
129 throw new InvalidOperationException(
"Error already set!");
133 AdditionalData = additionalData,
139 public async ValueTask<Stream>
GetResult(CancellationToken cancellationToken)
141 ??
throw new InvalidOperationException(
"Upload ticket expired!");
146 using (cancellationToken.Register(() =>
streamTcs.TrySetCanceled(cancellationToken)))
Represents an error message returned by the server.
Response for when file transfers are necessary.
IIOManager that resolves paths to Environment.CurrentDirectory.
const int DefaultBufferSize
Default FileStream buffer size used by .NET.
FileTicketResponse Ticket
The FileTicketResponse.
FileUploadProvider(FileTicketResponse ticket, FileUploadStreamKind streamKind)
Initializes a new instance of the FileUploadProvider class.
readonly TaskCompletionSource< Stream?> streamTcs
The TaskCompletionSource<TResult> for the Stream.
async ValueTask< ErrorMessageResponse?> Completion(Stream stream, CancellationToken cancellationToken)
Resolve the stream for the FileUploadProvider and awaits the upload.
void SetError(ErrorCode errorCode, string? additionalData)
Sets an errorCode that indicates why the consuming operation could not complete. May only be called ...
readonly FileUploadStreamKind streamKind
Determines the backing for the Stream returned from GetResult(CancellationToken).
ErrorMessageResponse? errorMessage
The ErrorMessageResponse that occurred while processing the upload if any.
readonly CancellationTokenSource ticketExpiryCts
The CancellationTokenSource for the ticket duration.
async ValueTask< Stream > GetResult(CancellationToken cancellationToken)
Gets the provided Stream. May be called multiple times, though cancelling any may cause all calls to ...
readonly TaskCompletionSource completionTcs
The TaskCompletionSource that completes in IDisposable.Dispose or when SetError(ErrorCode,...
void Expire()
Expire the FileUploadProvider.
A FileTicketResponse that waits for a pending upload.
new ValueTask< Stream?> GetResult(CancellationToken cancellationToken)
Gets the provided Stream. May be called multiple times, though cancelling any may cause all calls to ...
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...