Refined playlist loading:
- Removed intermediary collection. - Filtered non-path metadata lines.
This commit is contained in:
parent
53e4f47029
commit
49055daf4b
@ -77,7 +77,8 @@ public class ExtractedMetadata {
|
|||||||
Tag audioTags = null;
|
Tag audioTags = null;
|
||||||
try {
|
try {
|
||||||
audioTags = AudioFileIO.read(targetFile.toFile()).getTag();
|
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()));
|
return audioTags == null ? Optional.empty() : Optional.of(new ExtractedMetadata(audioTags, targetFile.toFile()));
|
||||||
}
|
}
|
||||||
|
@ -131,25 +131,40 @@ public interface IPlaylist {
|
|||||||
* @param targetFile File to read m3u playlist data from.
|
* @param targetFile File to read m3u playlist data from.
|
||||||
* @return List of songs contained in the playlist.
|
* @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<>();
|
List<Song> result = new ArrayList<>();
|
||||||
if(targetFile.exists()){
|
if(targetFile.exists()){
|
||||||
try {
|
try {
|
||||||
List<String> playlistData = Files.lines(targetFile.toPath()).filter(x -> !x.isEmpty() && !x.contains("#EXTINF")).collect(Collectors.toList());
|
List<String> playlistData = Files.lines(targetFile.toPath()).filter(x -> !x.isEmpty() && !x.contains("#EXTINF")).collect(Collectors.toList());
|
||||||
if(playlistData.get(0).equals("#EXTM3U")){
|
if(playlistData.get(0).equals("#EXTM3U")){
|
||||||
playlistData.remove(0);
|
playlistData.remove(0);
|
||||||
List<Path> songPaths = playlistData.stream().map(Paths::get).collect(Collectors.toList());
|
playlistData.stream().filter(x -> !x.startsWith("#")).map(Paths::get).map(x -> checkSongPath(x, targetFile)).map(ExtractedMetadata::autoParse)
|
||||||
for(int i = 0; i < songPaths.size(); i++){
|
.filter(Optional::isPresent).map(Optional::get).forEach(x ->
|
||||||
if(!Files.exists(songPaths.get(i))){ // If song file doesn't exists, try treating it as relative
|
result.add(new Song(x.getTrackNumber(), x.getDiscNumber(), x.getTitle(),
|
||||||
songPaths.set(i, Paths.get(targetFile.getParent(), songPaths.get(i).toString()));
|
new Artist(x.getArtist()), new Album(x.getAlbum()), x.getGenre(), x.getSongFile())));
|
||||||
}
|
|
||||||
}
|
|
||||||
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())));
|
|
||||||
}
|
}
|
||||||
} catch (IOException ignored) { }
|
} catch (IOException ignored) { }
|
||||||
}
|
}
|
||||||
return result;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user