summaryrefslogtreecommitdiff
path: root/Controllers/MediaController.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2026-03-30 04:39:14 +1100
committerJake Mannens <jake@asger.xyz>2026-03-30 04:39:14 +1100
commitdfe942d3cf4a60c06a530c64793981bfff5fe9fd (patch)
tree07e351fb7aa412a90559a352908927593b38a171 /Controllers/MediaController.cs
parent9f4033649a53222b0543343fc02b0c74a72235b4 (diff)
v0.5av0.5a
Diffstat (limited to 'Controllers/MediaController.cs')
-rw-r--r--Controllers/MediaController.cs40
1 files changed, 32 insertions, 8 deletions
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<string>();
+
+ 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}")]