blob: b0990706df27189b9a698bb825c55176027a176b (
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
|
using System.Net;
using ArcaneLibs;
using ArcaneLibs.Extensions;
using ArcaneLibs.Extensions.Streams;
namespace Spacebar.Interop.Cdn.Abstractions;
/// <summary>
/// Class only exists as a stepping stone between old cdn and migrations...
/// </summary>
/// <param name="baseUrl"></param>
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<IFileSource> Init(CancellationToken? cancellationToken = null) {
return this;
}
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
};
}
public async Task<bool> FileExists(string path, CancellationToken? cancellationToken = null) {
var res = await _httpClient.SendUnhandledAsync(new(HttpMethod.Head, path), cancellationToken);
if (!res.IsSuccessStatusCode) {
await using var s = await res.Content.ReadAsStreamAsync();
Console.WriteLine($"Got {res.StatusCode}: ({res.Content.Headers.ContentLength}b of {res.Content.Headers.ContentType})\n{s.ReadToEnd().AsString()}");
}
return res.IsSuccessStatusCode;
}
public async Task WriteFile(string path, Stream stream) {
Console.WriteLine("Can't write to HTTP store! Ignoring.");
}
}
|