fixed filename handling in url
Some checks failed
Build and Push Image / build-and-push (push) Failing after 4m36s

This commit is contained in:
2025-12-14 15:44:46 -05:00
parent 8964d1c069
commit db36e218ff

View File

@@ -14,9 +14,10 @@ namespace AobaServer.Controllers;
public class MediaController(AobaService aobaService, ILogger<MediaController> logger) : Controller public class MediaController(AobaService aobaService, ILogger<MediaController> logger) : Controller
{ {
[HttpGet("{id}")] [HttpGet("{id}")]
[HttpGet("{id}/*")] //[HttpGet("{id}/*")]
[HttpGet("{id}/{*fileName}")]
[ResponseCache(Duration = int.MaxValue)] [ResponseCache(Duration = int.MaxValue)]
public async Task<IActionResult> MediaAsync(ObjectId id, [FromServices] MongoClient client, CancellationToken cancellationToken) public async Task<IActionResult> MediaAsync(ObjectId id, [FromQuery] string? fileName, [FromServices] MongoClient client, CancellationToken cancellationToken)
{ {
var file = await aobaService.GetFileStreamAsync(id, seekable: true, cancellationToken: cancellationToken); var file = await aobaService.GetFileStreamAsync(id, seekable: true, cancellationToken: cancellationToken);
if (file.HasError) if (file.HasError)
@@ -29,6 +30,22 @@ public class MediaController(AobaService aobaService, ILogger<MediaController> l
return File(file, mime, true); return File(file, mime, true);
} }
[HttpGet("{id}/dl")]
[HttpGet("{id}/dl/{*fileName}")]
[ResponseCache(Duration = int.MaxValue)]
public async Task<IActionResult> 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);
}
/// <summary> /// <summary>
/// Redirect legacy media urls to the new url /// Redirect legacy media urls to the new url
/// </summary> /// </summary>