fix(opds): update mime type support for FB2 and CBX formats based on file extension (#2077)
* fix(opds): add mime type support for FB2 and CBX formats based on file extension Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(opds): return correct MIME types for all supported book formats in feed links Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(opds): remove incorrect MIME type mapping for DJVU and default fallback Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * test(opds): add unit tests for correct MIME type mapping in catalog feed generation Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> --------- Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+15
-5
@@ -547,7 +547,7 @@ public class OpdsFeedService {
|
||||
}
|
||||
|
||||
private void appendLinks(StringBuilder feed, Book book) {
|
||||
String mimeType = "application/" + fileMimeType(book);
|
||||
String mimeType = fileMimeType(book);
|
||||
feed.append(" <link href=\"/api/v1/opds/")
|
||||
.append(book.getId()).append("/download\" rel=\"http://opds-spec.org/acquisition\" type=\"").append(mimeType).append("\"/>\n");
|
||||
|
||||
@@ -604,12 +604,22 @@ public class OpdsFeedService {
|
||||
|
||||
private String fileMimeType(Book book) {
|
||||
if (book == null || book.getBookType() == null) {
|
||||
return "octet-stream";
|
||||
return "application/octet-stream";
|
||||
}
|
||||
return switch (book.getBookType()) {
|
||||
case PDF -> "pdf";
|
||||
case EPUB -> "epub+zip";
|
||||
default -> "octet-stream";
|
||||
case PDF -> "application/pdf";
|
||||
case EPUB -> "application/epub+zip";
|
||||
case FB2 -> "application/x-fictionbook+xml";
|
||||
case CBX -> {
|
||||
if (book.getFileName() != null) {
|
||||
String lower = book.getFileName().toLowerCase();
|
||||
if (lower.endsWith(".cbr")) yield "application/vnd.comicbook-rar";
|
||||
if (lower.endsWith(".cbz")) yield "application/vnd.comicbook+zip";
|
||||
if (lower.endsWith(".cb7")) yield "application/x-7z-compressed";
|
||||
if (lower.endsWith(".cbt")) yield "application/x-tar";
|
||||
}
|
||||
yield "application/vnd.comicbook+zip";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
package com.adityachandel.booklore.service.opds;
|
||||
|
||||
import com.adityachandel.booklore.config.security.service.AuthenticationService;
|
||||
import com.adityachandel.booklore.config.security.userdetails.OpdsUserDetails;
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import com.adityachandel.booklore.model.dto.OpdsUserV2;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.model.enums.OpdsSortOrder;
|
||||
import com.adityachandel.booklore.service.MagicShelfService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class OpdsFeedServiceMimeTypeTest {
|
||||
|
||||
private AuthenticationService authenticationService;
|
||||
private OpdsBookService opdsBookService;
|
||||
private MagicShelfService magicShelfService;
|
||||
private MagicShelfBookService magicShelfBookService;
|
||||
private OpdsFeedService opdsFeedService;
|
||||
private HttpServletRequest request;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
authenticationService = mock(AuthenticationService.class);
|
||||
opdsBookService = mock(OpdsBookService.class);
|
||||
magicShelfService = mock(MagicShelfService.class);
|
||||
magicShelfBookService = mock(MagicShelfBookService.class);
|
||||
opdsFeedService = new OpdsFeedService(authenticationService, opdsBookService, magicShelfService, magicShelfBookService);
|
||||
request = mock(HttpServletRequest.class);
|
||||
|
||||
mockAuthenticatedUser();
|
||||
mockRequest();
|
||||
}
|
||||
|
||||
private void mockAuthenticatedUser() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
OpdsUserV2 v2 = mock(OpdsUserV2.class);
|
||||
when(userDetails.getOpdsUserV2()).thenReturn(v2);
|
||||
when(v2.getUserId()).thenReturn(1L);
|
||||
when(v2.getSortOrder()).thenReturn(OpdsSortOrder.RECENT);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
}
|
||||
|
||||
private void mockRequest() {
|
||||
when(request.getParameter(any())).thenReturn(null);
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/opds/catalog");
|
||||
when(request.getQueryString()).thenReturn(null);
|
||||
}
|
||||
|
||||
private void mockBooksPage(Book book) {
|
||||
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
|
||||
when(opdsBookService.getBooksPage(eq(1L), any(), any(), any(), eq(0), eq(50))).thenReturn(page);
|
||||
when(opdsBookService.applySortOrder(any(), any())).thenReturn(page);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMimeTypeForEpub() {
|
||||
Book book = createBook(BookFileType.EPUB, "book.epub");
|
||||
mockBooksPage(book);
|
||||
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||
assertThat(xml).contains("type=\"application/epub+zip\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMimeTypeForPdf() {
|
||||
Book book = createBook(BookFileType.PDF, "document.pdf");
|
||||
mockBooksPage(book);
|
||||
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||
assertThat(xml).contains("type=\"application/pdf\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMimeTypeForCbz() {
|
||||
Book book = createBook(BookFileType.CBX, "comic.cbz");
|
||||
mockBooksPage(book);
|
||||
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||
assertThat(xml).contains("type=\"application/vnd.comicbook+zip\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMimeTypeForCbr() {
|
||||
Book book = createBook(BookFileType.CBX, "comic.cbr");
|
||||
mockBooksPage(book);
|
||||
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||
assertThat(xml).contains("type=\"application/vnd.comicbook-rar\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMimeTypeForCb7() {
|
||||
Book book = createBook(BookFileType.CBX, "comic.cb7");
|
||||
mockBooksPage(book);
|
||||
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||
assertThat(xml).contains("type=\"application/x-7z-compressed\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMimeTypeForCbt() {
|
||||
Book book = createBook(BookFileType.CBX, "comic.cbt");
|
||||
mockBooksPage(book);
|
||||
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||
assertThat(xml).contains("type=\"application/x-tar\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMimeTypeForFb2() {
|
||||
Book book = createBook(BookFileType.FB2, "book.fb2");
|
||||
mockBooksPage(book);
|
||||
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||
assertThat(xml).contains("type=\"application/x-fictionbook+xml\"");
|
||||
}
|
||||
|
||||
private Book createBook(BookFileType type, String fileName) {
|
||||
return Book.builder()
|
||||
.id(1L)
|
||||
.bookType(type)
|
||||
.fileName(fileName)
|
||||
.addedOn(Instant.now())
|
||||
.metadata(BookMetadata.builder().title("Test Book").build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user