Fixed duplication of existing songs when updating, added checking of directory for album art if none is contained in tags.

This commit is contained in:
neviyn 2016-03-22 19:16:53 +00:00
parent 6e8f49e5b5
commit c5ac14fed1
4 changed files with 24 additions and 17 deletions

View File

@ -137,7 +137,10 @@ public class HibernateDatabase implements IDatabase{
session.beginTransaction();
Optional<Album> albumObj = getOneAlbum(metadata.getAlbum());
if (!albumObj.isPresent()) {
Album album = new Album(metadata.getAlbum(), AlbumArtExtractor.getImageData(new File(metadata.getSongFile())));
byte[] art = AlbumArtExtractor.getImageData(new File(metadata.getSongFile()));
if(art == null)
AlbumArtExtractor.getImageDataFromFolder(metadata.getSongFile());
Album album = new Album(metadata.getAlbum(), art);
albumObj = Optional.of(album);
}
Optional<Artist> artistObj = getOneArtist(metadata.getArtist());
@ -153,7 +156,7 @@ public class HibernateDatabase implements IDatabase{
Song song = songObj.get();
song.updateData(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), artistObj.get(),
albumObj.get(), metadata.getGenre(), metadata.getSongFile());
session.update(song);
session.merge(song);
}
session.getTransaction().commit();
}

View File

@ -95,20 +95,6 @@ public class Song implements Comparable<Song>, IDBType {
return result;
}
/**
* Try to find album art for this song based on likely image file names in the folder.
*
* @return BufferedImage of album art or Optional.empty()
*/
public Optional<BufferedImage> getAlbumArt() {
Path dir = Paths.get(songFile).getParent();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{jpg,png}")) {
return Optional.of(ImageIO.read(stream.iterator().next().toFile()));
} catch (IOException | NoSuchElementException ignored) {
return Optional.empty();
}
}
public long getId() {
return id;
}

View File

@ -14,8 +14,11 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.NoSuchElementException;
import java.util.Optional;
public final class AlbumArtExtractor {
@ -29,7 +32,7 @@ public final class AlbumArtExtractor {
* @return Album art as a byte array.
*/
public static byte[] getImageData(File songFile){
Tag audioTags = null;
Tag audioTags;
try {
audioTags = AudioFileIO.read(songFile).getTag();
} catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException e) {
@ -81,4 +84,18 @@ public final class AlbumArtExtractor {
g.dispose();
return newImage;
}
/**
* Try to find album art for this song based on likely image file names in the folder.
*
* @return BufferedImage of album art or Optional.empty()
*/
public static byte[] getImageDataFromFolder(String songFile) {
Path dir = Paths.get(songFile).getParent();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{jpg,png}")) {
return convertImageToBytes(ImageIO.read(stream.iterator().next().toFile()));
} catch (IOException | NoSuchElementException ignored) {
return null;
}
}
}

View File

@ -184,6 +184,7 @@ public class HibernateDatabaseTest {
assertEquals(newMetadata.getTitle(), song.getTitle());
assertEquals(newMetadata.getGenre(), song.getGenre());
assertEquals(Integer.valueOf(200), song.getDiscNumber());
assertEquals(1, database.listAllT(Song.class).get().size());
}
@Test