From 0d94ae2c44ec1c69aecd7571cb30dae23bef90d5 Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Thu, 30 Nov 2023 09:17:17 +0800 Subject: [PATCH] fix(api): epub resources could not be retrieved if komga is running with a servlet context path --- .../gotson/komga/interfaces/api/rest/BookController.kt | 8 +++----- .../komga/interfaces/api/rest/PageHashController.kt | 2 +- .../komga/interfaces/api/rest/TransientBooksController.kt | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/BookController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/BookController.kt index 618821e5e..f017d5e3c 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/BookController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/BookController.kt @@ -78,7 +78,6 @@ import org.springframework.http.MediaType import org.springframework.http.ResponseEntity import org.springframework.security.access.prepost.PreAuthorize import org.springframework.security.core.annotation.AuthenticationPrincipal -import org.springframework.util.AntPathMatcher import org.springframework.util.MimeTypeUtils import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping @@ -96,9 +95,7 @@ import org.springframework.web.context.request.ServletWebRequest import org.springframework.web.context.request.WebRequest import org.springframework.web.multipart.MultipartFile import org.springframework.web.server.ResponseStatusException -import org.springframework.web.servlet.HandlerMapping import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody -import org.springframework.web.util.UriUtils import java.io.FileNotFoundException import java.io.OutputStream import java.nio.charset.StandardCharsets.UTF_8 @@ -667,15 +664,16 @@ class BookController( } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) @GetMapping( - value = ["api/v1/books/{bookId}/resource/**"], + value = ["api/v1/books/{bookId}/resource/{*resource}"], produces = ["*/*"], ) fun getBookResource( request: HttpServletRequest, @AuthenticationPrincipal principal: KomgaPrincipal?, @PathVariable bookId: String, + @PathVariable resource: String, ): ResponseEntity { - val resourceName = AntPathMatcher().extractPathWithinPattern(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE) as String, request.requestURI).let { UriUtils.decode(it, UTF_8) } + val resourceName = resource.removePrefix("/") val isFont = FONT_EXTENSIONS.contains(FilenameUtils.getExtension(resourceName).lowercase()) if (!isFont && principal == null) throw ResponseStatusException(HttpStatus.UNAUTHORIZED) diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/PageHashController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/PageHashController.kt index 233282b13..e42b86813 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/PageHashController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/PageHashController.kt @@ -87,7 +87,7 @@ class PageHashController( pageHashLifecycle.getPage(pageHash, resize)?.let { ResponseEntity.ok() .contentType(getMediaTypeOrDefault(it.mediaType)) - .body(it.bytes) + .body(it.bytes) } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) @PutMapping diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/TransientBooksController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/TransientBooksController.kt index db08588db..af6674497 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/TransientBooksController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/TransientBooksController.kt @@ -71,7 +71,7 @@ class TransientBooksController( ResponseEntity.ok() .contentType(getMediaTypeOrDefault(pageContent.mediaType)) - .body(pageContent.bytes) + .body(pageContent.bytes) } catch (ex: IndexOutOfBoundsException) { throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Page number does not exist") } catch (ex: MediaNotReadyException) {