From 535c6d7eca2caf48bb58df1b9039f56c9f10c302 Mon Sep 17 00:00:00 2001 From: Snd-R <76580768+Snd-R@users.noreply.github.com> Date: Thu, 30 Jun 2022 11:54:34 +0300 Subject: [PATCH] fix: re-enable content length header for book downloads this will allow progress bar to be displayed in clients --- .../web/EtagFilterConfiguration.kt | 35 ++++++++++++++----- .../interfaces/api/rest/BookController.kt | 1 + 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/web/EtagFilterConfiguration.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/web/EtagFilterConfiguration.kt index 86f99f8ff..b092d3e21 100644 --- a/komga/src/main/kotlin/org/gotson/komga/infrastructure/web/EtagFilterConfiguration.kt +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/web/EtagFilterConfiguration.kt @@ -3,18 +3,35 @@ package org.gotson.komga.infrastructure.web import org.springframework.boot.web.servlet.FilterRegistrationBean import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.http.server.PathContainer import org.springframework.web.filter.ShallowEtagHeaderFilter +import org.springframework.web.util.pattern.PathPatternParser +import javax.servlet.http.HttpServletRequest @Configuration class EtagFilterConfiguration { + + private val excludePatterns = listOf( + PathPatternParser.defaultInstance.parse("/api/v1/books/*/file/**"), + PathPatternParser.defaultInstance.parse("/opds/v1.2/books/*/file/**"), + PathPatternParser.defaultInstance.parse("/api/v1/readlists/*/file/**"), + PathPatternParser.defaultInstance.parse("/api/v1/series/*/file/**"), + ) + @Bean - fun shallowEtagHeaderFilter(): FilterRegistrationBean = - FilterRegistrationBean(ShallowEtagHeaderFilter()) - .also { - it.addUrlPatterns( - "/api/*", - "/opds/*", - ) - it.setName("etagFilter") - } + fun shallowEtagHeaderFilter(): FilterRegistrationBean = + FilterRegistrationBean( + object : ShallowEtagHeaderFilter() { + override fun shouldNotFilter(request: HttpServletRequest): Boolean { + val path = PathContainer.parsePath(request.servletPath) + return excludePatterns.any { it.matches(path) } + } + }, + ).also { + it.addUrlPatterns( + "/api/*", + "/opds/*", + ) + it.setName("etagFilter") + } } 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 53220ede4..be7384131 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 @@ -391,6 +391,7 @@ class BookController( }, ) .contentType(getMediaTypeOrDefault(media.mediaType)) + .contentLength(this.contentLength()) .body(stream) } } catch (ex: FileNotFoundException) {