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:
parent
6e8f49e5b5
commit
c5ac14fed1
@ -137,7 +137,10 @@ public class HibernateDatabase implements IDatabase{
|
|||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
Optional<Album> albumObj = getOneAlbum(metadata.getAlbum());
|
Optional<Album> albumObj = getOneAlbum(metadata.getAlbum());
|
||||||
if (!albumObj.isPresent()) {
|
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);
|
albumObj = Optional.of(album);
|
||||||
}
|
}
|
||||||
Optional<Artist> artistObj = getOneArtist(metadata.getArtist());
|
Optional<Artist> artistObj = getOneArtist(metadata.getArtist());
|
||||||
@ -153,7 +156,7 @@ public class HibernateDatabase implements IDatabase{
|
|||||||
Song song = songObj.get();
|
Song song = songObj.get();
|
||||||
song.updateData(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), artistObj.get(),
|
song.updateData(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), artistObj.get(),
|
||||||
albumObj.get(), metadata.getGenre(), metadata.getSongFile());
|
albumObj.get(), metadata.getGenre(), metadata.getSongFile());
|
||||||
session.update(song);
|
session.merge(song);
|
||||||
}
|
}
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
@ -95,20 +95,6 @@ public class Song implements Comparable<Song>, IDBType {
|
|||||||
return result;
|
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() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,11 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.DirectoryStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public final class AlbumArtExtractor {
|
public final class AlbumArtExtractor {
|
||||||
@ -29,7 +32,7 @@ public final class AlbumArtExtractor {
|
|||||||
* @return Album art as a byte array.
|
* @return Album art as a byte array.
|
||||||
*/
|
*/
|
||||||
public static byte[] getImageData(File songFile){
|
public static byte[] getImageData(File songFile){
|
||||||
Tag audioTags = null;
|
Tag audioTags;
|
||||||
try {
|
try {
|
||||||
audioTags = AudioFileIO.read(songFile).getTag();
|
audioTags = AudioFileIO.read(songFile).getTag();
|
||||||
} catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException e) {
|
} catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException e) {
|
||||||
@ -81,4 +84,18 @@ public final class AlbumArtExtractor {
|
|||||||
g.dispose();
|
g.dispose();
|
||||||
return newImage;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,6 +184,7 @@ public class HibernateDatabaseTest {
|
|||||||
assertEquals(newMetadata.getTitle(), song.getTitle());
|
assertEquals(newMetadata.getTitle(), song.getTitle());
|
||||||
assertEquals(newMetadata.getGenre(), song.getGenre());
|
assertEquals(newMetadata.getGenre(), song.getGenre());
|
||||||
assertEquals(Integer.valueOf(200), song.getDiscNumber());
|
assertEquals(Integer.valueOf(200), song.getDiscNumber());
|
||||||
|
assertEquals(1, database.listAllT(Song.class).get().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user