[backend/drive] Set content-type when uploading media to object storage

This commit is contained in:
Laura Hausmann 2024-05-07 02:55:17 +02:00
parent 922a759fb1
commit 16fdc48800
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 14 additions and 11 deletions

View file

@ -216,7 +216,7 @@ public class DriveService(
else
{
data.Seek(0, SeekOrigin.Begin);
await storageSvc.UploadFileAsync(filename, data);
await storageSvc.UploadFileAsync(filename, request.MimeType, data);
url = storageSvc.GetFilePublicUrl(filename).AbsoluteUri;
if (thumbnailFilename != null && res?.RenderThumbnail != null)
@ -226,7 +226,7 @@ public class DriveService(
await using var stream = new MemoryStream();
await res.RenderThumbnail(stream);
stream.Seek(0, SeekOrigin.Begin);
await storageSvc.UploadFileAsync(thumbnailFilename, stream);
await storageSvc.UploadFileAsync(thumbnailFilename, "image/webp", stream);
thumbnailUrl = storageSvc.GetFilePublicUrl(thumbnailFilename).AbsoluteUri;
}
catch (Exception e)
@ -242,7 +242,7 @@ public class DriveService(
await using var stream = new MemoryStream();
await res.RenderWebpublic(stream);
stream.Seek(0, SeekOrigin.Begin);
await storageSvc.UploadFileAsync(webpublicFilename, stream);
await storageSvc.UploadFileAsync(webpublicFilename, "image/webp", stream);
webpublicUrl = storageSvc.GetFilePublicUrl(webpublicFilename).AbsoluteUri;
}
catch (Exception e)
@ -267,7 +267,7 @@ public class DriveService(
else
{
data.Seek(0, SeekOrigin.Begin);
await storageSvc.UploadFileAsync(filename, data);
await storageSvc.UploadFileAsync(filename, request.MimeType, data);
url = storageSvc.GetFilePublicUrl(filename).AbsoluteUri;
}
}

View file

@ -61,7 +61,7 @@ public class ObjectStorageService(IOptions<Config.StorageSection> config, HttpCl
const string filename = ".iceshrimp-test";
var content = CryptographyHelpers.GenerateRandomString(16);
await UploadFileAsync(filename, Encoding.UTF8.GetBytes(content));
await UploadFileAsync(filename, "text/plain", Encoding.UTF8.GetBytes(content));
string result;
try
@ -78,12 +78,15 @@ public class ObjectStorageService(IOptions<Config.StorageSection> config, HttpCl
throw new Exception("Failed to verify access url (content mismatch)");
}
private Task UploadFileAsync(string filename, byte[] data) => UploadFileAsync(filename, new MemoryStream(data));
private Task UploadFileAsync(string filename, string contentType, byte[] data) =>
UploadFileAsync(filename, contentType, new MemoryStream(data));
public async Task UploadFileAsync(string filename, Stream data)
public async Task UploadFileAsync(string filename, string contentType, Stream data)
{
if (_bucket == null) throw new Exception("Refusing to upload to object storage with invalid configuration");
var blob = new Blob(GetFilenameWithPrefix(filename), data, _acl ?? BlobProperties.Empty);
var properties = (_acl ?? BlobProperties.Empty).ToDictionary();
properties.Add("Content-Type", contentType);
var blob = new Blob(GetFilenameWithPrefix(filename), data, properties);
await _bucket.PutAsync(blob);
}

View file

@ -55,7 +55,7 @@ public class StorageMaintenanceService(
var path = Path.Join(pathBase, file.AccessKey);
var stream = File.OpenRead(path);
await objectStorageSvc.UploadFileAsync(file.AccessKey, stream);
await objectStorageSvc.UploadFileAsync(file.AccessKey, file.Type, stream);
file.Url = objectStorageSvc.GetFilePublicUrl(file.AccessKey).AbsoluteUri;
pathsToDelete.Add(path);
}
@ -65,7 +65,7 @@ public class StorageMaintenanceService(
var path = Path.Join(pathBase, file.ThumbnailAccessKey);
var stream = File.OpenRead(path);
await objectStorageSvc.UploadFileAsync(file.ThumbnailAccessKey, stream);
await objectStorageSvc.UploadFileAsync(file.ThumbnailAccessKey, "image/webp", stream);
file.ThumbnailUrl = objectStorageSvc.GetFilePublicUrl(file.ThumbnailAccessKey).AbsoluteUri;
pathsToDelete.Add(path);
}
@ -75,7 +75,7 @@ public class StorageMaintenanceService(
var path = Path.Join(pathBase, file.WebpublicAccessKey);
var stream = File.OpenRead(path);
await objectStorageSvc.UploadFileAsync(file.WebpublicAccessKey, stream);
await objectStorageSvc.UploadFileAsync(file.WebpublicAccessKey, "image/webp", stream);
file.WebpublicUrl = objectStorageSvc.GetFilePublicUrl(file.WebpublicAccessKey).AbsoluteUri;
pathsToDelete.Add(path);
}