fix(api): avoid missing-cover.jpg when getting book thumbnails (#2256)

This commit is contained in:
James Ward
2026-08-05 15:46:59 -04:00
committed by GitHub
parent 0db0100ae4
commit 142063432d
3 changed files with 11 additions and 18 deletions
@@ -130,6 +130,9 @@ public class KomgaController {
Long firstBookId = Long.parseLong(books.getContent().getFirst().getId());
Resource coverImage = bookService.getBookThumbnail(firstBookId);
if (coverImage == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok()
.header("Content-Type", "image/jpeg")
.body(coverImage);
@@ -200,6 +203,9 @@ public class KomgaController {
validateBookContentAccess(bookId);
Resource coverImage = bookService.getBookThumbnail(bookId);
if (coverImage == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok()
.header("Content-Type", "image/jpeg")
.body(coverImage);
@@ -285,7 +285,7 @@ public class BookService {
}
public Resource getBookThumbnail(long bookId) {
return getBookThumbnailIfPresent(bookId).orElseGet(this::getMissingCoverResource);
return getBookThumbnailIfPresent(bookId).orElse(null);
}
public Optional<Resource> getBookCoverIfPresent(long bookId) {
@@ -293,7 +293,7 @@ public class BookService {
}
public Resource getBookCover(long bookId) {
return getBookCoverIfPresent(bookId).orElseGet(this::getMissingCoverResource);
return getBookCoverIfPresent(bookId).orElse(null);
}
public Resource getBookCover(String coverHash) {
@@ -305,16 +305,12 @@ public class BookService {
return resolveImageResource(fileService.getAudiobookThumbnailFile(bookId), bookId);
}
public Resource getAudiobookThumbnail(long bookId) {
return getAudiobookThumbnailIfPresent(bookId).orElseGet(this::getMissingCoverResource);
}
public Optional<Resource> getAudiobookCoverIfPresent(long bookId) {
return resolveImageResource(fileService.getAudiobookCoverFile(bookId), bookId);
}
public Resource getAudiobookCover(long bookId) {
return getAudiobookCoverIfPresent(bookId).orElseGet(this::getMissingCoverResource);
return getAudiobookCoverIfPresent(bookId).orElse(null);
}
private Optional<Resource> resolveImageResource(String filePath, long bookId) {
@@ -330,15 +326,6 @@ public class BookService {
}
}
private Resource getMissingCoverResource() {
try {
byte[] bytes = new ClassPathResource("static/images/missing-cover.jpg").getInputStream().readAllBytes();
return new ByteArrayResource(bytes);
} catch (IOException e) {
throw ApiError.INTERNAL_SERVER_ERROR.createException("Failed to load missing cover image");
}
}
public ResponseEntity<StreamingResponseBody> downloadBook(Long bookId) {
return bookDownloadService.downloadBook(bookId);
}
@@ -310,10 +310,10 @@ class BookServiceTest {
}
@Test
void getBookCover_fileMissing_returnsByteArrayResource() {
void getBookCover_fileMissing_returnsNull() {
when(fileService.getCoverFile(1L)).thenReturn("/tmp/nonexistent2.jpg");
Resource res = bookService.getBookCover(1L);
assertTrue(res instanceof ByteArrayResource);
assertNull(res);
}
@Test