refactor(covers): consolidate cover images into app-cover (#1928)
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
package org.booklore.config.security;
|
||||
|
||||
import org.booklore.config.security.filter.ImageCachingFilter;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
@Configuration
|
||||
public class ImageCacheConfig {
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<ImageCachingFilter> imageCachingFilterRegistration() {
|
||||
FilterRegistrationBean<ImageCachingFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
registrationBean.setFilter(new ImageCachingFilter());
|
||||
registrationBean.addUrlPatterns(
|
||||
"/api/v1/media/book/*/cover",
|
||||
"/api/v1/media/book/*/thumbnail",
|
||||
"/api/v1/media/book/*/backup-cover"
|
||||
);
|
||||
registrationBean.setOrder(Ordered.LOWEST_PRECEDENCE);
|
||||
return registrationBean;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package org.booklore.config.security.filter;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class ImageCachingFilter extends OncePerRequestFilter {
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
String uri = request.getRequestURI();
|
||||
if (uri.startsWith("/api/v1/media/book/") &&
|
||||
(uri.contains("/cover") || uri.contains("/thumbnail") || uri.contains("/backup-cover") ||
|
||||
uri.contains("/cbx/pages/"))) {
|
||||
response.setHeader(HttpHeaders.CACHE_CONTROL, "public, max-age=3600");
|
||||
response.setHeader(HttpHeaders.EXPIRES, String.valueOf(System.currentTimeMillis() + 3600_000));
|
||||
}
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,15 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
|
||||
@Tag(name = "Book Media", description = "Endpoints for retrieving book media such as covers, thumbnails, and pages")
|
||||
@AllArgsConstructor
|
||||
@@ -25,6 +28,8 @@ import java.io.IOException;
|
||||
@RequestMapping("/api/v1/media")
|
||||
public class BookMediaController {
|
||||
|
||||
private static final CacheControl IMAGE_CACHE = CacheControl.maxAge(Duration.ofHours(1)).cachePrivate();
|
||||
|
||||
private final BookService bookService;
|
||||
private final CbxReaderService cbxReaderService;
|
||||
private final BookDropService bookDropService;
|
||||
@@ -32,34 +37,44 @@ public class BookMediaController {
|
||||
|
||||
@Operation(summary = "Get book thumbnail", description = "Retrieve the thumbnail image for a specific book.")
|
||||
@ApiResponse(responseCode = "200", description = "Book thumbnail returned successfully")
|
||||
@ApiResponse(responseCode = "404", description = "No thumbnail exists for this book")
|
||||
@GetMapping("/book/{bookId}/thumbnail")
|
||||
@CheckBookAccess(bookIdParam = "bookId")
|
||||
public ResponseEntity<Resource> getBookThumbnail(@Parameter(description = "ID of the book") @PathVariable long bookId) {
|
||||
return ResponseEntity.ok(bookService.getBookThumbnail(bookId));
|
||||
return toImageResponse(bookService.getBookThumbnailIfPresent(bookId));
|
||||
}
|
||||
|
||||
@Operation(summary = "Get book cover", description = "Retrieve the cover image for a specific book.")
|
||||
@ApiResponse(responseCode = "200", description = "Book cover returned successfully")
|
||||
@ApiResponse(responseCode = "404", description = "No cover exists for this book")
|
||||
@GetMapping("/book/{bookId}/cover")
|
||||
@CheckBookAccess(bookIdParam = "bookId")
|
||||
public ResponseEntity<Resource> getBookCover(@Parameter(description = "ID of the book") @PathVariable long bookId) {
|
||||
return ResponseEntity.ok(bookService.getBookCover(bookId));
|
||||
return toImageResponse(bookService.getBookCoverIfPresent(bookId));
|
||||
}
|
||||
|
||||
@Operation(summary = "Get audiobook thumbnail", description = "Retrieve the audiobook thumbnail image for a specific book.")
|
||||
@ApiResponse(responseCode = "200", description = "Audiobook thumbnail returned successfully")
|
||||
@ApiResponse(responseCode = "404", description = "No audiobook thumbnail exists for this book")
|
||||
@GetMapping("/book/{bookId}/audiobook-thumbnail")
|
||||
@CheckBookAccess(bookIdParam = "bookId")
|
||||
public ResponseEntity<Resource> getAudiobookThumbnail(@Parameter(description = "ID of the book") @PathVariable long bookId) {
|
||||
return ResponseEntity.ok(bookService.getAudiobookThumbnail(bookId));
|
||||
return toImageResponse(bookService.getAudiobookThumbnailIfPresent(bookId));
|
||||
}
|
||||
|
||||
@Operation(summary = "Get audiobook cover", description = "Retrieve the audiobook cover image for a specific book.")
|
||||
@ApiResponse(responseCode = "200", description = "Audiobook cover returned successfully")
|
||||
@ApiResponse(responseCode = "404", description = "No audiobook cover exists for this book")
|
||||
@GetMapping("/book/{bookId}/audiobook-cover")
|
||||
@CheckBookAccess(bookIdParam = "bookId")
|
||||
public ResponseEntity<Resource> getAudiobookCover(@Parameter(description = "ID of the book") @PathVariable long bookId) {
|
||||
return ResponseEntity.ok(bookService.getAudiobookCover(bookId));
|
||||
return toImageResponse(bookService.getAudiobookCoverIfPresent(bookId));
|
||||
}
|
||||
|
||||
private ResponseEntity<Resource> toImageResponse(Optional<Resource> image) {
|
||||
return image
|
||||
.map(resource -> ResponseEntity.ok().cacheControl(IMAGE_CACHE).body(resource))
|
||||
.orElseGet(() -> ResponseEntity.notFound().cacheControl(CacheControl.noStore()).build());
|
||||
}
|
||||
|
||||
@Operation(summary = "Get CBX page as image", description = "Retrieve a specific page from a CBX book as an image.")
|
||||
@@ -72,6 +87,7 @@ public class BookMediaController {
|
||||
@Parameter(description = "Optional book type for alternative format (e.g., PDF, CBX)") @RequestParam(required = false) String bookType,
|
||||
HttpServletResponse response) throws IOException {
|
||||
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
|
||||
response.setHeader(HttpHeaders.CACHE_CONTROL, IMAGE_CACHE.getHeaderValue());
|
||||
cbxReaderService.streamPageImage(bookId, bookType, pageNumber, response.getOutputStream());
|
||||
}
|
||||
|
||||
@@ -110,4 +126,4 @@ public class BookMediaController {
|
||||
.body(file)
|
||||
: ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,30 +280,20 @@ public class BookService {
|
||||
return bookUpdateService.assignShelvesToBooks(bookIds, shelfIdsToAssign, shelfIdsToUnassign);
|
||||
}
|
||||
|
||||
public Optional<Resource> getBookThumbnailIfPresent(long bookId) {
|
||||
return resolveImageResource(fileService.getThumbnailFile(bookId), bookId);
|
||||
}
|
||||
|
||||
public Resource getBookThumbnail(long bookId) {
|
||||
Path thumbnailPath = Paths.get(fileService.getThumbnailFile(bookId));
|
||||
try {
|
||||
if (Files.exists(thumbnailPath)) {
|
||||
return new UrlResource(thumbnailPath.toUri());
|
||||
} else {
|
||||
return new ClassPathResource("static/images/missing-cover.jpg");
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Failed to load book cover for bookId=" + bookId, e);
|
||||
}
|
||||
return getBookThumbnailIfPresent(bookId).orElseGet(this::getMissingCoverResource);
|
||||
}
|
||||
|
||||
public Optional<Resource> getBookCoverIfPresent(long bookId) {
|
||||
return resolveImageResource(fileService.getCoverFile(bookId), bookId);
|
||||
}
|
||||
|
||||
public Resource getBookCover(long bookId) {
|
||||
Path coverPath = Paths.get(fileService.getCoverFile(bookId));
|
||||
try {
|
||||
if (Files.exists(coverPath)) {
|
||||
return new UrlResource(coverPath.toUri());
|
||||
} else {
|
||||
return getMissingCoverResource();
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Failed to load book cover for bookId=" + bookId, e);
|
||||
}
|
||||
return getBookCoverIfPresent(bookId).orElseGet(this::getMissingCoverResource);
|
||||
}
|
||||
|
||||
public Resource getBookCover(String coverHash) {
|
||||
@@ -311,29 +301,32 @@ public class BookService {
|
||||
return getBookCover(bookEntity.getId());
|
||||
}
|
||||
|
||||
public Optional<Resource> getAudiobookThumbnailIfPresent(long bookId) {
|
||||
return resolveImageResource(fileService.getAudiobookThumbnailFile(bookId), bookId);
|
||||
}
|
||||
|
||||
public Resource getAudiobookThumbnail(long bookId) {
|
||||
Path thumbnailPath = Paths.get(fileService.getAudiobookThumbnailFile(bookId));
|
||||
try {
|
||||
if (Files.exists(thumbnailPath)) {
|
||||
return new UrlResource(thumbnailPath.toUri());
|
||||
} else {
|
||||
return getMissingCoverResource();
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Failed to load audiobook thumbnail for bookId=" + bookId, e);
|
||||
}
|
||||
return getAudiobookThumbnailIfPresent(bookId).orElseGet(this::getMissingCoverResource);
|
||||
}
|
||||
|
||||
public Optional<Resource> getAudiobookCoverIfPresent(long bookId) {
|
||||
return resolveImageResource(fileService.getAudiobookCoverFile(bookId), bookId);
|
||||
}
|
||||
|
||||
public Resource getAudiobookCover(long bookId) {
|
||||
Path coverPath = Paths.get(fileService.getAudiobookCoverFile(bookId));
|
||||
return getAudiobookCoverIfPresent(bookId).orElseGet(this::getMissingCoverResource);
|
||||
}
|
||||
|
||||
private Optional<Resource> resolveImageResource(String filePath, long bookId) {
|
||||
Path path = Paths.get(filePath);
|
||||
if (!Files.exists(path)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
try {
|
||||
if (Files.exists(coverPath)) {
|
||||
return new UrlResource(coverPath.toUri());
|
||||
} else {
|
||||
return getMissingCoverResource();
|
||||
}
|
||||
return Optional.of(new UrlResource(path.toUri()));
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Failed to load audiobook cover for bookId=" + bookId, e);
|
||||
log.error("Failed to load image for bookId={} from path={}", bookId, filePath, e);
|
||||
throw ApiError.INTERNAL_SERVER_ERROR.createException("Failed to load image for bookId=" + bookId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user