[backend/api-shared] Fix upload of files larger than 28MB.
This commit is contained in:
parent
89efdff3c0
commit
b50121d0a1
4 changed files with 29 additions and 0 deletions
|
@ -25,6 +25,7 @@ namespace Iceshrimp.Backend.Controllers.Mastodon;
|
|||
[Produces(MediaTypeNames.Application.Json)]
|
||||
public class MediaController(DriveService driveSvc, DatabaseContext db) : ControllerBase
|
||||
{
|
||||
[MaxRequestSizeIsMaxUploadSize]
|
||||
[HttpPost("/api/v1/media")]
|
||||
[HttpPost("/api/v2/media")]
|
||||
[ProducesResults(HttpStatusCode.OK)]
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
using Iceshrimp.Backend.Core.Configuration;
|
||||
using Iceshrimp.Backend.Core.Middleware;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Iceshrimp.Backend.Controllers.Shared.Attributes;
|
||||
|
||||
public class MaxRequestSizeIsMaxUploadSize : Attribute, IResourceFilter
|
||||
{
|
||||
public void OnResourceExecuting(ResourceExecutingContext context)
|
||||
{
|
||||
var feature = context.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() ??
|
||||
throw new Exception("Failed to get IHttpMaxRequestBodySizeFeature");
|
||||
var config = context.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<Config.StorageSection>>();
|
||||
feature.MaxRequestBodySize = config.Value.MaxUploadSizeBytes;
|
||||
|
||||
if (context.HttpContext.Request.ContentLength > config.Value.MaxUploadSizeBytes)
|
||||
throw GracefulException.RequestEntityTooLarge("Attachment is too large.",
|
||||
$"The media upload size limit is set to {config.Value.MaxUploadSizeBytes} bytes.");
|
||||
}
|
||||
|
||||
public void OnResourceExecuted(ResourceExecutedContext context) { }
|
||||
}
|
|
@ -82,6 +82,7 @@ public class DriveController(
|
|||
[Authorize]
|
||||
[Produces(MediaTypeNames.Application.Json)]
|
||||
[ProducesResults(HttpStatusCode.OK)]
|
||||
[MaxRequestSizeIsMaxUploadSize]
|
||||
public async Task<DriveFileResponse> UploadFile(IFormFile file)
|
||||
{
|
||||
var user = HttpContext.GetUserOrFail();
|
||||
|
|
|
@ -232,6 +232,9 @@ public class GracefulException(
|
|||
public static GracefulException Conflict(string message, string? details = null) =>
|
||||
new(HttpStatusCode.Conflict, message, details);
|
||||
|
||||
public static GracefulException RequestEntityTooLarge(string message, string? details = null) =>
|
||||
new(HttpStatusCode.RequestEntityTooLarge, message, details);
|
||||
|
||||
public static GracefulException RecordNotFound() => new(HttpStatusCode.NotFound, "Record not found");
|
||||
|
||||
public static GracefulException MisdirectedRequest() =>
|
||||
|
|
Loading…
Add table
Reference in a new issue