tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
FileTransferStreamHandlerExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Net;
4using System.Net.Mime;
5using System.Threading;
6using System.Threading.Tasks;
7
8using Microsoft.AspNetCore.Http;
9using Microsoft.AspNetCore.Mvc;
10using Microsoft.Net.Http.Headers;
11
16
18{
23 {
32 public static async ValueTask<IActionResult> GenerateDownloadResponse(
33 this IFileTransferStreamHandler fileTransferService,
34 ControllerBase controller,
35 string ticket,
36 CancellationToken cancellationToken)
37 {
38 ArgumentNullException.ThrowIfNull(fileTransferService);
39
40 ArgumentNullException.ThrowIfNull(controller);
41
42 if (ticket == null)
43 return controller.BadRequest(new ErrorMessageResponse(ErrorCode.ModelValidationFailure));
44
45 var streamAccept = new MediaTypeHeaderValue(MediaTypeNames.Application.Octet);
46 if (!controller.Request.GetTypedHeaders().Accept.Any(streamAccept.IsSubsetOf))
47 return controller.StatusCode((int)HttpStatusCode.NotAcceptable, new ErrorMessageResponse(ErrorCode.BadHeaders)
48 {
49 AdditionalData = $"File downloads must accept both {MediaTypeNames.Application.Octet} and {MediaTypeNames.Application.Json}!",
50 });
51
52 var fileTicketResult = new FileTicketResponse
53 {
54 FileTicket = ticket,
55 };
56
57 var (stream, errorMessage) = await fileTransferService.RetrieveDownloadStream(fileTicketResult, cancellationToken);
58 try
59 {
60 if (errorMessage != null)
61 return controller.Conflict(errorMessage);
62
63 if (stream == null)
64 return controller.Gone();
65
66 return new LimitedStreamResult(stream);
67 }
68 catch
69 {
70 if (stream != null)
71 await stream.DisposeAsync();
72
73 throw;
74 }
75 }
76 }
77}
Represents an error message returned by the server.
Response for when file transfers are necessary.
Very similar to FileStreamResult except it's IActionResultExecutor<TResult> contains a fix for https:...
static async ValueTask< IActionResult > GenerateDownloadResponse(this IFileTransferStreamHandler fileTransferService, ControllerBase controller, string ticket, CancellationToken cancellationToken)
Downloads a file with a given ticket .
Reads and writes to Streams associated with FileTicketResponses.
ValueTask< Tuple< Stream?, ErrorMessageResponse?> > RetrieveDownloadStream(FileTicketResponse ticketResponse, CancellationToken cancellationToken)
Gets the the Stream for a given ticketResponse associated with a pending download.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12