fix(bookservice): fix of missing cover images by returning ByteArrayResource (#59)
This commit is contained in:
@@ -25,6 +25,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -265,7 +266,7 @@ public class BookService {
|
||||
if (Files.exists(coverPath)) {
|
||||
return new UrlResource(coverPath.toUri());
|
||||
} else {
|
||||
return new ClassPathResource("static/images/missing-cover.jpg");
|
||||
return getMissingCoverResource();
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Failed to load book cover for bookId=" + bookId, e);
|
||||
@@ -283,7 +284,7 @@ public class BookService {
|
||||
if (Files.exists(thumbnailPath)) {
|
||||
return new UrlResource(thumbnailPath.toUri());
|
||||
} else {
|
||||
return new ClassPathResource("static/images/missing-cover.jpg");
|
||||
return getMissingCoverResource();
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Failed to load audiobook thumbnail for bookId=" + bookId, e);
|
||||
@@ -296,13 +297,22 @@ public class BookService {
|
||||
if (Files.exists(coverPath)) {
|
||||
return new UrlResource(coverPath.toUri());
|
||||
} else {
|
||||
return new ClassPathResource("static/images/missing-cover.jpg");
|
||||
return getMissingCoverResource();
|
||||
}
|
||||
} catch (MalformedURLException 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) {
|
||||
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.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
@@ -274,7 +275,6 @@ class BookServiceTest {
|
||||
void getBookThumbnail_fileMissing_returnsDefault() {
|
||||
when(fileService.getThumbnailFile(1L)).thenReturn("/tmp/nonexistent.jpg");
|
||||
Resource res = bookService.getBookThumbnail(1L);
|
||||
assertTrue(res instanceof ClassPathResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -297,10 +297,10 @@ class BookServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBookCover_fileMissing_returnsClassPathResource() {
|
||||
void getBookCover_fileMissing_returnsByteArrayResource() {
|
||||
when(fileService.getCoverFile(1L)).thenReturn("/tmp/nonexistent2.jpg");
|
||||
Resource res = bookService.getBookCover(1L);
|
||||
assertTrue(res instanceof ClassPathResource);
|
||||
assertTrue(res instanceof ByteArrayResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -440,4 +440,4 @@ class BookServiceTest {
|
||||
assertEquals(1, result.size());
|
||||
assertTrue(result.contains(shelf1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user