summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.Cdn/Controllers/StaticAssetController.cs
blob: 52b802a59094774ece14009d7d4bc3bd1c2532a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using ArcaneLibs.Extensions.Streams;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi;
using Spacebar.AdminApi.TestClient.Services.Services;
using Spacebar.Cdn.Extensions;
using Spacebar.Cdn.Services;
using Spacebar.Interop.Cdn.Abstractions;

namespace Spacebar.Cdn.Controllers;

[ApiController]
public class StaticAssetController(LruFileCache lfc, IFileSource fs, CdnWorkerService cws) : ControllerBase {
    [HttpGet("/embed/avatars/{avatarIdx}")]
    [HttpGet("/embed/avatars/{avatarIdx}.{ext}")]
    public async Task<IActionResult> GetUserAvatar(string avatarIdx, string ext = "png") {
        DiscordImageResizeParams resizeParams = Request.GetResizeParams();
        var cacheKey = Request.Path + resizeParams.ToSerializedName();
        LruFileCache.Entry? entry;
            entry = await lfc.GetOrAdd(cacheKey, async () => {
                
                var res = await cws.GetRawClient("q8").GetAsync(Request.Path + Request.QueryString);
                var outStream = await res.Content.ReadAsStreamAsync();

                return new LruFileCache.Entry() {
                    Data = outStream.ReadToEnd().ToArray(),
                    MimeType = res.Content.Headers.ContentType?.ToString() ?? Mimes.GetMime(Mimes.GetFormatForExtension(ext))
                };
            });

        return new FileContentResult(entry.Data, entry.MimeType);
    }
}