summary refs log tree commit diff
path: root/ReferenceClientProxyImplementation/Controllers/AssetsControllers.cs
blob: 8bd78bcf1d3500992505cb89ffd6a6f2f9ae80a3 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//#define MEMCACHE
using System.Collections.Frozen;
using ArcaneLibs.Extensions.Streams;
using Microsoft.AspNetCore.Mvc;
using ReferenceClientProxyImplementation.Configuration;
using ReferenceClientProxyImplementation.Services;

namespace ReferenceClientProxyImplementation.Controllers;

[Controller]
[Route("/")]
public class AssetsController(ProxyConfiguration proxyConfiguration, ClientStoreService clientStore) : Controller {
#if MEMCACHE
    private static FrozenDictionary<string, ReadOnlyMemory<byte>> memCache = new Dictionary<string, ReadOnlyMemory<byte>>().ToFrozenDictionary();
#endif

    [HttpGet("/assets/{*res:required}")]
    public async Task<IActionResult> Asset(string res) {
        if (res is "version.staging.json" or "version.internal.json")
            res = $"version.{proxyConfiguration.TestClient.Revision}.json";
        else if (res.EndsWith(".map")) {
            return NotFound();
        }

        var ext = res.Split(".").Last();
        var contentType = ext switch {
            //text types
            "html" => "text/html",
            "js" => "text/javascript",
            "css" => "text/css",
            "txt" => "text/plain",
            "csv" => "text/csv",
            "json" => "application/json",
            //image types
            "apng" => "image/apng",
            "gif" => "image/gif",
            "jpg" => "image/jpeg",
            "png" => "image/png",
            "svg" => "image/svg+xml",
            "webp" => "image/webp",
            "ico" => "image/x-icon",
            //script types
            "wasm" => "application/wasm",
            _ => "application/octet-stream"
        };
        // Response.Headers.ContentType = contentType;
#if MEMCACHE
        if (memCache.TryGetValue(res, out var value)) {
            // value.Position = 0;
            var cms = new MemoryStream(value.ToArray());
            // await value.CopyToAsync(cms);
            // ms.Position = 0;
            
            return new FileStreamResult(cms, contentType);
        }
#endif

#if MEMCACHE
        var stream = await clientStore.GetPatchedClientAsset("assets/" + res);
        stream.Position = 0;
        // var ms = new MemoryStream(stream.ReadToEnd().ToArray(), false);
        // memCache = memCache.Append(new KeyValuePair<string, MemoryStream>(res, ms)).ToFrozenDictionary();
        // return new FileStreamResult(ms, contentType);
        var mem = new ReadOnlyMemory<byte>(stream.ReadToEnd().ToArray());
        memCache = memCache.Append(new KeyValuePair<string, ReadOnlyMemory<byte>>(res, mem)).ToFrozenDictionary();
        return new FileStreamResult(new MemoryStream(mem.ToArray()), contentType);
#else
        return new FileStreamResult(await clientStore.GetPatchedClientAsset("assets/" + res), contentType);
#endif
        // return ;
    }
}