From b8c2de705db54245b7d3576b221014ee2c22aa30 Mon Sep 17 00:00:00 2001 From: XShine555 Date: Thu, 28 May 2026 21:04:09 +0200 Subject: [PATCH] PreventOverwrite --- Application/Contracts/IStorageService.cs | 6 ++---- Infrastructure/Services/StorageService.cs | 6 +++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Application/Contracts/IStorageService.cs b/Application/Contracts/IStorageService.cs index 388d680..2a29c15 100644 --- a/Application/Contracts/IStorageService.cs +++ b/Application/Contracts/IStorageService.cs @@ -4,12 +4,11 @@ namespace Musify.Application.Contracts { Task GetFileAsync(string bucket, string key, CancellationToken cancellationToken); - // Returns null if the object does not exist Task HeadObjectAsync(string bucket, string key, CancellationToken cancellationToken); Task GetUrlAsync(string bucket, string key, TimeSpan ExpirationTime, CancellationToken cancellationToken); - Task GetUploadUrlAsync(string bucket, string key, string contentType, TimeSpan expirationTime, CancellationToken cancellationToken); + Task GetUploadUrlAsync(string bucket, string key, string contentType, TimeSpan expirationTime, CancellationToken cancellationToken, bool preventOverwrite = true); Task UploadFileAsync(string filePath, string bucket, string key, CancellationToken cancellationToken); @@ -23,8 +22,7 @@ namespace Musify.Application.Contracts CancellationToken cancellationToken); Task RemoveFolderAsync(string bucket, string folderKey, CancellationToken cancellationToken); - - // Returns (key, lastModifiedUtc) for all objects under the given prefix + IAsyncEnumerable<(string Key, DateTime LastModifiedUtc)> ListObjectsAsync(string bucket, string prefix, CancellationToken cancellationToken); } } \ No newline at end of file diff --git a/Infrastructure/Services/StorageService.cs b/Infrastructure/Services/StorageService.cs index 7f11820..3ea9804 100644 --- a/Infrastructure/Services/StorageService.cs +++ b/Infrastructure/Services/StorageService.cs @@ -55,7 +55,8 @@ namespace Musify.Infrastructure.Services string key, string contentType, TimeSpan expirationTime, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + bool preventOverwrite = true) { var request = new GetPreSignedUrlRequest { @@ -67,6 +68,9 @@ namespace Musify.Infrastructure.Services ContentType = contentType }; + if (preventOverwrite) + request.Headers["If-None-Match"] = "*"; + logger.LogDebug("Generating pre-signed upload URL for {Bucket}/{Key}", bucket, key); return await amazonS3.GetPreSignedURLAsync(request); }