From 20217223095bbbedde529d83a78aed9f3fade3a3 Mon Sep 17 00:00:00 2001 From: kor-Chipmunk Date: Thu, 15 Feb 2024 16:30:00 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=8C=EC=9B=90=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EC=B6=94=EC=B6=9C=20=EB=AA=BB=ED=95=A0=20=EC=8B=9C=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9D=B4=EB=A6=84=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=B6=94=EC=B6=9C=ED=95=98=EB=8F=84=EB=A1=9D=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../music/repository/AlbumRepository.java | 5 +- .../music/repository/ArtistRepository.java | 5 +- .../lalala/music/service/AlbumService.java | 14 +++- .../lalala/music/service/ArtistService.java | 20 +++-- .../music/service/MusicExtractorService.java | 82 +++++++++++++++++-- 5 files changed, 107 insertions(+), 19 deletions(-) diff --git a/src/backend/music-server/music-api/src/main/java/com/lalala/music/repository/AlbumRepository.java b/src/backend/music-server/music-api/src/main/java/com/lalala/music/repository/AlbumRepository.java index ccba6b70..9796f35c 100644 --- a/src/backend/music-server/music-api/src/main/java/com/lalala/music/repository/AlbumRepository.java +++ b/src/backend/music-server/music-api/src/main/java/com/lalala/music/repository/AlbumRepository.java @@ -1,7 +1,10 @@ package com.lalala.music.repository; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import com.lalala.music.entity.AlbumEntity; -public interface AlbumRepository extends JpaRepository {} +public interface AlbumRepository extends JpaRepository { + Optional findByTitleAndArtistId(String title, Long artistId); +} diff --git a/src/backend/music-server/music-api/src/main/java/com/lalala/music/repository/ArtistRepository.java b/src/backend/music-server/music-api/src/main/java/com/lalala/music/repository/ArtistRepository.java index 03fd10c1..73dff38c 100644 --- a/src/backend/music-server/music-api/src/main/java/com/lalala/music/repository/ArtistRepository.java +++ b/src/backend/music-server/music-api/src/main/java/com/lalala/music/repository/ArtistRepository.java @@ -1,7 +1,10 @@ package com.lalala.music.repository; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import com.lalala.music.entity.ArtistEntity; -public interface ArtistRepository extends JpaRepository {} +public interface ArtistRepository extends JpaRepository { + Optional findByNameAndAgency(String name, String agency); +} diff --git a/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/AlbumService.java b/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/AlbumService.java index c8acc263..ff1fd991 100644 --- a/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/AlbumService.java +++ b/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/AlbumService.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.function.Function; import java.util.stream.Collectors; @@ -39,13 +40,18 @@ public class AlbumService { public AlbumDetailDTO createAlbum(CreateAlbumRequestDTO request) { ArtistEntity artist = ArtistUtils.findById(request.getArtistId(), artistRepository); - AlbumEntity album = + Optional album = repository.findByTitleAndArtistId(request.getTitle(), artist.getId()); + if (album.isPresent()) { + return AlbumDetailDTO.from(album.get(), artist); + } + + AlbumEntity newAlbum = new AlbumEntity( request.getTitle(), request.getCoverUrl(), request.getType(), request.getReleasedAt()); - album.updateArtist(request.getArtistId()); - album = repository.save(album); + newAlbum.updateArtist(request.getArtistId()); + newAlbum = repository.save(newAlbum); - return AlbumDetailDTO.from(album, artist); + return AlbumDetailDTO.from(newAlbum, artist); } public List getAlbums(int page, int pageSize) { diff --git a/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/ArtistService.java b/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/ArtistService.java index bee6c7c1..5693c057 100644 --- a/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/ArtistService.java +++ b/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/ArtistService.java @@ -2,6 +2,7 @@ import java.util.List; +import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -32,11 +33,20 @@ public class ArtistService { @Transactional public ArtistDTO createArtist(CreateArtistRequestDTO request) { - ArtistEntity artist = - new ArtistEntity( - request.getName(), request.getGender(), request.getType(), request.getAgency()); - artist = repository.save(artist); - return ArtistDTO.from(artist); + Optional artist = repository.findByNameAndAgency(request.getName(), request.getAgency()); + + if (artist.isPresent()) { + return ArtistDTO.from(artist.get()); + } + + ArtistEntity newArtist = new ArtistEntity( + request.getName(), + request.getGender(), + request.getType(), + request.getAgency() + ); + newArtist = repository.save(newArtist); + return ArtistDTO.from(newArtist); } public List getArtists(int page, int pageSize) { diff --git a/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/MusicExtractorService.java b/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/MusicExtractorService.java index 7baf59fd..e2660325 100644 --- a/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/MusicExtractorService.java +++ b/src/backend/music-server/music-api/src/main/java/com/lalala/music/service/MusicExtractorService.java @@ -1,5 +1,7 @@ package com.lalala.music.service; +import com.lalala.exception.BusinessException; +import com.lalala.exception.ErrorCode; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; @@ -17,8 +19,6 @@ import org.springframework.web.multipart.MultipartFile; import com.lalala.music.dto.ExtractedMusicDTO; -import com.lalala.music.exception.BusinessException; -import com.lalala.music.exception.ErrorCode; import com.mpatric.mp3agic.ID3v1; import com.mpatric.mp3agic.ID3v2; import com.mpatric.mp3agic.InvalidDataException; @@ -73,16 +73,43 @@ public ExtractedMusicDTO extract(File file) { } else if (mp3file.hasId3v2Tag()) { ID3v2 id3v2Tag = mp3file.getId3v2Tag(); + String fileName = getNameWithoutExtension(file.getName()); + String title = id3v2Tag.getTitle(); + if (title == null || title.contains(" - ")) { + title = getTitleFromFileName(fileName); + } + + String artist = id3v2Tag.getArtist(); + if (artist == null) { + artist = getArtistFromFileName(fileName); + } + + String album = id3v2Tag.getAlbum(); + if (album == null) { + album = "Various Album"; + } + + String lyrics = id3v2Tag.getLyrics(); + if (lyrics == null) { + lyrics = ""; + } + + String extension = null; + byte[] albumImage = null; + String mimeType = id3v2Tag.getAlbumImageMimeType(); - String extension = mimeType.split("/")[1]; + if (mimeType != null) { + extension = mimeType.split("/")[1]; + albumImage = id3v2Tag.getAlbumImage(); + } return new ExtractedMusicDTO( - id3v2Tag.getTitle(), - id3v2Tag.getArtist(), - id3v2Tag.getAlbum(), - id3v2Tag.getLyrics() == null ? "" : id3v2Tag.getLyrics(), + title, + artist, + album, + lyrics, mp3file.getLengthInSeconds(), - id3v2Tag.getAlbumImage(), + albumImage, extension); } else { throw new BusinessException(ErrorCode.INVALID_MP3_TAG); @@ -92,4 +119,43 @@ public ExtractedMusicDTO extract(File file) { throw new BusinessException(ErrorCode.INVALID_MP3_TAG); } } + + private String getNameWithoutExtension(String fileNameWithExtension) { + int dotIndex = fileNameWithExtension.lastIndexOf('.'); + return (dotIndex == -1) ? fileNameWithExtension : fileNameWithExtension.substring(0, dotIndex); + } + + private String getTitleFromFileName(String fileName) { + if (fileName.lastIndexOf('-') == -1) { + return fileName; + } + + int hyphenIndex = fileName.lastIndexOf(" - ") + 1; + String subStringToHyphen = fileName.substring(hyphenIndex + 1, fileName.length()).trim(); + + int featuringIndex = subStringToHyphen.lastIndexOf("."); + if (featuringIndex != -1) { + int lastSpaceIndex = subStringToHyphen.substring(0, featuringIndex).lastIndexOf(" "); + return subStringToHyphen.substring(0, lastSpaceIndex).trim(); + } + + return subStringToHyphen; + } + + private String getArtistFromFileName(String fileName) { + if (fileName.lastIndexOf('-') == -1) { + return "Various Artist"; + } + + int hyphenIndex = fileName.lastIndexOf(" - ") + 1; + String subStringToHyphen = fileName.substring(0, hyphenIndex).trim(); + + int featuringIndex = subStringToHyphen.lastIndexOf("."); + if (featuringIndex != -1) { + int lastSpaceIndex = subStringToHyphen.substring(0, featuringIndex).lastIndexOf(" "); + return subStringToHyphen.substring(0, lastSpaceIndex).trim(); + } + + return subStringToHyphen; + } }