Skip to content

Commit

Permalink
feat: 음원 정보 추출 못할 시 파일 이름으로 추출하도록 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kor-Chipmunk committed Feb 15, 2024
1 parent 8310681 commit 2021722
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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<AlbumEntity, Long> {}
public interface AlbumRepository extends JpaRepository<AlbumEntity, Long> {
Optional<AlbumEntity> findByTitleAndArtistId(String title, Long artistId);
}
Original file line number Diff line number Diff line change
@@ -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<ArtistEntity, Long> {}
public interface ArtistRepository extends JpaRepository<ArtistEntity, Long> {
Optional<ArtistEntity> findByNameAndAgency(String name, String agency);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -39,13 +40,18 @@ public class AlbumService {
public AlbumDetailDTO createAlbum(CreateAlbumRequestDTO request) {
ArtistEntity artist = ArtistUtils.findById(request.getArtistId(), artistRepository);

AlbumEntity album =
Optional<AlbumEntity> 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<AlbumDTO> getAlbums(int page, int pageSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import java.util.Optional;
import lombok.RequiredArgsConstructor;

import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -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<ArtistEntity> 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<ArtistDTO> getArtists(int page, int pageSize) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}

0 comments on commit 2021722

Please sign in to comment.