Skip to content

Commit 802ecf8

Browse files
committed
LocalId refactoring
1 parent 6325ac0 commit 802ecf8

5 files changed

Lines changed: 9 additions & 35 deletions

File tree

api/src/main/java/xyz/gianlu/librespot/api/handlers/PlayerHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static void current(HttpServerExchange exchange, @NotNull Player player)
136136
obj.add("episode", ProtobufToJson.convert(metadata.episode));
137137
} else if (id instanceof LocalId) {
138138
JsonObject metadataObj = new JsonObject();
139-
metadataObj.addProperty("name", ((LocalId) id).fileName());
139+
metadataObj.addProperty("name", ((LocalId) id).name());
140140
metadataObj.addProperty("artist", ((LocalId) id).artist());
141141
metadataObj.addProperty("album", ((LocalId) id).album());
142142
metadataObj.addProperty("duration", ((LocalId) id).duration());

lib/src/main/java/xyz/gianlu/librespot/audio/MetadataWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Metadata.ImageGroup getCoverImage() {
9292
public String getName() {
9393
if (track != null) return track.getName();
9494
else if (episode != null) return episode.getName();
95-
else return localTrack.fileName();
95+
else return localTrack.name();
9696
}
9797

9898
/**

lib/src/main/java/xyz/gianlu/librespot/metadata/LocalId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public String album() {
6363
}
6464

6565
@NotNull
66-
public String fileName() {
66+
public String name() {
6767
try {
6868
return URLDecoder.decode(data[2], "UTF-8");
6969
} catch (UnsupportedEncodingException ex) {

player/src/main/java/xyz/gianlu/librespot/player/StateWrapper.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import xyz.gianlu.librespot.player.state.RestrictionsManager.Action;
5656

5757
import java.io.Closeable;
58-
import java.io.File;
5958
import java.io.IOException;
6059
import java.util.*;
6160
import java.util.function.Function;
@@ -128,24 +127,6 @@ private static String generateSessionId(@NotNull Random random) {
128127
return Base64.getEncoder().withoutPadding().encodeToString(bytes);
129128
}
130129

131-
private boolean shouldPlay(@Nullable PlayableId id) {
132-
if (id == null)
133-
return false;
134-
135-
if (id instanceof UnsupportedId)
136-
return false;
137-
138-
if (id instanceof LocalId) {
139-
if (conf.localFilesPath == null)
140-
return false;
141-
142-
File localFile = new File(conf.localFilesPath, ((LocalId) id).fileName()); // FIXME
143-
return localFile.exists() && localFile.canRead();
144-
}
145-
146-
return true;
147-
}
148-
149130
private boolean shouldPlay(@NotNull ContextTrack track) {
150131
if (!track.getMetadataOrDefault("force_remove_reasons", "").isEmpty())
151132
return false;
@@ -154,15 +135,8 @@ private boolean shouldPlay(@NotNull ContextTrack track) {
154135
if (PlayableId.isDelimiter(track.getUri()))
155136
return false;
156137

157-
if (PlayableId.isLocal(track.getUri())) {
158-
if (conf.localFilesPath == null)
159-
return false;
160-
161-
LocalId id = (LocalId) PlayableId.fromUri(track.getUri());
162-
File localFile = new File(conf.localFilesPath, id.fileName()); // FIXME
163-
if (!localFile.exists() || !localFile.canRead())
164-
return false;
165-
}
138+
if (PlayableId.isLocal(track.getUri()))
139+
return false;
166140
}
167141

168142
boolean filterExplicit = "1".equals(session.getUserAttribute("filter-explicit-content"));
@@ -1129,7 +1103,7 @@ synchronized void initializeStart() throws IOException, MercuryClient.MercuryExc
11291103
else state.getOptionsBuilder().setShufflingContext(false); // Must do this directly!
11301104

11311105
setCurrentTrackIndex(0);
1132-
if (!shouldPlay(getCurrentPlayable())) {
1106+
if (!shouldPlay(tracks.get(getCurrentTrackIndex()))) {
11331107
LOGGER.debug("Cannot play currently selected track, skipping: {}", getCurrentPlayable());
11341108

11351109
boolean repeatTrack = isRepeatingTrack();
@@ -1182,7 +1156,7 @@ synchronized void initializeFrom(@NotNull Function<List<ContextTrack>, Integer>
11821156
LOGGER.warn("Failed updating current track metadata.", ex);
11831157
}
11841158

1185-
if (!shouldPlay(getCurrentPlayable())) {
1159+
if (!shouldPlay(tracks.get(getCurrentTrackIndex()))) {
11861160
LOGGER.debug("Cannot play currently selected track, skipping: {}", getCurrentPlayable());
11871161

11881162
boolean repeatTrack = isRepeatingTrack();

player/src/main/java/xyz/gianlu/librespot/player/playback/PlayerQueueEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void load(boolean preload) throws IOException, Codec.CodecException, Mer
110110
PlayableContentFeeder.LoadedStream stream;
111111
if (playable instanceof LocalId)
112112
stream = PlayableContentFeeder.LoadedStream.forLocalFile((LocalId) playable,
113-
new File(conf.localFilesPath, ((LocalId) playable).fileName()));
113+
new File(conf.localFilesPath, ((LocalId) playable).name()));
114114
else
115115
stream = session.contentFeeder().load(playable, new VorbisOnlyAudioQuality(conf.preferredQuality), preload, this);
116116

@@ -124,7 +124,7 @@ private void load(boolean preload) throws IOException, Codec.CodecException, Mer
124124
LOGGER.info("Loaded track. {name: '{}', artists: '{}', duration: {}, uri: {}, id: {}}", metadata.track.getName(),
125125
Utils.artistsToString(metadata.track.getArtistList()), metadata.track.getDuration(), playable.toSpotifyUri(), playbackId);
126126
} else if (playable instanceof LocalId) {
127-
LOGGER.info("Loaded local file. {filename: '{}', duration: {}, uri: {}, id: {}}", ((LocalId) playable).fileName(),
127+
LOGGER.info("Loaded local file. {filename: '{}', duration: {}, uri: {}, id: {}}", ((LocalId) playable).name(),
128128
((LocalId) playable).duration(), playable.toSpotifyUri(), playbackId);
129129
}
130130

0 commit comments

Comments
 (0)