From dfe942d3cf4a60c06a530c64793981bfff5fe9fd Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Mon, 30 Mar 2026 04:39:14 +1100 Subject: v0.5a --- Controllers/MediaController.cs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'Controllers/MediaController.cs') diff --git a/Controllers/MediaController.cs b/Controllers/MediaController.cs index 2c015f7..cd6916f 100644 --- a/Controllers/MediaController.cs +++ b/Controllers/MediaController.cs @@ -8,18 +8,26 @@ namespace HyperBooru.Controllers; [ApiController] [Route("/media")] public class MediaController : Controller { - private IMediaService mediaService; - private IConfigService config; - private HBContext db; + private IHttpContextAccessor httpContextAccessor; + private IMediaService mediaService; + private IConfigService config; + private HBContext db; + + private readonly string[] FormatPriority = [ + "image/webp", + "image/png" + ]; public MediaController( + IHttpContextAccessor httpContextAccessor, IMediaService mediaService, IConfigService config, HBContext db) { - this.mediaService = mediaService; - this.config = config; - this.db = db; + this.httpContextAccessor = httpContextAccessor; + this.mediaService = mediaService; + this.config = config; + this.db = db; } [HttpGet("{mediaId}")] @@ -30,9 +38,25 @@ public class MediaController : Controller { if(media is null) return NotFound(); - var fs = System.IO.File.OpenRead(mediaService.GetPath(media)); + // Check if the requested media item is a HEIC image and if it is, convert it + // otherwise, return the original file content, unaltered + if(media.CurrentUploadedFile!.MimeType == "image/heic") { + // If the media needs to be converted, check the HTTP request for allowed + // media formats, and convert to the best available format or WebP otherwise + var allowedTypes = httpContextAccessor + .HttpContext? + .Request + .GetTypedHeaders().Accept.Select(h => h.MediaType.ToString()) ?? Array.Empty(); + + var format = FormatPriority.FirstOrDefault(f => allowedTypes.Contains(f)) ?? "image/webp"; - return new FileStreamResult(fs, media.CurrentUploadedFile!.MimeType); + var fs = mediaService.GetConverted(media, format); + + return new FileStreamResult(fs, format); + } else { + var fs = System.IO.File.OpenRead(mediaService.GetPath(media)); + return new FileStreamResult(fs, media.CurrentUploadedFile!.MimeType); + } } [HttpGet("thumb/{mediaId}")] -- cgit v1.3