From 5e6b0b21a63eb0360c30ee0b16b0c9e16943f782 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sun, 14 Dec 2025 16:22:51 -0500 Subject: [PATCH] fix filename handling --- AobaServer/Controllers/MediaController.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/AobaServer/Controllers/MediaController.cs b/AobaServer/Controllers/MediaController.cs index 0625f92..16dabc2 100644 --- a/AobaServer/Controllers/MediaController.cs +++ b/AobaServer/Controllers/MediaController.cs @@ -14,7 +14,7 @@ namespace AobaServer.Controllers; public class MediaController(AobaService aobaService, ILogger logger) : Controller { [HttpGet("{id}")] - [HttpGet("{id}/*")] + [HttpGet("{id}/{*fn}")] [ResponseCache(Duration = int.MaxValue)] public async Task MediaAsync(ObjectId id, [FromServices] MongoClient client, CancellationToken cancellationToken) { @@ -29,6 +29,22 @@ public class MediaController(AobaService aobaService, ILogger l return File(file, mime, true); } + [HttpGet("{id}/dl")] + [HttpGet("{id}/dl/{*fn}")] + [ResponseCache(Duration = int.MaxValue)] + public async Task DownloadMediaAsync(ObjectId id, [FromServices] MongoClient client, [FromQuery] string? fn = null, CancellationToken cancellationToken = default) + { + var file = await aobaService.GetFileStreamAsync(id, seekable: true, cancellationToken: cancellationToken); + if (file.HasError) + { + logger.LogError(file.Error.Exception, "Failed to load media stream"); + return NotFound(); + } + var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename); + _ = aobaService.IncrementViewCountAsync(id, cancellationToken); + return File(file, mime, fn ?? file.Value.FileInfo.Filename, true); + } + /// /// Redirect legacy media urls to the new url ///