tgstation-server 5.12.7
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
16
18{
22 [Route(Routes.Transfer)]
23 [RequestSizeLimit(Limits.MaximumFileTransferSize)]
24 public sealed class TransferController : ApiController
25 {
30
39 IDatabaseContext databaseContext,
40 IAuthenticationContextFactory authenticationContextFactory,
42 ILogger<ApiController> logger)
43 : base(
44 databaseContext,
45 authenticationContextFactory,
46 logger,
47 true)
48 {
49 this.fileTransferService = fileTransferService ?? throw new ArgumentNullException(nameof(fileTransferService));
50 }
51
60 [TgsAuthorize]
61 [HttpGet]
62 [ProducesResponseType(200, Type = typeof(LimitedStreamResult))]
63 [ProducesResponseType(410, Type = typeof(ErrorMessageResponse))]
64 public Task<IActionResult> Download([Required, FromQuery] string ticket, CancellationToken cancellationToken)
65 => fileTransferService.GenerateDownloadResponse(this, ticket, cancellationToken);
66
76 [TgsAuthorize]
77 [HttpPut]
78 [ProducesResponseType(204)]
79 [ProducesResponseType(410, Type = typeof(ErrorMessageResponse))]
80 public async Task<IActionResult> Upload([Required, FromQuery] string ticket, CancellationToken cancellationToken)
81 {
82 if (ticket == null)
83 return BadRequest(new ErrorMessageResponse(ErrorCode.ModelValidationFailure));
84
85 var fileTicketResult = new FileTicketResponse
86 {
87 FileTicket = ticket,
88 };
89
90 var result = await fileTransferService.SetUploadStream(fileTicketResult, Request.Body, cancellationToken);
91 if (result != null)
92 return result.ErrorCode == ErrorCode.ResourceNotPresent
93 ? this.Gone()
94 : Conflict(result);
95
96 return NoContent();
97 }
98 }
99}
Sanity limits to prevent users from overloading.
Definition: Limits.cs:9
const int MaximumFileTransferSize
The maximum size for file transfers.
Definition: Limits.cs:28
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:98
Base Controller for API functions.
Very similar to FileStreamResult except it's IActionResultExecutor<TResult> contains a fix for https:...
Task< IActionResult > Download([Required, FromQuery] string ticket, CancellationToken cancellationToken)
Downloads a file with a given ticket .
readonly IFileTransferStreamHandler fileTransferService
The IFileTransferStreamHandler for the TransferController.
async Task< IActionResult > Upload([Required, FromQuery] string ticket, CancellationToken cancellationToken)
Uploads a file with a given ticket .
TransferController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IFileTransferStreamHandler fileTransferService, ILogger< ApiController > logger)
Initializes a new instance of the TransferController class.
Reads and writes to Streams associated with FileTicketResponses.
Task< ErrorMessageResponse > SetUploadStream(FileTicketResponse ticket, Stream stream, CancellationToken cancellationToken)
Sets the Stream for a given ticket associated with a pending upload.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition: ErrorCode.cs:11