[backend/services] Improve handling of ObjectStorageService in local storage mode
This commit is contained in:
parent
1130c7b4c1
commit
81e8becef1
1 changed files with 20 additions and 2 deletions
|
@ -12,10 +12,21 @@ namespace Iceshrimp.Backend.Core.Services;
|
||||||
public class ObjectStorageService(IOptions<Config.StorageSection> config, HttpClient httpClient) {
|
public class ObjectStorageService(IOptions<Config.StorageSection> config, HttpClient httpClient) {
|
||||||
private readonly string? _accessUrl = config.Value.ObjectStorage?.AccessUrl;
|
private readonly string? _accessUrl = config.Value.ObjectStorage?.AccessUrl;
|
||||||
|
|
||||||
private readonly S3Bucket _bucket = GetBucket(config);
|
private readonly S3Bucket? _bucket = GetBucketSafely(config);
|
||||||
|
|
||||||
private readonly string? _prefix = config.Value.ObjectStorage?.Prefix?.Trim('/');
|
private readonly string? _prefix = config.Value.ObjectStorage?.Prefix?.Trim('/');
|
||||||
|
|
||||||
|
private static S3Bucket? GetBucketSafely(IOptions<Config.StorageSection> config) {
|
||||||
|
if (config.Value.Mode != Enums.FileStorage.Local) return GetBucket(config);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return GetBucket(config);
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static S3Bucket GetBucket(IOptions<Config.StorageSection> config) {
|
private static S3Bucket GetBucket(IOptions<Config.StorageSection> config) {
|
||||||
var s3Config = config.Value.ObjectStorage ?? throw new Exception("Invalid object storage configuration");
|
var s3Config = config.Value.ObjectStorage ?? throw new Exception("Invalid object storage configuration");
|
||||||
|
|
||||||
|
@ -52,14 +63,17 @@ public class ObjectStorageService(IOptions<Config.StorageSection> config, HttpCl
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UploadFileAsync(string filename, byte[] data) {
|
public async Task UploadFileAsync(string filename, byte[] data) {
|
||||||
|
if (_bucket == null) throw new Exception("Refusing to upload to object storage with invalid configuration");
|
||||||
await _bucket.PutAsync(new Blob(GetFilenameWithPrefix(filename), data));
|
await _bucket.PutAsync(new Blob(GetFilenameWithPrefix(filename), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UploadFileAsync(string filename, Stream data) {
|
public async Task UploadFileAsync(string filename, Stream data) {
|
||||||
|
if (_bucket == null) throw new Exception("Refusing to upload to object storage with invalid configuration");
|
||||||
await _bucket.PutAsync(new Blob(GetFilenameWithPrefix(filename), data));
|
await _bucket.PutAsync(new Blob(GetFilenameWithPrefix(filename), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> UploadFileAsync(byte[] data) {
|
public async Task<string> UploadFileAsync(byte[] data) {
|
||||||
|
if (_bucket == null) throw new Exception("Refusing to upload to object storage with invalid configuration");
|
||||||
var filename = Guid.NewGuid().ToString().ToLowerInvariant();
|
var filename = Guid.NewGuid().ToString().ToLowerInvariant();
|
||||||
await _bucket.PutAsync(new Blob(GetFilenameWithPrefix(filename), data));
|
await _bucket.PutAsync(new Blob(GetFilenameWithPrefix(filename), data));
|
||||||
return filename;
|
return filename;
|
||||||
|
@ -71,6 +85,8 @@ public class ObjectStorageService(IOptions<Config.StorageSection> config, HttpCl
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask<Stream?> GetFileAsync(string filename) {
|
public async ValueTask<Stream?> GetFileAsync(string filename) {
|
||||||
|
if (_bucket == null) throw new Exception("Refusing to get file from object storage with invalid configuration");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var res = await _bucket.GetAsync(GetFilenameWithPrefix(filename));
|
var res = await _bucket.GetAsync(GetFilenameWithPrefix(filename));
|
||||||
return await res.OpenAsync();
|
return await res.OpenAsync();
|
||||||
|
@ -81,6 +97,8 @@ public class ObjectStorageService(IOptions<Config.StorageSection> config, HttpCl
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task RemoveFilesAsync(params string[] filenames) {
|
public async Task RemoveFilesAsync(params string[] filenames) {
|
||||||
|
if (_bucket == null)
|
||||||
|
throw new Exception("Refusing to remove file from object storage with invalid configuration");
|
||||||
await _bucket.DeleteAsync(filenames.Select(GetFilenameWithPrefix).ToImmutableList());
|
await _bucket.DeleteAsync(filenames.Select(GetFilenameWithPrefix).ToImmutableList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue