fix(opds): add audiobook covers for opds navigation (#1425)
This commit is contained in:
@@ -67,6 +67,16 @@ public class OpdsController {
|
|||||||
public ResponseEntity<Resource> getBookCover(@Parameter(description = "ID of the book") @PathVariable long bookId) {
|
public ResponseEntity<Resource> getBookCover(@Parameter(description = "ID of the book") @PathVariable long bookId) {
|
||||||
opdsBookService.validateBookContentAccess(bookId, getOpdsUserId());
|
opdsBookService.validateBookContentAccess(bookId, getOpdsUserId());
|
||||||
Resource coverImage = bookService.getBookThumbnail(bookId);
|
Resource coverImage = bookService.getBookThumbnail(bookId);
|
||||||
|
|
||||||
|
if (coverImage == null) {
|
||||||
|
// Fall back to audiobook if possible
|
||||||
|
coverImage = bookService.getAudiobookCover(bookId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (coverImage == null) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
String contentType = "image/jpeg";
|
String contentType = "image/jpeg";
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentType(MediaType.parseMediaType(contentType))
|
.contentType(MediaType.parseMediaType(contentType))
|
||||||
|
|||||||
@@ -555,6 +555,20 @@ public class OpdsFeedService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Instant getCoverUpdatedOn(Book book) {
|
||||||
|
if (book.getMetadata() != null) {
|
||||||
|
if (book.getMetadata().getCoverUpdatedOn() != null) {
|
||||||
|
return book.getMetadata().getCoverUpdatedOn();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (book.getMetadata().getAudiobookCoverUpdatedOn() != null) {
|
||||||
|
return book.getMetadata().getAudiobookCoverUpdatedOn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private void appendLinks(StringBuilder feed, Book book) {
|
private void appendLinks(StringBuilder feed, Book book) {
|
||||||
// Add acquisition link for primary file
|
// Add acquisition link for primary file
|
||||||
if (book.getPrimaryFile() != null) {
|
if (book.getPrimaryFile() != null) {
|
||||||
@@ -568,8 +582,9 @@ public class OpdsFeedService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (book.getMetadata() != null && book.getMetadata().getCoverUpdatedOn() != null) {
|
Instant coverUpdatedOn = getCoverUpdatedOn(book);
|
||||||
String coverUrl = "/api/v1/opds/" + book.getId() + "/cover?" + book.getMetadata().getCoverUpdatedOn();
|
if (coverUpdatedOn != null) {
|
||||||
|
String coverUrl = "/api/v1/opds/" + book.getId() + "/cover?" + coverUpdatedOn;
|
||||||
feed.append(" <link rel=\"http://opds-spec.org/image\" href=\"")
|
feed.append(" <link rel=\"http://opds-spec.org/image\" href=\"")
|
||||||
.append(escapeXml(coverUrl)).append("\" type=\"image/jpeg\"/>\n");
|
.append(escapeXml(coverUrl)).append("\" type=\"image/jpeg\"/>\n");
|
||||||
feed.append(" <link rel=\"http://opds-spec.org/image/thumbnail\" href=\"")
|
feed.append(" <link rel=\"http://opds-spec.org/image/thumbnail\" href=\"")
|
||||||
|
|||||||
@@ -167,6 +167,98 @@ class OpdsFeedServiceTest {
|
|||||||
verify(opdsBookService).getBooksPage(TEST_USER_ID, null, null, null, 0, 50);
|
verify(opdsBookService).getBooksPage(TEST_USER_ID, null, null, null, 0, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generateCatalogFeed_shouldOmitCoverWhenNotUpdatedOn() {
|
||||||
|
mockAuthenticatedUser();
|
||||||
|
|
||||||
|
when(request.getParameter("libraryId")).thenReturn(null);
|
||||||
|
when(request.getParameter("shelfId")).thenReturn(null);
|
||||||
|
when(request.getParameter("magicShelfId")).thenReturn(null);
|
||||||
|
when(request.getParameter("q")).thenReturn(null);
|
||||||
|
when(request.getParameter("page")).thenReturn(null);
|
||||||
|
when(request.getParameter("size")).thenReturn(null);
|
||||||
|
when(request.getRequestURI()).thenReturn("/api/v1/opds/catalog");
|
||||||
|
when(request.getQueryString()).thenReturn(null);
|
||||||
|
|
||||||
|
Book book = Book.builder()
|
||||||
|
.id(10L)
|
||||||
|
.primaryFile(BookFile.builder().id(1L).bookType(BookFileType.EPUB).build())
|
||||||
|
.addedOn(FIXED_INSTANT)
|
||||||
|
.metadata(BookMetadata.builder()
|
||||||
|
.build())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
|
||||||
|
when(opdsBookService.getBooksPage(eq(TEST_USER_ID), any(), any(), any(), eq(0), eq(50))).thenReturn(page);
|
||||||
|
when(opdsBookService.applySortOrder(any(), any())).thenReturn(page);
|
||||||
|
|
||||||
|
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||||
|
assertThat(xml).doesNotContain("link rel=\"http://opds-spec.org/image\" href=\"/api/v1/opds/10/cover");
|
||||||
|
verify(opdsBookService).getBooksPage(TEST_USER_ID, null, null, null, 0, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generateCatalogFeed_shouldShowEbookCover() {
|
||||||
|
mockAuthenticatedUser();
|
||||||
|
|
||||||
|
when(request.getParameter("libraryId")).thenReturn(null);
|
||||||
|
when(request.getParameter("shelfId")).thenReturn(null);
|
||||||
|
when(request.getParameter("magicShelfId")).thenReturn(null);
|
||||||
|
when(request.getParameter("q")).thenReturn(null);
|
||||||
|
when(request.getParameter("page")).thenReturn(null);
|
||||||
|
when(request.getParameter("size")).thenReturn(null);
|
||||||
|
when(request.getRequestURI()).thenReturn("/api/v1/opds/catalog");
|
||||||
|
when(request.getQueryString()).thenReturn(null);
|
||||||
|
|
||||||
|
Book book = Book.builder()
|
||||||
|
.id(10L)
|
||||||
|
.primaryFile(BookFile.builder().id(1L).bookType(BookFileType.EPUB).build())
|
||||||
|
.addedOn(FIXED_INSTANT)
|
||||||
|
.metadata(BookMetadata.builder()
|
||||||
|
.coverUpdatedOn(FIXED_INSTANT)
|
||||||
|
.build())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
|
||||||
|
when(opdsBookService.getBooksPage(eq(TEST_USER_ID), any(), any(), any(), eq(0), eq(50))).thenReturn(page);
|
||||||
|
when(opdsBookService.applySortOrder(any(), any())).thenReturn(page);
|
||||||
|
|
||||||
|
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||||
|
assertThat(xml).contains("link rel=\"http://opds-spec.org/image\" href=\"/api/v1/opds/10/cover");
|
||||||
|
verify(opdsBookService).getBooksPage(TEST_USER_ID, null, null, null, 0, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void generateCatalogFeed_shouldShowAudiobookCover() {
|
||||||
|
mockAuthenticatedUser();
|
||||||
|
|
||||||
|
when(request.getParameter("libraryId")).thenReturn(null);
|
||||||
|
when(request.getParameter("shelfId")).thenReturn(null);
|
||||||
|
when(request.getParameter("magicShelfId")).thenReturn(null);
|
||||||
|
when(request.getParameter("q")).thenReturn(null);
|
||||||
|
when(request.getParameter("page")).thenReturn(null);
|
||||||
|
when(request.getParameter("size")).thenReturn(null);
|
||||||
|
when(request.getRequestURI()).thenReturn("/api/v1/opds/catalog");
|
||||||
|
when(request.getQueryString()).thenReturn(null);
|
||||||
|
|
||||||
|
Book book = Book.builder()
|
||||||
|
.id(10L)
|
||||||
|
.primaryFile(BookFile.builder().id(1L).bookType(BookFileType.EPUB).build())
|
||||||
|
.addedOn(FIXED_INSTANT)
|
||||||
|
.metadata(BookMetadata.builder()
|
||||||
|
.audiobookCoverUpdatedOn(FIXED_INSTANT)
|
||||||
|
.build())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
|
||||||
|
when(opdsBookService.getBooksPage(eq(TEST_USER_ID), any(), any(), any(), eq(0), eq(50))).thenReturn(page);
|
||||||
|
when(opdsBookService.applySortOrder(any(), any())).thenReturn(page);
|
||||||
|
|
||||||
|
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||||
|
assertThat(xml).contains("link rel=\"http://opds-spec.org/image\" href=\"/api/v1/opds/10/cover");
|
||||||
|
verify(opdsBookService).getBooksPage(TEST_USER_ID, null, null, null, 0, 50);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateCatalogFeed_shouldHandleEmptyPage() {
|
void generateCatalogFeed_shouldHandleEmptyPage() {
|
||||||
mockAuthenticatedUser();
|
mockAuthenticatedUser();
|
||||||
|
|||||||
Reference in New Issue
Block a user