fix(storage): record historical object destinations
This commit is contained in:
1 parent
d572ad575b
commit
2b41ab4ea3
14 files changed
+272
-21
No files matched your search
@@ -13,7 +13,7 @@ namespace Heimdall.Infrastructure.Storage;
|
||||
/// (<c>STORAGE__PROVIDER</c>). Reads the DB each resolution so a change made
|
||||
/// in the UI is picked up without restart (broadcast reload on top).
|
||||
/// </summary>
|
||||
public sealed class StorageProviderFactory : IStorageProvider
|
||||
public sealed class StorageProviderFactory : IStorageProvider, Heimdall.Application.Common.IStorageObjectDeletion
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
@@ -44,7 +44,62 @@ public sealed class StorageProviderFactory : IStorageProvider
|
||||
|
||||
public async Task UploadAsync(string key, Stream content, string contentType, CancellationToken ct = default)
|
||||
{
|
||||
var provider = Resolve();
|
||||
var location = CaptureCurrentLocation();
|
||||
await UploadAsync(key, content, contentType, location, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>Captures the current destination for persistence alongside an object reference.</summary>
|
||||
public Heimdall.Domain.Entities.StorageLocation CaptureCurrentLocation()
|
||||
{
|
||||
using var scope = _services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<IApplicationDbContext>();
|
||||
var provider = GetSetting(db, "storage.provider");
|
||||
if (string.IsNullOrWhiteSpace(provider))
|
||||
provider = Environment.GetEnvironmentVariable("STORAGE__PROVIDER");
|
||||
if (string.IsNullOrWhiteSpace(provider))
|
||||
provider = "Local";
|
||||
|
||||
if (provider.Equals("Local", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var root = GetSetting(db, "storage.local.path");
|
||||
root = string.IsNullOrWhiteSpace(root)
|
||||
? Environment.GetEnvironmentVariable("STORAGE__LOCAL__PATH") ?? "./data/storage"
|
||||
: root;
|
||||
return new Heimdall.Domain.Entities.StorageLocation(
|
||||
Heimdall.Domain.Enums.StorageProvider.Local,
|
||||
LocalRoot: Path.GetFullPath(root));
|
||||
}
|
||||
|
||||
if (provider.Equals("S3", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var endpoint = GetSetting(db, "storage.s3.endpoint");
|
||||
if (string.IsNullOrWhiteSpace(endpoint))
|
||||
endpoint = Environment.GetEnvironmentVariable("ALARIK_ENDPOINT");
|
||||
if (string.IsNullOrWhiteSpace(endpoint))
|
||||
endpoint = Environment.GetEnvironmentVariable("S3_ENDPOINT");
|
||||
var bucket = GetSetting(db, "storage.s3.bucket");
|
||||
bucket = string.IsNullOrWhiteSpace(bucket) ? "heimdall" : bucket;
|
||||
var location = new Heimdall.Domain.Entities.StorageLocation(
|
||||
Heimdall.Domain.Enums.StorageProvider.S3,
|
||||
S3Endpoint: endpoint,
|
||||
S3Bucket: bucket);
|
||||
if (!location.IsComplete)
|
||||
throw new InvalidOperationException("S3 storage is not configured; no object can be written.");
|
||||
return location;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Unknown storage provider '{provider}'.");
|
||||
}
|
||||
|
||||
/// <summary>Uploads to an explicitly captured destination so persisted metadata matches the object.</summary>
|
||||
public async Task UploadAsync(
|
||||
string key,
|
||||
Stream content,
|
||||
string contentType,
|
||||
Heimdall.Domain.Entities.StorageLocation location,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var provider = Resolve(location);
|
||||
try { await provider.UploadAsync(key, content, contentType, ct).ConfigureAwait(false); }
|
||||
finally { DisposeProvider(provider); }
|
||||
}
|
||||
@@ -87,6 +142,14 @@ public sealed class StorageProviderFactory : IStorageProvider
|
||||
finally { DisposeProvider(resolved); }
|
||||
}
|
||||
|
||||
/// <summary>Deletes using the exact non-secret destination captured on the owning object.</summary>
|
||||
public async Task DeleteAsync(string key, Heimdall.Domain.Entities.StorageLocation location, CancellationToken ct = default)
|
||||
{
|
||||
var resolved = Resolve(location);
|
||||
try { await resolved.DeleteAsync(key, ct).ConfigureAwait(false); }
|
||||
finally { DisposeProvider(resolved); }
|
||||
}
|
||||
|
||||
public async Task DeleteBatchAsync(IReadOnlyCollection<string> keys, CancellationToken ct = default)
|
||||
{
|
||||
var provider = Resolve();
|
||||
@@ -104,6 +167,17 @@ public sealed class StorageProviderFactory : IStorageProvider
|
||||
finally { DisposeProvider(provider); }
|
||||
}
|
||||
|
||||
/// <summary>Deletes a batch from the exact non-secret destination captured on the owning objects.</summary>
|
||||
public async Task DeleteBatchAsync(
|
||||
IReadOnlyCollection<string> keys,
|
||||
Heimdall.Domain.Entities.StorageLocation location,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var provider = Resolve(location);
|
||||
try { await provider.DeleteBatchAsync(keys, ct).ConfigureAwait(false); }
|
||||
finally { DisposeProvider(provider); }
|
||||
}
|
||||
|
||||
public async Task<bool> HealthCheckAsync(CancellationToken ct = default)
|
||||
{
|
||||
var provider = Resolve();
|
||||
@@ -149,6 +223,36 @@ public sealed class StorageProviderFactory : IStorageProvider
|
||||
scope.ServiceProvider.GetRequiredService<ILogger<LocalStorageProvider>>());
|
||||
}
|
||||
|
||||
private IStorageProvider Resolve(Heimdall.Domain.Entities.StorageLocation location)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(location);
|
||||
if (!location.IsComplete)
|
||||
throw new InvalidOperationException("Recorded storage location is incomplete; refusing to use current storage settings.");
|
||||
|
||||
using var scope = _services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<IApplicationDbContext>();
|
||||
if (location.Provider == Heimdall.Domain.Enums.StorageProvider.Local)
|
||||
return new LocalStorageProvider(location.LocalRoot,
|
||||
scope.ServiceProvider.GetRequiredService<ILogger<LocalStorageProvider>>());
|
||||
|
||||
if (location.Provider == Heimdall.Domain.Enums.StorageProvider.S3)
|
||||
{
|
||||
var s3 = new AlarikStorageProvider(
|
||||
location.S3Endpoint,
|
||||
location.S3Bucket,
|
||||
GetSetting(db, "storage.s3.accessKey"),
|
||||
GetSetting(db, "storage.s3.secretKey"),
|
||||
GetSetting(db, "storage.s3.region"),
|
||||
scope.ServiceProvider.GetRequiredService<ILogger<AlarikStorageProvider>>());
|
||||
if (s3.IsConfigured)
|
||||
return s3;
|
||||
s3.Dispose();
|
||||
throw new InvalidOperationException("Recorded S3 destination is not configured; refusing to use current storage settings.");
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Unknown recorded storage provider '{location.Provider}'.");
|
||||
}
|
||||
|
||||
private static string? GetSetting(IApplicationDbContext db, string key)
|
||||
{
|
||||
var setting = db.AppSettings
|
||||
|
||||
Reference in new issue
Block a user