Implemented bucket delete and improved avatar update

This commit is contained in:
Marcel Baumgartner 2023-11-10 15:10:09 +01:00
parent f779e5e920
commit d8bb2b7356
2 changed files with 29 additions and 3 deletions

View file

@ -63,4 +63,22 @@ public class BucketService
else
throw new FileNotFoundException();
}
public Task Delete(string bucket, string file, bool ignoreNotFound = false)
{
var filePath = PathBuilder.File(BasePath, bucket, file);
if (File.Exists(filePath))
{
File.Delete(filePath);
return Task.CompletedTask;
}
// This section will only be reached if the file does not exist
if (!ignoreNotFound)
throw new FileNotFoundException();
return Task.CompletedTask;
}
}

View file

@ -16,17 +16,25 @@ public class UserDetailsService
public async Task UpdateAvatar(User user, Stream stream, string fileName)
{
if (user.Avatar != null)
{
await BucketService.Delete("avatars", user.Avatar, true);
}
var file = await BucketService.Store("avatars", stream, fileName);
user.Avatar = file;
UserRepository.Update(user);
}
public Task UpdateAvatar(User user) // Overload to reset avatar
public async Task UpdateAvatar(User user) // Overload to reset avatar
{
if (user.Avatar != null)
{
await BucketService.Delete("avatars", user.Avatar, true);
}
user.Avatar = null;
UserRepository.Update(user);
return Task.CompletedTask;
}
}