tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
TransferController.cs
Go to the documentation of this file.
1using System;
2using System.ComponentModel.DataAnnotations;
3using System.Threading;
4using System.Threading.Tasks;
5
6using Microsoft.AspNetCore.Mvc;
7using Microsoft.Extensions.Logging;
8
18
20{
24 [Route(Routes.Transfer)]
25 [RequestSizeLimit(Limits.MaximumFileTransferSize)]
26 public sealed class TransferController : ApiController
27 {
32
42 IDatabaseContext databaseContext,
43 IAuthenticationContext authenticationContext,
45 ILogger<ApiController> logger,
46 IApiHeadersProvider apiHeaders)
47 : base(
48 databaseContext,
49 authenticationContext,
50 apiHeaders,
51 logger,
52 true)
53 {
54 this.fileTransferService = fileTransferService ?? throw new ArgumentNullException(nameof(fileTransferService));
55 }
56
65 [TgsAuthorize]
66 [HttpGet]
67 [ProducesResponseType(200, Type = typeof(LimitedStreamResult))]
68 [ProducesResponseType(410, Type = typeof(ErrorMessageResponse))]
69 public ValueTask<IActionResult> Download([Required, FromQuery] string ticket, CancellationToken cancellationToken)
70 => fileTransferService.GenerateDownloadResponse(this, ticket, cancellationToken);
71
81 [TgsAuthorize]
82 [HttpPut]
83 [DisableRequestSizeLimit]
84 [ProducesResponseType(204)]
85 [ProducesResponseType(410, Type = typeof(ErrorMessageResponse))]
86 public async ValueTask<IActionResult> Upload([Required, FromQuery] string ticket, CancellationToken cancellationToken)
87 {
88 if (ticket == null)
89 return BadRequest(new ErrorMessageResponse(ErrorCode.ModelValidationFailure));
90
91 var fileTicketResult = new FileTicketResponse
92 {
93 FileTicket = ticket,
94 };
95
96 var result = await fileTransferService.SetUploadStream(fileTicketResult, Request.Body, cancellationToken);
97 if (result != null)
98 return result.ErrorCode == ErrorCode.ResourceNotPresent
99 ? this.Gone()
100 : Conflict(result);
101
102 return NoContent();
103 }
104 }
105}
Sanity limits to prevent users from overloading.
Definition Limits.cs:9
const int MaximumFileTransferSize
The maximum size for file transfers.
Definition Limits.cs:33
Represents an error message returned by the server.
Response for when file transfers are necessary.
Routes to a server actions.
Definition Routes.cs:9
const string Transfer
The transfer controller.
Definition Routes.cs:108
Base Controller for API functions.
Very similar to FileStreamResult except it's IActionResultExecutor<TResult> contains a fix for https:...
ValueTask< IActionResult > Download([Required, FromQuery] string ticket, CancellationToken cancellationToken)
Downloads a file with a given ticket .
TransferController(IDatabaseContext databaseContext, IAuthenticationContext authenticationContext, IFileTransferStreamHandler fileTransferService, ILogger< ApiController > logger, IApiHeadersProvider apiHeaders)
Initializes a new instance of the TransferController class.
readonly IFileTransferStreamHandler fileTransferService
The IFileTransferStreamHandler for the TransferController.
async ValueTask< IActionResult > Upload([Required, FromQuery] string ticket, CancellationToken cancellationToken)
Uploads a file with a given ticket .
For creating and accessing authentication contexts.
Reads and writes to Streams associated with FileTicketResponses.
ValueTask< ErrorMessageResponse?> SetUploadStream(FileTicketResponse ticketResponse, Stream stream, CancellationToken cancellationToken)
Sets the Stream for a given ticketResponse associated with a pending upload.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12
@ Required
DMAPI validation must suceed for the deployment to succeed.