tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ControllerBaseExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Net;
3using System.Net.Mime;
4
5using Microsoft.AspNetCore.Hosting;
6using Microsoft.AspNetCore.Mvc;
7using Microsoft.AspNetCore.StaticFiles;
8using Microsoft.Extensions.Logging;
9using Microsoft.Extensions.Primitives;
10using Microsoft.Net.Http.Headers;
11
14
16{
21 {
28 public static ObjectResult Created(this ControllerBase controller, object payload) => controller.StatusCode(HttpStatusCode.Created, payload);
29
35 public static ObjectResult Unauthorized(this ControllerBase controller) => controller.StatusCode(HttpStatusCode.Unauthorized, null);
36
42 public static ObjectResult Gone(this ControllerBase controller)
43 => controller?.StatusCode(HttpStatusCode.Gone, new ErrorMessageResponse(ErrorCode.ResourceNotPresent)) ?? throw new ArgumentNullException(nameof(controller));
44
52 public static ObjectResult StatusCode(this ControllerBase controller, HttpStatusCode statusCode, object? errorMessage)
53 => controller?.StatusCode((int)statusCode, errorMessage) ?? throw new ArgumentNullException(nameof(controller));
54
63 public static VirtualFileResult? TryServeFile(this ControllerBase controller, IWebHostEnvironment hostEnvironment, ILogger logger, string path)
64 {
65 ArgumentNullException.ThrowIfNull(controller);
66 ArgumentNullException.ThrowIfNull(hostEnvironment);
67 ArgumentNullException.ThrowIfNull(logger);
68 ArgumentNullException.ThrowIfNull(path);
69
70 var fileInfo = hostEnvironment.WebRootFileProvider.GetFileInfo(path);
71 if (fileInfo.Exists)
72 {
73 logger.LogTrace("Serving static file \"{filename}\"...", path);
74 var contentTypeProvider = new FileExtensionContentTypeProvider();
75 if (!contentTypeProvider.TryGetContentType(fileInfo.Name, out var contentType))
76 contentType = MediaTypeNames.Application.Octet;
77 else if (contentType == MediaTypeNames.Application.Json)
78 controller.Response.Headers.Add(
79 HeaderNames.CacheControl,
80 new StringValues(new[] { "public", "max-age=31536000", "immutable" }));
81
82 return controller.File(path, contentType);
83 }
84
85 return null;
86 }
87 }
88}
Represents an error message returned by the server.
static ObjectResult Created(this ControllerBase controller, object payload)
Generic 201 response with a given payload .
static ObjectResult Unauthorized(this ControllerBase controller)
Generic 401 response.
static ObjectResult StatusCode(this ControllerBase controller, HttpStatusCode statusCode, object? errorMessage)
Strongly type calls to ControllerBase.StatusCode(int, object).
static ? VirtualFileResult TryServeFile(this ControllerBase controller, IWebHostEnvironment hostEnvironment, ILogger logger, string path)
Try to serve a given file path .
static ObjectResult Gone(this ControllerBase controller)
Generic 410 response.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12