-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/rename music file #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
75d6b7b
a392805
b6f35d1
7db1c54
4b0829f
9dc362d
37fabe1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using MyMusicLibrary.Communication.Request; | ||
| using MyMusicLibrary.Communication.Responses; | ||
|
|
||
| namespace MyMusicLibrary.Application.UseCases.Music.Update; | ||
| public interface IUpdateMusicUseCase | ||
| { | ||
| Task<ResponseProfileMusicJson> Execute(RequestUpdateMusic request); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||||||||||||||||||||||||||
| using MyMusicLibrary.Communication.Request; | ||||||||||||||||||||||||||||||
| using MyMusicLibrary.Communication.Responses; | ||||||||||||||||||||||||||||||
| using MyMusicLibrary.Domain.Repositories.Music; | ||||||||||||||||||||||||||||||
| using MyMusicLibrary.Domain.Repositories.UnitOfWork; | ||||||||||||||||||||||||||||||
| using MyMusicLibrary.Domain.Services.LoggedUser; | ||||||||||||||||||||||||||||||
| using MyMusicLibrary.Domain.Services.Storage.Aws; | ||||||||||||||||||||||||||||||
| using MyMusicLibrary.Exceptions; | ||||||||||||||||||||||||||||||
| using MyMusicLibrary.Exceptions.ExceptionsBase; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| namespace MyMusicLibrary.Application.UseCases.Music.Update; | ||||||||||||||||||||||||||||||
| public class UpdateMusicUseCase : IUpdateMusicUseCase | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| private readonly IMusicWriteOnlyRepository _musicWriteOnlyRepository; | ||||||||||||||||||||||||||||||
| private readonly ILoggedUser _loggedUser; | ||||||||||||||||||||||||||||||
| private readonly IMusicReadOnlyRepository _musicReadOnlyRepository; | ||||||||||||||||||||||||||||||
| private readonly IUnitOfWork _unitOfWork; | ||||||||||||||||||||||||||||||
| private readonly IS3Service _s3Service; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public UpdateMusicUseCase(IMusicWriteOnlyRepository musicWriteOnlyRepository, | ||||||||||||||||||||||||||||||
| ILoggedUser loggedUser, | ||||||||||||||||||||||||||||||
| IMusicReadOnlyRepository musicReadOnlyRepository, | ||||||||||||||||||||||||||||||
| IUnitOfWork unitOfWork, | ||||||||||||||||||||||||||||||
| IS3Service s3Service) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| _musicWriteOnlyRepository = musicWriteOnlyRepository; | ||||||||||||||||||||||||||||||
| _loggedUser = loggedUser; | ||||||||||||||||||||||||||||||
| _musicReadOnlyRepository = musicReadOnlyRepository; | ||||||||||||||||||||||||||||||
| _unitOfWork = unitOfWork; | ||||||||||||||||||||||||||||||
| _s3Service = s3Service; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public async Task<ResponseProfileMusicJson> Execute(RequestUpdateMusic request) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| var user = await _loggedUser.User(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var music = await _musicReadOnlyRepository.GetById(user, request.MusicId); | ||||||||||||||||||||||||||||||
| if (music is null) | ||||||||||||||||||||||||||||||
| throw new NotFoundException(ResourceMessagesException.MUSIC_EMPTY); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var oldKey = music.MusicKey!; | ||||||||||||||||||||||||||||||
| var extension = Path.GetExtension(oldKey); | ||||||||||||||||||||||||||||||
| var folder = Path.GetDirectoryName(oldKey)?.Replace("\\", "/"); | ||||||||||||||||||||||||||||||
| var newKey = string.IsNullOrEmpty(folder) | ||||||||||||||||||||||||||||||
| ? $"{request.Name}{extension}" | ||||||||||||||||||||||||||||||
| : $"{folder}/{request.Name}{extension}"; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| await _s3Service.RenameFileAsync(oldKey, newKey); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| music.Name = request.Name; | ||||||||||||||||||||||||||||||
| music.MusicKey = newKey; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| await _musicWriteOnlyRepository.Update(user, music); | ||||||||||||||||||||||||||||||
| await _unitOfWork.Commit(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+54
|
||||||||||||||||||||||||||||||
| await _s3Service.RenameFileAsync(oldKey, newKey); | |
| music.Name = request.Name; | |
| music.MusicKey = newKey; | |
| await _musicWriteOnlyRepository.Update(user, music); | |
| await _unitOfWork.Commit(); | |
| music.Name = request.Name; | |
| music.MusicKey = newKey; | |
| await _musicWriteOnlyRepository.Update(user, music); | |
| await _unitOfWork.Commit(); | |
| await _s3Service.RenameFileAsync(oldKey, newKey); |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -37,4 +37,16 @@ public async Task UnfavoriteMusic(long musicId) | |||||||||||||
|
|
||||||||||||||
| _dbContext.UserFavoritesMusic.Remove(music!); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| public async Task Update(Domain.Entities.User user, Domain.Entities.Music music) | ||||||||||||||
| { | ||||||||||||||
| var musicUpdate = await _dbContext.Music | ||||||||||||||
| .Where(m => m.Id == music.Id && m.UserId == user.Id) | ||||||||||||||
| .FirstOrDefaultAsync(); | ||||||||||||||
|
|
||||||||||||||
| if (musicUpdate is null) | ||||||||||||||
| return; | ||||||||||||||
|
|
||||||||||||||
|
||||||||||||||
| // Copy updatable properties from music to musicUpdate | |
| musicUpdate.Name = music.Name; | |
| musicUpdate.MusicKey = music.MusicKey; | |
| // Add other properties to update as needed |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -73,4 +73,28 @@ public async Task<S3UrlDto> GetFileUrl(string key) | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return await Task.FromResult(new S3UrlDto(url: url)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public async Task RenameFileAsync(string oldKey, string newKey) | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| // Copia o arquivo existente para o novo nome | ||||||||||||||||||||||||
| var copyRequest = new CopyObjectRequest | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| SourceBucket = bucketName, | ||||||||||||||||||||||||
| SourceKey = oldKey, | ||||||||||||||||||||||||
| DestinationBucket = bucketName, | ||||||||||||||||||||||||
| DestinationKey = newKey | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| await _s3Client.CopyObjectAsync(copyRequest); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Remove o arquivo antigo | ||||||||||||||||||||||||
| var deleteRequest = new DeleteObjectRequest | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| BucketName = bucketName, | ||||||||||||||||||||||||
| Key = oldKey | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| await _s3Client.DeleteObjectAsync(deleteRequest); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| await _s3Client.DeleteObjectAsync(deleteRequest); | |
| try | |
| { | |
| await _s3Client.DeleteObjectAsync(deleteRequest); | |
| } | |
| catch (Exception ex) | |
| { | |
| // Log the error. Replace with your logging framework if available. | |
| Console.WriteLine($"Failed to delete old S3 object '{oldKey}': {ex.Message}"); | |
| // Optionally, rethrow or handle as needed. | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace MyMusicLibrary.Communication.Request; | ||
| public class RequestUpdateMusic | ||
| { | ||
| public long MusicId { get; set; } | ||
| public string Name { get; set; } = string.Empty; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for constructing S3 keys by extracting folder paths and extensions could be fragile. S3 keys are not file system paths and don't have backslashes. The
Path.GetDirectoryNamemethod is designed for file system paths and may not handle S3 keys correctly in all cases. Consider using string manipulation with forward slashes directly or creating a dedicated method to parse S3 keys.