using VoidCat.Model; using VoidCat.Services.Abstractions; namespace VoidCat.Services.Files; /// /// Primary class for accessing implementations /// public class FileStoreFactory : IFileStore { private readonly IFileMetadataStore _metadataStore; private readonly IEnumerable _fileStores; public FileStoreFactory(IEnumerable fileStores, IFileMetadataStore metadataStore) { _fileStores = fileStores; _metadataStore = metadataStore; } /// /// Get files store interface by key /// /// /// public IFileStore? GetFileStore(string? key) { if (key == default && _fileStores.Count() == 1) { return _fileStores.First(); } return _fileStores.FirstOrDefault(a => a.Key == key); } /// public string? Key => null; /// public ValueTask Ingress(IngressPayload payload, CancellationToken cts) { var store = GetFileStore(payload.Meta.Storage!); if (store == default) { throw new InvalidOperationException($"Cannot find store '{payload.Meta.Storage}'"); } return store.Ingress(payload, cts); } /// public async ValueTask Egress(EgressRequest request, Stream outStream, CancellationToken cts) { var store = await GetStore(request.Id); await store.Egress(request, outStream, cts); } /// public async ValueTask DeleteFile(Guid id) { var store = await GetStore(id); await store.DeleteFile(id); } /// public async ValueTask Open(EgressRequest request, CancellationToken cts) { var store = await GetStore(request.Id); return await store.Open(request, cts); } /// /// Get file store for a file by id /// /// /// /// private async Task GetStore(Guid id) { var meta = await _metadataStore.Get(id); var store = GetFileStore(meta?.Storage); if (store == default) { throw new InvalidOperationException($"Cannot find store '{meta?.Storage}'"); } return store; } }