From db36e218ffab53d81b36ae5be49028f278b054c2 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sun, 14 Dec 2025 15:44:46 -0500 Subject: [PATCH] fixed filename handling in url --- AobaServer/Controllers/MediaController.cs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/AobaServer/Controllers/MediaController.cs b/AobaServer/Controllers/MediaController.cs index 0625f92..e848223 100644 --- a/AobaServer/Controllers/MediaController.cs +++ b/AobaServer/Controllers/MediaController.cs @@ -14,9 +14,10 @@ namespace AobaServer.Controllers; public class MediaController(AobaService aobaService, ILogger logger) : Controller { [HttpGet("{id}")] - [HttpGet("{id}/*")] + //[HttpGet("{id}/*")] + [HttpGet("{id}/{*fileName}")] [ResponseCache(Duration = int.MaxValue)] - public async Task MediaAsync(ObjectId id, [FromServices] MongoClient client, CancellationToken cancellationToken) + public async Task MediaAsync(ObjectId id, [FromQuery] string? fileName, [FromServices] MongoClient client, CancellationToken cancellationToken) { var file = await aobaService.GetFileStreamAsync(id, seekable: true, cancellationToken: cancellationToken); if (file.HasError) @@ -29,6 +30,22 @@ public class MediaController(AobaService aobaService, ILogger l return File(file, mime, true); } + [HttpGet("{id}/dl")] + [HttpGet("{id}/dl/{*fileName}")] + [ResponseCache(Duration = int.MaxValue)] + public async Task MediaDownloadAsync(ObjectId id, [FromQuery] string? fileName, [FromServices] MongoClient client, CancellationToken cancellationToken) + { + 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, fileName ?? file.Value.FileInfo.Filename, true); + } + /// /// Redirect legacy media urls to the new url ///