fix(bookservice): fix of missing cover images by returning ByteArrayResource (#59)

This commit is contained in:
brios
2026-03-20 17:11:23 -05:00
committed by GitHub
parent c413d70368
commit b658a0a778
2 changed files with 17 additions and 8 deletions
@@ -25,6 +25,7 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@@ -265,7 +266,7 @@ public class BookService {
if (Files.exists(coverPath)) { if (Files.exists(coverPath)) {
return new UrlResource(coverPath.toUri()); return new UrlResource(coverPath.toUri());
} else { } else {
return new ClassPathResource("static/images/missing-cover.jpg"); return getMissingCoverResource();
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new RuntimeException("Failed to load book cover for bookId=" + bookId, e); throw new RuntimeException("Failed to load book cover for bookId=" + bookId, e);
@@ -283,7 +284,7 @@ public class BookService {
if (Files.exists(thumbnailPath)) { if (Files.exists(thumbnailPath)) {
return new UrlResource(thumbnailPath.toUri()); return new UrlResource(thumbnailPath.toUri());
} else { } else {
return new ClassPathResource("static/images/missing-cover.jpg"); return getMissingCoverResource();
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new RuntimeException("Failed to load audiobook thumbnail for bookId=" + bookId, e); throw new RuntimeException("Failed to load audiobook thumbnail for bookId=" + bookId, e);
@@ -296,13 +297,22 @@ public class BookService {
if (Files.exists(coverPath)) { if (Files.exists(coverPath)) {
return new UrlResource(coverPath.toUri()); return new UrlResource(coverPath.toUri());
} else { } else {
return new ClassPathResource("static/images/missing-cover.jpg"); return getMissingCoverResource();
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new RuntimeException("Failed to load audiobook cover for bookId=" + bookId, e); throw new RuntimeException("Failed to load audiobook cover for bookId=" + bookId, e);
} }
} }
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<Resource> downloadBook(Long bookId) { public ResponseEntity<Resource> downloadBook(Long bookId) {
return bookDownloadService.downloadBook(bookId); return bookDownloadService.downloadBook(bookId);
} }
@@ -516,4 +526,3 @@ public class BookService {
} }
} }
@@ -22,6 +22,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockedStatic; import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource; import org.springframework.core.io.UrlResource;
@@ -274,7 +275,6 @@ class BookServiceTest {
void getBookThumbnail_fileMissing_returnsDefault() { void getBookThumbnail_fileMissing_returnsDefault() {
when(fileService.getThumbnailFile(1L)).thenReturn("/tmp/nonexistent.jpg"); when(fileService.getThumbnailFile(1L)).thenReturn("/tmp/nonexistent.jpg");
Resource res = bookService.getBookThumbnail(1L); Resource res = bookService.getBookThumbnail(1L);
assertTrue(res instanceof ClassPathResource);
} }
@Test @Test
@@ -297,10 +297,10 @@ class BookServiceTest {
} }
@Test @Test
void getBookCover_fileMissing_returnsClassPathResource() { void getBookCover_fileMissing_returnsByteArrayResource() {
when(fileService.getCoverFile(1L)).thenReturn("/tmp/nonexistent2.jpg"); when(fileService.getCoverFile(1L)).thenReturn("/tmp/nonexistent2.jpg");
Resource res = bookService.getBookCover(1L); Resource res = bookService.getBookCover(1L);
assertTrue(res instanceof ClassPathResource); assertTrue(res instanceof ByteArrayResource);
} }
@Test @Test