Skip to content

Commit 1625d48

Browse files
committed
Bump version to 1.4.7 and centralize track ID validation logic
1 parent 5ea53ff commit 1625d48

File tree

6 files changed

+50
-23
lines changed

6 files changed

+50
-23
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'de.labystudio'
7-
version '1.4.6'
7+
version '1.4.7'
88

99
compileJava {
1010
sourceCompatibility = '1.8'

src/main/java/de/labystudio/spotifyapi/model/Track.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
public class Track {
1111

12+
public static final int ID_LENGTH = 22;
13+
1214
private final String id;
1315
private final String name;
1416
private final String artist;
@@ -31,6 +33,10 @@ public Track(
3133
this.coverArt = coverArt;
3234
}
3335

36+
public boolean isIdValid() {
37+
return isTrackIdValid(this.id);
38+
}
39+
3440
public String getId() {
3541
return this.id;
3642
}
@@ -65,4 +71,28 @@ public int hashCode() {
6571
public String toString() {
6672
return String.format("[%s] %s - %s", this.id, this.name, this.artist);
6773
}
74+
75+
/**
76+
* Checks if the given track ID is valid.
77+
* A track ID is valid if there are no characters with a value of zero.
78+
* It also has to be exactly 22 characters long.
79+
*
80+
* @param trackId The track ID to check.
81+
* @return True if the track ID is valid, false otherwise.
82+
*/
83+
public static boolean isTrackIdValid(String trackId) {
84+
if (trackId == null) {
85+
return false;
86+
}
87+
88+
for (char c : trackId.toCharArray()) {
89+
boolean isValidCharacter = c >= 'a' && c <= 'z'
90+
|| c >= 'A' && c <= 'Z'
91+
|| c >= '0' && c <= '9';
92+
if (!isValidCharacter) {
93+
return false;
94+
}
95+
}
96+
return !trackId.contains(" ") && trackId.length() == ID_LENGTH;
97+
}
6898
}

src/main/java/de/labystudio/spotifyapi/open/Cache.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public void setCacheSize(int cacheSize) {
4242
* @param key The key of the entry
4343
*/
4444
public void push(String key, T value) {
45+
if (key == null) {
46+
throw new IllegalArgumentException("Key cannot be null");
47+
}
48+
if (value == null) {
49+
throw new IllegalArgumentException("Value cannot be null");
50+
}
51+
4552
// Remove entry from cache if cache is full
4653
if (this.cacheQueue.size() > this.cacheSize) {
4754
String urlToRemove = this.cacheQueue.remove(0);

src/main/java/de/labystudio/spotifyapi/open/OpenSpotifyAPI.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public void requestImageAsync(Track track, Consumer<BufferedImage> callback) {
166166
* @param callback Response with the buffered image track. It won't be called on an error.
167167
*/
168168
public void requestImageAsync(String trackId, Consumer<BufferedImage> callback) {
169+
if (!Track.isTrackIdValid(trackId)) {
170+
throw new IllegalArgumentException("Invalid track ID: " + trackId);
171+
}
172+
169173
this.executor.execute(() -> {
170174
try {
171175
BufferedImage image = this.requestImage(trackId);
@@ -250,6 +254,10 @@ public BufferedImage requestImage(Track track) throws IOException {
250254
* @throws IOException if the request failed
251255
*/
252256
public BufferedImage requestImage(String trackId) throws IOException {
257+
if (!Track.isTrackIdValid(trackId)) {
258+
throw new IllegalArgumentException("Invalid track ID: " + trackId);
259+
}
260+
253261
// Try to get image from cache by track id
254262
BufferedImage cachedImage = this.imageCache.get(trackId);
255263
if (cachedImage != null) {

src/main/java/de/labystudio/spotifyapi/platform/windows/WinSpotifyAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected void onTick() {
9595

9696
// Read track id and check if track id is valid
9797
String trackId = this.process.readTrackId();
98-
if (!this.process.isTrackIdValid(trackId)) {
98+
if (!Track.isTrackIdValid(trackId)) {
9999
throw new IllegalStateException("Invalid track ID: " + trackId);
100100
}
101101

src/main/java/de/labystudio/spotifyapi/platform/windows/api/spotify/SpotifyProcess.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.labystudio.spotifyapi.platform.windows.api.spotify;
22

3+
import de.labystudio.spotifyapi.model.Track;
34
import de.labystudio.spotifyapi.platform.windows.api.WinProcess;
45
import de.labystudio.spotifyapi.platform.windows.api.jna.Psapi;
56
import de.labystudio.spotifyapi.platform.windows.api.jna.WindowsMediaControl;
@@ -91,7 +92,7 @@ private long findTrackIdAddress() {
9192

9293
// Check if the hardcoded offset is valid
9394
long targetAddressTrackId = chromeElfAddress + trackIdOffset;
94-
if (this.isTrackIdValid(this.readTrackId(targetAddressTrackId))) {
95+
if (Track.isTrackIdValid(this.readTrackId(targetAddressTrackId))) {
9596
// If the offset works, exit the loop
9697
addressTrackId = targetAddressTrackId;
9798
break;
@@ -108,7 +109,7 @@ private long findTrackIdAddress() {
108109
long scanAddressFrom = chromeElfAddress + minTrackIdOffset - threshold;
109110
long scanAddressTo = chromeElfAddress + maxTrackIdOffset + threshold;
110111
addressTrackId = this.findAddressOfText(scanAddressFrom, scanAddressTo, PREFIX_SPOTIFY_TRACK, (address, index) -> {
111-
return this.isTrackIdValid(this.readTrackId(address));
112+
return Track.isTrackIdValid(this.readTrackId(address));
112113
});
113114
}
114115

@@ -150,23 +151,4 @@ public String readTrackId() {
150151
public PlaybackAccessor getPlaybackAccessor() {
151152
return this.playbackAccessor;
152153
}
153-
154-
/**
155-
* Checks if the given track ID is valid.
156-
* A track ID is valid if there are no characters with a value of zero.
157-
*
158-
* @param trackId The track ID to check.
159-
* @return True if the track ID is valid, false otherwise.
160-
*/
161-
public boolean isTrackIdValid(String trackId) {
162-
for (char c : trackId.toCharArray()) {
163-
boolean isValidCharacter = c >= 'a' && c <= 'z'
164-
|| c >= 'A' && c <= 'Z'
165-
|| c >= '0' && c <= '9';
166-
if (!isValidCharacter) {
167-
return false;
168-
}
169-
}
170-
return true;
171-
}
172154
}

0 commit comments

Comments
 (0)