Optimized image storage

This commit is contained in:
Mia Rose Winter 2024-01-14 19:24:38 +01:00
parent 6e0b4f75b7
commit cc8efee25f
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
3 changed files with 13 additions and 2 deletions

View file

@ -60,8 +60,10 @@
var guid = await ImageService.StoreImageAsync(tempFilePath);
if (!guid.HasValue) throw new ApplicationException("Processing Image failed.");
Guid? imageToDelete = null;
await using var context = await ContextFactory.CreateDbContextAsync();
if (User.ProfilePicture is not null) {
imageToDelete = User.ProfilePicture.ImageId;
context.Remove(User.ProfilePicture);
}
@ -72,7 +74,9 @@
context.Update(User);
await context.SaveChangesAsync();
// TODO notify changed pfp
if (imageToDelete is not null)
ImageService.Delete(imageToDelete.Value);
await InvokeAsync(StateHasChanged);
}
}

View file

@ -30,7 +30,8 @@
try {
string tempPath = await FileUtilities.StoreTemporary(args.File.OpenReadStream(MaxFileSize));
await FileUploadedCallback.InvokeAsync(tempPath);
Message = string.Empty;
Message = string.Empty;
File.Delete(tempPath);
} catch (Exception ex) {
Message = "File upload failed: " + ex.Message;
} finally {

View file

@ -1,4 +1,5 @@
using ImageMagick;
using static System.Net.Mime.MediaTypeNames;
namespace Wave.Services;
@ -47,4 +48,9 @@ public class ImageService(ILogger<ImageService> logger) {
return null;
}
}
public void Delete(Guid imageId) {
string path = Path.Combine(BasePath, imageId + ImageExtension);
File.Delete(path);
}
}