From c5ac14fed173fec87b99deef1ec4d89c4f7a016c Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 22 Mar 2016 19:16:53 +0000 Subject: [PATCH] Fixed duplication of existing songs when updating, added checking of directory for album art if none is contained in tags. --- .../musicplayer/db/HibernateDatabase.java | 7 +++++-- src/main/java/musicplayer/model/Song.java | 14 -------------- .../musicplayer/util/AlbumArtExtractor.java | 19 ++++++++++++++++++- .../musicplayer/db/HibernateDatabaseTest.java | 1 + 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/main/java/musicplayer/db/HibernateDatabase.java b/src/main/java/musicplayer/db/HibernateDatabase.java index aaab4bd..d14b672 100644 --- a/src/main/java/musicplayer/db/HibernateDatabase.java +++ b/src/main/java/musicplayer/db/HibernateDatabase.java @@ -137,7 +137,10 @@ public class HibernateDatabase implements IDatabase{ session.beginTransaction(); Optional 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 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(); } diff --git a/src/main/java/musicplayer/model/Song.java b/src/main/java/musicplayer/model/Song.java index 8810ccf..0610c4f 100644 --- a/src/main/java/musicplayer/model/Song.java +++ b/src/main/java/musicplayer/model/Song.java @@ -95,20 +95,6 @@ public class Song implements Comparable, 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 getAlbumArt() { - Path dir = Paths.get(songFile).getParent(); - try (DirectoryStream 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; } diff --git a/src/main/java/musicplayer/util/AlbumArtExtractor.java b/src/main/java/musicplayer/util/AlbumArtExtractor.java index 7758e01..378ea7d 100644 --- a/src/main/java/musicplayer/util/AlbumArtExtractor.java +++ b/src/main/java/musicplayer/util/AlbumArtExtractor.java @@ -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 stream = Files.newDirectoryStream(dir, "*.{jpg,png}")) { + return convertImageToBytes(ImageIO.read(stream.iterator().next().toFile())); + } catch (IOException | NoSuchElementException ignored) { + return null; + } + } } diff --git a/src/test/java/musicplayer/db/HibernateDatabaseTest.java b/src/test/java/musicplayer/db/HibernateDatabaseTest.java index ebe743d..0edf950 100644 --- a/src/test/java/musicplayer/db/HibernateDatabaseTest.java +++ b/src/test/java/musicplayer/db/HibernateDatabaseTest.java @@ -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