Refined playlist loading:

- Removed intermediary collection.
- Filtered non-path metadata lines.
This commit is contained in:
neviyn 2016-12-03 19:09:11 +00:00
parent 53e4f47029
commit 49055daf4b
2 changed files with 26 additions and 10 deletions

View File

@ -77,7 +77,8 @@ public class ExtractedMetadata {
Tag audioTags = null;
try {
audioTags = AudioFileIO.read(targetFile.toFile()).getTag();
} catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException ignored) {
} catch (CannotReadException | IOException | ReadOnlyFileException | TagException |
InvalidAudioFrameException | NullPointerException ignored) {
}
return audioTags == null ? Optional.empty() : Optional.of(new ExtractedMetadata(audioTags, targetFile.toFile()));
}

View File

@ -131,25 +131,40 @@ public interface IPlaylist {
* @param targetFile File to read m3u playlist data from.
* @return List of songs contained in the playlist.
*/
static List<Song> readPlaylistFromFile(File targetFile){
static List<Song> readPlaylistFromFile(final File targetFile){
List<Song> result = new ArrayList<>();
if(targetFile.exists()){
try {
List<String> playlistData = Files.lines(targetFile.toPath()).filter(x -> !x.isEmpty() && !x.contains("#EXTINF")).collect(Collectors.toList());
if(playlistData.get(0).equals("#EXTM3U")){
playlistData.remove(0);
List<Path> songPaths = playlistData.stream().map(Paths::get).collect(Collectors.toList());
for(int i = 0; i < songPaths.size(); i++){
if(!Files.exists(songPaths.get(i))){ // If song file doesn't exists, try treating it as relative
songPaths.set(i, Paths.get(targetFile.getParent(), songPaths.get(i).toString()));
}
}
songPaths.stream().map(ExtractedMetadata::autoParse).filter(Optional::isPresent).map(Optional::get)
.forEach(x -> result.add(new Song(x.getTrackNumber(), x.getDiscNumber(), x.getTitle(), new Artist(x.getArtist()), new Album(x.getAlbum()), x.getGenre(), x.getSongFile())));
playlistData.stream().filter(x -> !x.startsWith("#")).map(Paths::get).map(x -> checkSongPath(x, targetFile)).map(ExtractedMetadata::autoParse)
.filter(Optional::isPresent).map(Optional::get).forEach(x ->
result.add(new Song(x.getTrackNumber(), x.getDiscNumber(), x.getTitle(),
new Artist(x.getArtist()), new Album(x.getAlbum()), x.getGenre(), x.getSongFile())));
}
} catch (IOException ignored) { }
}
return result;
}
/**
* Check whether a Path points to a valid file, if not try again under the assumption that the Path is instead
* relative instead of absolute. If neither then null is returned.
* @param targetFile Path to check if points to a valid file.
* @param playlistFile Playlist file being read (used to potentially transform paths to relative paths).
* @return A Path to a valid file or null.
*/
static Path checkSongPath(final Path targetFile, final File playlistFile){
if(Files.exists(targetFile)){
return targetFile;
}
else{
Path alternateFile = Paths.get(playlistFile.getParent(), targetFile.toString());
if(Files.exists(alternateFile))
return alternateFile;
}
return null;
}
}