Skip to content

Commit

Permalink
fix(api): book file download uses streaming
Browse files Browse the repository at this point in the history
instead of loading the full file in memory
  • Loading branch information
gotson committed Apr 3, 2020
1 parent 856b0a5 commit 74be1f0
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.gotson.komga.interfaces.rest.dto.BookDto
import org.gotson.komga.interfaces.rest.dto.BookMetadataUpdateDto
import org.gotson.komga.interfaces.rest.dto.PageDto
import org.gotson.komga.interfaces.rest.dto.toDto
import org.springframework.core.io.FileSystemResource
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
Expand All @@ -46,7 +47,6 @@ import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.context.request.WebRequest
import org.springframework.web.server.ResponseStatusException
import java.io.File
import java.io.FileNotFoundException
import java.nio.file.NoSuchFileException
import java.time.ZoneOffset
Expand Down Expand Up @@ -211,18 +211,21 @@ class BookController(
fun getBookFile(
@AuthenticationPrincipal principal: KomgaPrincipal,
@PathVariable bookId: Long
): ResponseEntity<ByteArray> =
): ResponseEntity<FileSystemResource> =
bookRepository.findByIdOrNull(bookId)?.let { book ->
if (!principal.user.canAccessBook(book)) throw ResponseStatusException(HttpStatus.UNAUTHORIZED)
try {
ResponseEntity.ok()
.headers(HttpHeaders().apply {
contentDisposition = ContentDisposition.builder("attachment")
.filename(book.fileName())
.build()
})
.contentType(getMediaTypeOrDefault(book.media.mediaType))
.body(File(book.url.toURI()).readBytes())
with(FileSystemResource(book.path())) {
if (!exists()) throw FileNotFoundException(path)
ResponseEntity.ok()
.headers(HttpHeaders().apply {
contentDisposition = ContentDisposition.builder("attachment")
.filename(book.fileName())
.build()
})
.contentType(getMediaTypeOrDefault(book.media.mediaType))
.body(this)
}
} catch (ex: FileNotFoundException) {
logger.warn(ex) { "File not found: $book" }
throw ResponseStatusException(HttpStatus.NOT_FOUND, "File not found, it may have moved")
Expand Down

0 comments on commit 74be1f0

Please sign in to comment.