Skip to content

Commit

Permalink
Merge pull request #515 from kagemomiji/issue505-change-cover-arts-ur…
Browse files Browse the repository at this point in the history
…l-without-guest-user

#505 Update artist image URLs with username parameter
  • Loading branch information
kagemomiji authored Jun 24, 2024
2 parents 517346e + cebdf37 commit 123c884
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,9 @@ public void getArtistInfo(HttpServletRequest request, HttpServletResponse respon
}
// extract base url
String baseUrl = NetworkUtil.getBaseUrl(request);
result.setSmallImageUrl(artistService.getArtistImageUrlByMediaFile(baseUrl, mediaFile, 34));
result.setMediumImageUrl(artistService.getArtistImageUrlByMediaFile(baseUrl, mediaFile, 64));
result.setLargeImageUrl(artistService.getArtistImageUrlByMediaFile(baseUrl, mediaFile, 300));
result.setSmallImageUrl(artistService.getArtistImageUrlByMediaFile(baseUrl, mediaFile, 34, username));
result.setMediumImageUrl(artistService.getArtistImageUrlByMediaFile(baseUrl, mediaFile, 64, username));
result.setLargeImageUrl(artistService.getArtistImageUrlByMediaFile(baseUrl, mediaFile, 300, username));

Response res = createResponse();
res.setArtistInfo(result);
Expand Down Expand Up @@ -497,9 +497,9 @@ public void getArtistInfo2(HttpServletRequest request, HttpServletResponse respo
result.setLastFmUrl(artistBio.getLastFmUrl());
}
String baseUrl = NetworkUtil.getBaseUrl(request);
result.setSmallImageUrl(artistService.getArtistImageURL(baseUrl, artist.getName(), 34));
result.setMediumImageUrl(artistService.getArtistImageURL(baseUrl, artist.getName(), 64));
result.setLargeImageUrl(artistService.getArtistImageURL(baseUrl, artist.getName(), 300));
result.setSmallImageUrl(artistService.getArtistImageURL(baseUrl, artist.getName(), 34, username));
result.setMediumImageUrl(artistService.getArtistImageURL(baseUrl, artist.getName(), 64, username));
result.setLargeImageUrl(artistService.getArtistImageURL(baseUrl, artist.getName(), 300, username));
Response res = createResponse();
res.setArtistInfo2(result);
jaxbWriter.writeResponse(request, response, res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.airsonic.player.domain.Artist;
import org.airsonic.player.domain.MediaFile;
import org.airsonic.player.domain.MusicFolder;
import org.airsonic.player.domain.User;
import org.airsonic.player.domain.entity.StarredArtist;
import org.airsonic.player.repository.ArtistRepository;
import org.airsonic.player.repository.OffsetBasedPageRequest;
Expand Down Expand Up @@ -52,16 +51,13 @@ public class ArtistService {

private final JWTSecurityService jwtSecurityService;

private final SecurityService securityService;

private final MediaFileService mediaFileService;

public ArtistService(ArtistRepository artistRepository, StarredArtistRepository starredArtistRepository,
JWTSecurityService jwtSecurityService, SecurityService securityService, MediaFileService mediaFileService) {
JWTSecurityService jwtSecurityService, MediaFileService mediaFileService) {
this.artistRepository = artistRepository;
this.starredArtistRepository = starredArtistRepository;
this.jwtSecurityService = jwtSecurityService;
this.securityService = securityService;
this.mediaFileService = mediaFileService;
}

Expand Down Expand Up @@ -235,7 +231,7 @@ public Artist save(Artist artist) {
*/
@Nullable
@Transactional
public String getArtistImageURL(@Nonnull String baseUrl, @Nullable String artistName, int size) {
public String getArtistImageURL(@Nonnull String baseUrl, @Nullable String artistName, int size, String username) {
if (!StringUtils.hasLength(artistName)) {
LOG.debug("getArtistImageURL: artistName is null");
return null;
Expand All @@ -244,9 +240,8 @@ public String getArtistImageURL(@Nonnull String baseUrl, @Nullable String artist
// expire in 5 minutes
Instant expires = Instant.now().plusSeconds(300);
return artistRepository.findByName(artistName).map(artist -> {
securityService.createGuestUserIfNotExists();
return baseUrl + jwtSecurityService.addJWTToken(
User.USERNAME_GUEST,
username,
UriComponentsBuilder
.fromUriString(prefix + "/coverArt.view")
.queryParam("id", String.format("ar-%d", artist.getId()))
Expand All @@ -259,7 +254,7 @@ public String getArtistImageURL(@Nonnull String baseUrl, @Nullable String artist

@Nullable
@Transactional
public String getArtistImageUrlByMediaFile(@Nonnull String baseUrl, @Nullable MediaFile mediaFile, int size) {
public String getArtistImageUrlByMediaFile(@Nonnull String baseUrl, @Nullable MediaFile mediaFile, int size, @Nonnull String username) {

if (mediaFile == null) {
LOG.debug("getArtistImageUrlByMediaFile: mediaFile is null");
Expand All @@ -272,8 +267,8 @@ public String getArtistImageUrlByMediaFile(@Nonnull String baseUrl, @Nullable Me
// artist
if (mediaFile.isAudio() || mediaFile.isAlbum()) {
// get artist image url by album artist or artist
String url = Optional.ofNullable(getArtistImageURL(baseUrl, mediaFile.getAlbumArtist(), size))
.orElse(getArtistImageURL(baseUrl, mediaFile.getArtist(), size));
String url = Optional.ofNullable(getArtistImageURL(baseUrl, mediaFile.getAlbumArtist(), size, username))
.orElse(getArtistImageURL(baseUrl, mediaFile.getArtist(), size, username));
if (url != null)
return url;

Expand All @@ -292,9 +287,8 @@ public String getArtistImageUrlByMediaFile(@Nonnull String baseUrl, @Nullable Me
String prefix = "ext";
// expire in 5 minutes
Instant expires = Instant.now().plusSeconds(300);
securityService.createGuestUserIfNotExists();
return baseUrl + jwtSecurityService.addJWTToken(
User.USERNAME_GUEST,
username,
UriComponentsBuilder
.fromUriString(prefix + "/coverArt.view")
.queryParam("id", String.valueOf(mediaFileId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public class ArtistServiceTest {
@Mock
private StarredArtistRepository starredArtistRepository;

@Mock
private SecurityService securityService;

@Mock
private SettingsService settingsService;

Expand All @@ -77,7 +74,7 @@ public class ArtistServiceTest {
public void setUp() {
jwtSecurityService = Mockito.spy(new JWTSecurityService(settingsService));
artistService = new ArtistService(artistRepository, starredArtistRepository, jwtSecurityService,
securityService, mediaFileService);
mediaFileService);
}

@Test
Expand All @@ -95,12 +92,11 @@ public void testGetArtistImageURL() {
String url = "";
try (MockedStatic<Instant> instantMock = Mockito.mockStatic(Instant.class, Mockito.CALLS_REAL_METHODS)) {
instantMock.when(Instant::now).thenReturn(now);
url = artistService.getArtistImageURL("http://example.com/", "artist", 30);
url = artistService.getArtistImageURL("http://example.com/", "artist", 30, User.USERNAME_GUEST);
}

// Then
verify(artistRepository).findByName("artist");
verify(securityService).createGuestUserIfNotExists();
assertTrue(url.startsWith("http://example.com/ext/coverArt.view?id=ar-1&size=30&jwt="));
verify(jwtSecurityService).addJWTToken(eq(User.USERNAME_GUEST), any(UriComponentsBuilder.class),
eq(now.plusSeconds(300L)));
Expand All @@ -114,22 +110,22 @@ public void testGetArtistImageURLNoArtistShouldReturnNull() {
when(artistRepository.findByName("artist")).thenReturn(Optional.empty());

// When
String url = artistService.getArtistImageURL("http://example.com/", "artist", 30);
String url = artistService.getArtistImageURL("http://example.com/", "artist", 30, User.USERNAME_GUEST);

// Then
verify(artistRepository).findByName("artist");
verifyNoInteractions(securityService, jwtSecurityService, settingsService);
verifyNoInteractions(jwtSecurityService, settingsService);
assertNull(url);
}

@Test
public void testGetArtistImageURLNullArtistNameShouldReturnNull() {

// When
String url = artistService.getArtistImageURL("http://example.com/", null, 30);
String url = artistService.getArtistImageURL("http://example.com/", null, 30, User.USERNAME_GUEST);

// Then
verifyNoInteractions(artistRepository, securityService, jwtSecurityService, settingsService);
verifyNoInteractions(artistRepository, jwtSecurityService, settingsService);
assertNull(url);
}

Expand Down Expand Up @@ -159,12 +155,11 @@ public void testGetArtistImageURLbyMediaFileWithAlbumArtist(boolean isAudio, boo
String url = "";
try (MockedStatic<Instant> instantMock = Mockito.mockStatic(Instant.class, Mockito.CALLS_REAL_METHODS)) {
instantMock.when(Instant::now).thenReturn(now);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30, User.USERNAME_GUEST);
}

// Then
verify(artistRepository).findByName("artist");
verify(securityService).createGuestUserIfNotExists();
assertTrue(url.startsWith("http://example.com/ext/coverArt.view?id=ar-1&size=30&jwt="));
verify(jwtSecurityService).addJWTToken(eq(User.USERNAME_GUEST), any(UriComponentsBuilder.class),
eq(now.plusSeconds(300L)));
Expand Down Expand Up @@ -196,12 +191,11 @@ public void testGetArtistImageURLbyMediaFileWithArtist(boolean isAudio, boolean
String url = "";
try (MockedStatic<Instant> instantMock = Mockito.mockStatic(Instant.class, Mockito.CALLS_REAL_METHODS)) {
instantMock.when(Instant::now).thenReturn(now);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30, User.USERNAME_GUEST);
}

// Then
verify(artistRepository).findByName("artist");
verify(securityService).createGuestUserIfNotExists();
assertTrue(url.startsWith("http://example.com/ext/coverArt.view?id=ar-1&size=30&jwt="));
verify(jwtSecurityService).addJWTToken(eq(User.USERNAME_GUEST), any(UriComponentsBuilder.class),
eq(now.plusSeconds(300L)));
Expand All @@ -221,13 +215,12 @@ public void testGetArtistImageURLbyMediaFileWithArtistMedia() {
String url = "";
try (MockedStatic<Instant> instantMock = Mockito.mockStatic(Instant.class, Mockito.CALLS_REAL_METHODS)) {
instantMock.when(Instant::now).thenReturn(now);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30, User.USERNAME_GUEST);
}

// Then
verifyNoInteractions(artistRepository);
verifyNoMoreInteractions(mockedMediaFile);
verify(securityService).createGuestUserIfNotExists();
assertTrue(url.startsWith("http://example.com/ext/coverArt.view?id=2&size=30&jwt="));
verify(jwtSecurityService).addJWTToken(eq(User.USERNAME_GUEST), any(UriComponentsBuilder.class),
eq(now.plusSeconds(300L)));
Expand All @@ -250,13 +243,12 @@ public void testGetArtistImageURLbyMediaFileWithAlbumMediaWithNoArtistUrl() {
String url = "";
try (MockedStatic<Instant> instantMock = Mockito.mockStatic(Instant.class, Mockito.CALLS_REAL_METHODS)) {
instantMock.when(Instant::now).thenReturn(now);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30, User.USERNAME_GUEST);
}

// Then
verifyNoInteractions(artistRepository);
verifyNoMoreInteractions(mockedMediaFile);
verify(securityService).createGuestUserIfNotExists();
assertTrue(url.startsWith("http://example.com/ext/coverArt.view?id=2&size=30&jwt="));
verify(jwtSecurityService).addJWTToken(eq(User.USERNAME_GUEST), any(UriComponentsBuilder.class),
eq(now.plusSeconds(300L)));
Expand All @@ -279,13 +271,12 @@ public void testGetArtistImageURLbyMediaFileWithAudioMediaWithNoArtistUrl() {
String url = "";
try (MockedStatic<Instant> instantMock = Mockito.mockStatic(Instant.class, Mockito.CALLS_REAL_METHODS)) {
instantMock.when(Instant::now).thenReturn(now);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30);
url = artistService.getArtistImageUrlByMediaFile("http://example.com/", mockedMediaFile, 30, User.USERNAME_GUEST);
}

// Then
verifyNoInteractions(artistRepository);
verifyNoMoreInteractions(mockedMediaFile);
verify(securityService).createGuestUserIfNotExists();
assertTrue(url.startsWith("http://example.com/ext/coverArt.view?id=2&size=30&jwt="));
verify(jwtSecurityService).addJWTToken(eq(User.USERNAME_GUEST), any(UriComponentsBuilder.class),
eq(now.plusSeconds(300L)));
Expand Down

0 comments on commit 123c884

Please sign in to comment.