diff --git a/backend/src/main/java/org/booklore/config/security/ImageCacheConfig.java b/backend/src/main/java/org/booklore/config/security/ImageCacheConfig.java deleted file mode 100644 index 3015539d7..000000000 --- a/backend/src/main/java/org/booklore/config/security/ImageCacheConfig.java +++ /dev/null @@ -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 imageCachingFilterRegistration() { - FilterRegistrationBean 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; - } -} diff --git a/backend/src/main/java/org/booklore/config/security/filter/ImageCachingFilter.java b/backend/src/main/java/org/booklore/config/security/filter/ImageCachingFilter.java deleted file mode 100644 index 4483a3445..000000000 --- a/backend/src/main/java/org/booklore/config/security/filter/ImageCachingFilter.java +++ /dev/null @@ -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); - } -} diff --git a/backend/src/main/java/org/booklore/controller/BookMediaController.java b/backend/src/main/java/org/booklore/controller/BookMediaController.java index 25eb3101c..ff44617d7 100644 --- a/backend/src/main/java/org/booklore/controller/BookMediaController.java +++ b/backend/src/main/java/org/booklore/controller/BookMediaController.java @@ -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 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 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 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 getAudiobookCover(@Parameter(description = "ID of the book") @PathVariable long bookId) { - return ResponseEntity.ok(bookService.getAudiobookCover(bookId)); + return toImageResponse(bookService.getAudiobookCoverIfPresent(bookId)); + } + + private ResponseEntity toImageResponse(Optional 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(); } -} \ No newline at end of file +} diff --git a/backend/src/main/java/org/booklore/service/book/BookService.java b/backend/src/main/java/org/booklore/service/book/BookService.java index 75abaa31a..2b3fdde65 100644 --- a/backend/src/main/java/org/booklore/service/book/BookService.java +++ b/backend/src/main/java/org/booklore/service/book/BookService.java @@ -280,30 +280,20 @@ public class BookService { return bookUpdateService.assignShelvesToBooks(bookIds, shelfIdsToAssign, shelfIdsToUnassign); } + public Optional 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 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 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 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 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); } } diff --git a/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.html b/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.html index 853e874ce..d61e383b1 100644 --- a/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.html +++ b/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.html @@ -6,20 +6,14 @@ (keydown.enter)="onCardClick($event)">
- @if (coverImageUrl(); as url) { - - } @else { - - } +
@if (showBookTypePill() && displayFormat(); as fmt) { diff --git a/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.scss b/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.scss index 972087cc6..3c8c996bd 100644 --- a/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.scss +++ b/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.scss @@ -47,10 +47,6 @@ border-radius: 8px 8px 0 0; } -.book-cover.square-cover { - object-fit: contain; -} - .cover-container::after { content: ""; position: absolute; diff --git a/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.ts b/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.ts index cb5419bd8..11fb1976f 100644 --- a/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.ts +++ b/frontend/src/app/features/book/components/book-browser/book-card/book-card.component.ts @@ -9,7 +9,7 @@ import {CheckboxChangeEvent, Checkbox} from 'primeng/checkbox'; import {FormsModule} from '@angular/forms'; import {MetadataRefreshType} from '../../../../metadata/model/request/metadata-refresh-type.enum'; import {UrlHelperService} from '../../../../../shared/service/url-helper.service'; -import {CoverPlaceholderComponent} from '../../../../../shared/components/cover-generator/cover-generator.component'; +import {CoverComponent} from '../../../../../shared/components/cover/cover.component'; import {NgClass} from '@angular/common'; import {UserService} from '../../../../settings/user-management/user.service'; import {EmailService} from '../../../../settings/email-v2/email.service'; @@ -31,7 +31,7 @@ import {ButtonDirective} from 'primeng/button'; selector: 'app-book-card', templateUrl: './book-card.component.html', styleUrls: ['./book-card.component.scss'], - imports: [ButtonDirective, Checkbox, FormsModule, NgClass, TieredMenu, Tooltip, RouterLink, TranslocoPipe, CoverPlaceholderComponent], + imports: [ButtonDirective, Checkbox, FormsModule, NgClass, TieredMenu, Tooltip, RouterLink, TranslocoPipe, CoverComponent], standalone: true, changeDetection: ChangeDetectionStrategy.OnPush }) diff --git a/frontend/src/app/features/book/components/book-browser/book-table/book-table-row.component.html b/frontend/src/app/features/book/components/book-browser/book-table/book-table-row.component.html index f9ad87f44..fc1f95975 100644 --- a/frontend/src/app/features/book/components/book-browser/book-table/book-table-row.component.html +++ b/frontend/src/app/features/book/components/book-browser/book-table/book-table-row.component.html @@ -48,21 +48,14 @@ (mouseleave)="coverPreviewHide.emit(currentBook.id)" (focusin)="showCoverPreview(coverOrigin)" (focusout)="coverPreviewHide.emit(currentBook.id)"> - @if (imageUrl) { - - } @else { -