refactor(opds): remove chunky specific code as chunky doesn't work anymore with Komga

This commit is contained in:
Gauthier Roebroeck 2023-10-05 12:04:51 +08:00
parent 6d549359bf
commit 9d0a533897

View File

@ -50,13 +50,11 @@ import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
@ -227,7 +225,6 @@ class OpdsController(
@GetMapping(ROUTE_ON_DECK)
fun getOnDeck(
@AuthenticationPrincipal principal: KomgaPrincipal,
@RequestHeader(name = HttpHeaders.USER_AGENT, required = false, defaultValue = "") userAgent: String,
@Parameter(hidden = true) page: Pageable,
): OpdsFeed {
val bookPage = bookDtoRepository.findAllOnDeck(
@ -249,7 +246,7 @@ class OpdsController(
linkStart(),
*linkPage(builder, bookPage).toTypedArray(),
),
entries = bookPage.getEntriesWithSeriesTitle(userAgent),
entries = bookPage.content.getEntriesWithSeriesTitle(),
)
}
@ -257,7 +254,6 @@ class OpdsController(
@GetMapping(ROUTE_KEEP_READING)
fun getKeepReading(
@AuthenticationPrincipal principal: KomgaPrincipal,
@RequestHeader(name = HttpHeaders.USER_AGENT, required = false, defaultValue = "") userAgent: String,
@Parameter(hidden = true) page: Pageable,
): OpdsFeed {
val pageable = PageRequest.of(page.pageNumber, page.pageSize, Sort.by(Sort.Order.desc("readProgress.readDate")))
@ -288,7 +284,7 @@ class OpdsController(
linkStart(),
*linkPage(builder, bookPage).toTypedArray(),
),
entries = bookPage.getEntriesWithSeriesTitle(userAgent),
entries = bookPage.content.getEntriesWithSeriesTitle(),
)
}
@ -336,7 +332,6 @@ class OpdsController(
@GetMapping(ROUTE_SERIES_LATEST)
fun getLatestSeries(
@AuthenticationPrincipal principal: KomgaPrincipal,
@RequestHeader(name = HttpHeaders.USER_AGENT, required = false, defaultValue = "") userAgent: String,
@Parameter(hidden = true) page: Pageable,
): OpdsFeed {
val pageable = PageRequest.of(page.pageNumber, page.pageSize, Sort.by(Sort.Order.desc("lastModified")))
@ -350,11 +345,6 @@ class OpdsController(
val uriBuilder = uriBuilder(ROUTE_SERIES_LATEST)
val pageIndex = seriesPage.number * seriesPage.size
val entries = seriesPage.mapIndexed { index, seriesDto ->
seriesDto.toOpdsEntry(if (shouldEnforceSort(userAgent)) pageIndex + index + 1 else null)
}
return OpdsFeedNavigation(
id = ID_SERIES_LATEST,
title = "Latest series",
@ -365,7 +355,7 @@ class OpdsController(
linkStart(),
*linkPage(uriBuilder, seriesPage).toTypedArray(),
),
entries = entries,
entries = seriesPage.content.map { it.toOpdsEntry() },
)
}
@ -373,7 +363,6 @@ class OpdsController(
@GetMapping(ROUTE_BOOKS_LATEST)
fun getLatestBooks(
@AuthenticationPrincipal principal: KomgaPrincipal,
@RequestHeader(name = HttpHeaders.USER_AGENT, required = false, defaultValue = "") userAgent: String,
@Parameter(hidden = true) page: Pageable,
): OpdsFeed {
val bookSearch = BookSearchWithReadProgress(
@ -397,7 +386,7 @@ class OpdsController(
linkStart(),
*linkPage(uriBuilder, bookPage).toTypedArray(),
),
entries = bookPage.getEntriesWithSeriesTitle(userAgent),
entries = bookPage.content.getEntriesWithSeriesTitle(),
)
}
@ -510,7 +499,6 @@ class OpdsController(
@GetMapping("series/{id}")
fun getOneSeries(
@AuthenticationPrincipal principal: KomgaPrincipal,
@RequestHeader(name = HttpHeaders.USER_AGENT, required = false, defaultValue = "") userAgent: String,
@PathVariable id: String,
@Parameter(hidden = true) page: Pageable,
): OpdsFeed =
@ -525,7 +513,7 @@ class OpdsController(
val pageable = PageRequest.of(page.pageNumber, page.pageSize, Sort.by(Sort.Order.asc("metadata.numberSort")))
val entries = bookDtoRepository.findAll(bookSearch, principal.user.id, pageable, principal.user.restrictions)
.map { bookDto -> bookDto.toOpdsEntry(mediaRepository.findById(bookDto.id)) { if (shouldEnforceSort(userAgent)) "${decimalFormat.format(it.metadata.numberSort)} - " else "" } }
.map { it.toOpdsEntry(mediaRepository.findById(it.id)) }
val uriBuilder = uriBuilder("series/$id")
@ -583,7 +571,6 @@ class OpdsController(
@GetMapping("collections/{id}")
fun getOneCollection(
@AuthenticationPrincipal principal: KomgaPrincipal,
@RequestHeader(name = HttpHeaders.USER_AGENT, required = false, defaultValue = "") userAgent: String,
@PathVariable id: String,
@Parameter(hidden = true) page: Pageable,
): OpdsFeed =
@ -599,10 +586,7 @@ class OpdsController(
)
val entries = seriesDtoRepository.findAllByCollectionId(collection.id, seriesSearch, principal.user.id, pageable, principal.user.restrictions)
.map { seriesDto ->
val index = if (shouldEnforceSort(userAgent)) collection.seriesIds.indexOf(seriesDto.id) + 1 else null
seriesDto.toOpdsEntry(index)
}
.map { it.toOpdsEntry() }
val uriBuilder = uriBuilder("collections/$id")
@ -624,7 +608,6 @@ class OpdsController(
@GetMapping("readlists/{id}")
fun getOneReadList(
@AuthenticationPrincipal principal: KomgaPrincipal,
@RequestHeader(name = HttpHeaders.USER_AGENT, required = false, defaultValue = "") userAgent: String,
@PathVariable id: String,
@Parameter(hidden = true) page: Pageable,
): OpdsFeed =
@ -649,8 +632,7 @@ class OpdsController(
)
val entries = booksPage.map { bookDto ->
val index = readList.bookIds.filterValues { it == bookDto.id }.keys.first()
bookDto.toOpdsEntry(mediaRepository.findById(bookDto.id)) { "${if (shouldEnforceSort(userAgent)) "${decimalFormat.format(index + 1)} - " else ""}${it.seriesTitle} ${it.metadata.number}: " }
bookDto.toOpdsEntry(mediaRepository.findById(bookDto.id)) { "${it.seriesTitle} ${it.metadata.number}: " }
}
val uriBuilder = uriBuilder("readlists/$id")
@ -750,15 +732,10 @@ class OpdsController(
link = OpdsLinkFeedNavigation(OpdsLinkRel.SUBSECTION, uriBuilder("readlists/$id").toUriString()),
)
private fun Page<BookDto>.getEntriesWithSeriesTitle(userAgent: String): List<OpdsEntryAcquisition> {
val pageIndex = (this.number * this.size)
return this.mapIndexed { index, bookDto ->
bookDto.toOpdsEntry(mediaRepository.findById(bookDto.id)) { "${if (shouldEnforceSort(userAgent)) "${pageIndex + index + 1} - " else ""}${it.seriesTitle} ${it.metadata.number}: " }
private fun List<BookDto>.getEntriesWithSeriesTitle(): List<OpdsEntryAcquisition> =
map { bookDto ->
bookDto.toOpdsEntry(mediaRepository.findById(bookDto.id)) { "${it.seriesTitle} ${it.metadata.number}: " }
}
}
private fun shouldEnforceSort(userAgent: String) =
userAgent.contains("chunky", ignoreCase = true)
private fun sanitize(fileName: String): String =
fileName.replace(";", "")