summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.Cdn/Extensions/ImageController.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-05-04 19:31:13 +0200
committerRory& <root@rory.gay>2026-05-04 19:31:13 +0200
commit06c5f39cd1c093809a2143c0dfccd690f6b4404d (patch)
treee57986abfe522e5025341883aee3fe5a45747057 /extra/admin-api/Spacebar.Cdn/Extensions/ImageController.cs
parentswitch to harmony-erlpack (diff)
downloadserver-ts-06c5f39cd1c093809a2143c0dfccd690f6b4404d.tar.xz
CDN-CS work
Diffstat (limited to 'extra/admin-api/Spacebar.Cdn/Extensions/ImageController.cs')
-rw-r--r--extra/admin-api/Spacebar.Cdn/Extensions/ImageController.cs48
1 files changed, 23 insertions, 25 deletions
diff --git a/extra/admin-api/Spacebar.Cdn/Extensions/ImageController.cs b/extra/admin-api/Spacebar.Cdn/Extensions/ImageController.cs

index f8178540..9a70fd0d 100644 --- a/extra/admin-api/Spacebar.Cdn/Extensions/ImageController.cs +++ b/extra/admin-api/Spacebar.Cdn/Extensions/ImageController.cs
@@ -1,35 +1,33 @@ +using ArcaneLibs.Extensions.Streams; using Microsoft.AspNetCore.Mvc; using Spacebar.AdminApi.TestClient.Services.Services; +using Spacebar.Cdn.Services; +using Spacebar.Interop.Cdn.Abstractions; namespace Spacebar.Cdn.Extensions; +[ApiController] +public class ImageController(LruFileCache lfc, IFileSource fs, CdnWorkerService cws) : ControllerBase { + public async Task<IActionResult> GetImage(string path) { + DiscordImageResizeParams resizeParams = Request.GetResizeParams(); + var cacheKey = path + resizeParams.ToSerializedName(); + if (!Request.Query.Any() || resizeParams.Passthrough) { + await using var original = await fs.GetFile(path); + return new FileContentResult(original.Stream.ReadToEnd().ToArray(), original.MimeType); + } + + var entry = await lfc.GetOrAdd(cacheKey, async () => { + var original = await fs.GetFile(path); + var res = await cws.GetRawClient("q8").GetAsync("/scale" + path + Request.QueryString); + var outStream = await res.Content.ReadAsStreamAsync(); -public static class HttpRequestExtensions { - extension(HttpRequest request) { - public DiscordImageResizeParams GetResizeParams() { - return new() { - Size = request.Query.ContainsKey("size") && uint.TryParse(request.Query["size"], out uint size) ? size : null, - Quality = request.Query.ContainsKey("quality") && Enum.TryParse<DiscordImageResizeQuality>(request.Query["quality"], true, out var quality) - ? quality - : DiscordImageResizeQuality.High, - KeepAspectRatio = !request.Query.ContainsKey("keepAspectRatio") || !bool.TryParse(request.Query["keepAspectRatio"], out bool kar) || kar, - Passthrough = request.Query.ContainsKey("passthrough") && bool.TryParse(request.Query["passthrough"], out bool pt) && pt, - Animated = request.Query.ContainsKey("animated") && bool.TryParse(request.Query["animated"], out bool an) && an, - SpacebarAllowUpscale = request.Query.ContainsKey("allowUpscale") && bool.TryParse(request.Query["allowUpscale"], out bool au) && au, - SpacebarOptimiseGif = request.Query.ContainsKey("optimiseGif") && bool.TryParse(request.Query["optimiseGif"], out bool og) && og, + return new LruFileCache.Entry() { + Data = outStream.ReadToEnd().ToArray(), + MimeType = res.Content.Headers.ContentType?.ToString() ?? original.MimeType }; - } - } - - extension (HttpResponse response) { - public void SetSuccessCacheHeader() { - int cacheDuration = (int)TimeSpan.FromHours(6).TotalSeconds; - response.Headers.CacheControl = $"public, max-age={cacheDuration}, s-maxage={cacheDuration}, immutable"; - } + }); - public void SetFailureCacheHeader() { - int cacheDuration = (int)TimeSpan.FromMinutes(5).TotalSeconds; - response.Headers.CacheControl = $"public, max-age={cacheDuration}, s-maxage={cacheDuration}, immutable"; - } + // byte array with mime type result + return new FileContentResult(entry.Data, entry.MimeType); } } \ No newline at end of file