summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.Cdn/Services/ProxyFileSource.cs
diff options
context:
space:
mode:
Diffstat (limited to 'extra/admin-api/Spacebar.Cdn/Services/ProxyFileSource.cs')
-rw-r--r--extra/admin-api/Spacebar.Cdn/Services/ProxyFileSource.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/extra/admin-api/Spacebar.Cdn/Services/ProxyFileSource.cs b/extra/admin-api/Spacebar.Cdn/Services/ProxyFileSource.cs
new file mode 100644

index 00000000..bc6ab2ae --- /dev/null +++ b/extra/admin-api/Spacebar.Cdn/Services/ProxyFileSource.cs
@@ -0,0 +1,29 @@ +namespace Spacebar.AdminApi.TestClient.Services.Services; + +public class ProxyFileSource(string baseUrl) : IFileSource { + private static LruFileCache _cache = new(100 * 1024 * 1024); // 100 MB + + private readonly StreamingHttpClient _httpClient = new() { + BaseAddress = new Uri(baseUrl) + }; + + public string BaseUrl => baseUrl; + + public async Task<FileInfo> GetFile(string path, CancellationToken? cancellationToken = null) { + var res = await _cache.GetOrAdd(path, async () => { + var res = await _httpClient.SendUnhandledAsync(new(HttpMethod.Get, path), cancellationToken); + res.EnsureSuccessStatusCode(); + var ms = new MemoryStream(); + await res.Content.CopyToAsync(ms); + return new LruFileCache.Entry { + Data = ms.ToArray(), + MimeType = res.Content.Headers.ContentType?.MediaType ?? "application/octet-stream" + }; + }); + + return new() { + Stream = new MemoryStream(res.Data), + MimeType = res.MimeType + }; + } +} \ No newline at end of file