mirror of
https://git.v0l.io/Kieran/void.cat.git
synced 2025-11-15 06:46:33 +01:00
Refactor basic store
This commit is contained in:
@@ -28,6 +28,11 @@ public class LocalDiskFileMetadataStore : IFileMetadataStore
|
||||
return GetMeta<TMeta>(id);
|
||||
}
|
||||
|
||||
public ValueTask<VoidFileMeta?> Get(Guid id)
|
||||
{
|
||||
return GetMeta<VoidFileMeta>(id);
|
||||
}
|
||||
|
||||
public async ValueTask Set(Guid id, SecretVoidFileMeta meta)
|
||||
{
|
||||
var path = MapMeta(id);
|
||||
|
||||
@@ -30,10 +30,7 @@ public class LocalDiskFileStore : StreamFileStore, IFileStore
|
||||
|
||||
public async ValueTask Egress(EgressRequest request, Stream outStream, CancellationToken cts)
|
||||
{
|
||||
var path = MapPath(request.Id);
|
||||
if (!File.Exists(path)) throw new VoidFileNotFoundException(request.Id);
|
||||
|
||||
await using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
|
||||
await using var fs = await Open(request, cts);
|
||||
await EgressFromStream(fs, request, outStream, cts);
|
||||
}
|
||||
|
||||
@@ -100,6 +97,14 @@ public class LocalDiskFileStore : StreamFileStore, IFileStore
|
||||
await _metadataStore.Delete(id);
|
||||
}
|
||||
|
||||
public ValueTask<Stream> Open(EgressRequest request, CancellationToken cts)
|
||||
{
|
||||
var path = MapPath(request.Id);
|
||||
if (!File.Exists(path)) throw new VoidFileNotFoundException(request.Id);
|
||||
|
||||
return ValueTask.FromResult<Stream>(new FileStream(path, FileMode.Open, FileAccess.Read));
|
||||
}
|
||||
|
||||
private string MapPath(Guid id) =>
|
||||
Path.Join(_settings.DataDirectory, FilesDir, id.ToString());
|
||||
}
|
||||
@@ -20,7 +20,33 @@ public class S3FileMetadataStore : IFileMetadataStore
|
||||
_client = _config.CreateClient();
|
||||
}
|
||||
|
||||
public async ValueTask<TMeta?> Get<TMeta>(Guid id) where TMeta : VoidFileMeta
|
||||
public ValueTask<TMeta?> Get<TMeta>(Guid id) where TMeta : VoidFileMeta
|
||||
{
|
||||
return GetMeta<TMeta>(id);
|
||||
}
|
||||
|
||||
public ValueTask<VoidFileMeta?> Get(Guid id)
|
||||
{
|
||||
return GetMeta<VoidFileMeta>(id);
|
||||
}
|
||||
|
||||
public async ValueTask Set(Guid id, SecretVoidFileMeta meta)
|
||||
{
|
||||
await _client.PutObjectAsync(new()
|
||||
{
|
||||
BucketName = _config.BucketName,
|
||||
Key = ToKey(id),
|
||||
ContentBody = JsonConvert.SerializeObject(meta),
|
||||
ContentType = "application/json"
|
||||
});
|
||||
}
|
||||
|
||||
public async ValueTask Delete(Guid id)
|
||||
{
|
||||
await _client.DeleteObjectAsync(_config.BucketName, ToKey(id));
|
||||
}
|
||||
|
||||
private async ValueTask<TMeta?> GetMeta<TMeta>(Guid id) where TMeta : VoidFileMeta
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -48,22 +74,6 @@ public class S3FileMetadataStore : IFileMetadataStore
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public async ValueTask Set(Guid id, SecretVoidFileMeta meta)
|
||||
{
|
||||
await _client.PutObjectAsync(new()
|
||||
{
|
||||
BucketName = _config.BucketName,
|
||||
Key = ToKey(id),
|
||||
ContentBody = JsonConvert.SerializeObject(meta),
|
||||
ContentType = "application/json"
|
||||
});
|
||||
}
|
||||
|
||||
public async ValueTask Delete(Guid id)
|
||||
{
|
||||
await _client.DeleteObjectAsync(_config.BucketName, ToKey(id));
|
||||
}
|
||||
|
||||
|
||||
private static string ToKey(Guid id) => $"{id}-metadata";
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class S3FileStore : StreamFileStore, IFileStore
|
||||
},
|
||||
Headers =
|
||||
{
|
||||
ContentLength = (long)payload.Meta.Size
|
||||
ContentLength = (long) payload.Meta.Size
|
||||
}
|
||||
};
|
||||
|
||||
@@ -50,19 +50,8 @@ public class S3FileStore : StreamFileStore, IFileStore
|
||||
|
||||
public async ValueTask Egress(EgressRequest request, Stream outStream, CancellationToken cts)
|
||||
{
|
||||
var req = new GetObjectRequest()
|
||||
{
|
||||
BucketName = _config.BucketName,
|
||||
Key = request.Id.ToString()
|
||||
};
|
||||
if (request.Ranges.Any())
|
||||
{
|
||||
var r = request.Ranges.First();
|
||||
req.ByteRange = new ByteRange(r.OriginalString);
|
||||
}
|
||||
|
||||
var obj = await _client.GetObjectAsync(req, cts);
|
||||
await EgressFull(request.Id, obj.ResponseStream, outStream, cts);
|
||||
await using var stream = await Open(request, cts);
|
||||
await EgressFull(request.Id, stream, outStream, cts);
|
||||
}
|
||||
|
||||
public async ValueTask<PagedResult<PublicVoidFile>> ListFiles(PagedRequest request)
|
||||
@@ -124,4 +113,21 @@ public class S3FileStore : StreamFileStore, IFileStore
|
||||
{
|
||||
await _client.DeleteObjectAsync(_config.BucketName, id.ToString());
|
||||
}
|
||||
|
||||
public async ValueTask<Stream> Open(EgressRequest request, CancellationToken cts)
|
||||
{
|
||||
var req = new GetObjectRequest()
|
||||
{
|
||||
BucketName = _config.BucketName,
|
||||
Key = request.Id.ToString()
|
||||
};
|
||||
if (request.Ranges.Any())
|
||||
{
|
||||
var r = request.Ranges.First();
|
||||
req.ByteRange = new ByteRange(r.OriginalString);
|
||||
}
|
||||
|
||||
var obj = await _client.GetObjectAsync(req, cts);
|
||||
return obj.ResponseStream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user