diff --git a/Iceshrimp.Backend/Core/Configuration/Config.cs b/Iceshrimp.Backend/Core/Configuration/Config.cs index b11665b2..65eb97a4 100644 --- a/Iceshrimp.Backend/Core/Configuration/Config.cs +++ b/Iceshrimp.Backend/Core/Configuration/Config.cs @@ -133,5 +133,6 @@ public sealed class Config public string? Bucket { get; init; } public string? Prefix { get; init; } public string? AccessUrl { get; init; } + public string? SetAcl { get; init; } } } \ No newline at end of file diff --git a/Iceshrimp.Backend/Core/Services/ObjectStorageService.cs b/Iceshrimp.Backend/Core/Services/ObjectStorageService.cs index d1394db9..69cd8525 100644 --- a/Iceshrimp.Backend/Core/Services/ObjectStorageService.cs +++ b/Iceshrimp.Backend/Core/Services/ObjectStorageService.cs @@ -18,6 +18,10 @@ public class ObjectStorageService(IOptions config, HttpCl private readonly string? _prefix = config.Value.ObjectStorage?.Prefix?.Trim('/'); + private readonly IReadOnlyDictionary? _acl = config.Value.ObjectStorage?.SetAcl != null + ? new Dictionary { { "x-amz-acl", config.Value.ObjectStorage.SetAcl } }.AsReadOnly() + : null; + private static S3Bucket? GetBucketSafely(IOptions config) { if (config.Value.Mode != Enums.FileStorage.Local) return GetBucket(config); @@ -71,24 +75,13 @@ public class ObjectStorageService(IOptions config, HttpCl throw new Exception("Failed to verify access url (content mismatch)"); } - 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)); - } + private Task UploadFileAsync(string filename, byte[] data) => UploadFileAsync(filename, new MemoryStream(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)); - } - - public async Task UploadFileAsync(byte[] data) - { - if (_bucket == null) throw new Exception("Refusing to upload to object storage with invalid configuration"); - var filename = Guid.NewGuid().ToString().ToLowerInvariant(); - await _bucket.PutAsync(new Blob(GetFilenameWithPrefix(filename), data)); - return filename; + var blob = new Blob(GetFilenameWithPrefix(filename), data, _acl ?? BlobProperties.Empty); + await _bucket.PutAsync(blob); } public Uri GetFilePublicUrl(string filename) diff --git a/Iceshrimp.Backend/configuration.ini b/Iceshrimp.Backend/configuration.ini index 32e4c474..8a56aff4 100644 --- a/Iceshrimp.Backend/configuration.ini +++ b/Iceshrimp.Backend/configuration.ini @@ -83,6 +83,7 @@ Path = /path/to/media/location ;;Bucket = ;;Prefix = ;;AccessUrl = https://endpoint.example.org/ +;;SetAcl = public-read [Logging:LogLevel] Default = Information