fix(api): return created thumbnail

This commit is contained in:
Snd-R 2022-03-14 09:03:19 +03:00 committed by GitHub
parent 6449f2247d
commit dd1ffbe54a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 22 deletions

View File

@ -107,7 +107,7 @@ class BookLifecycle(
}
}
fun addThumbnailForBook(thumbnail: ThumbnailBook, markSelected: MarkSelectedPreference) {
fun addThumbnailForBook(thumbnail: ThumbnailBook, markSelected: MarkSelectedPreference): ThumbnailBook {
when (thumbnail.type) {
ThumbnailBook.Type.GENERATED -> {
// only one generated thumbnail is allowed
@ -140,7 +140,9 @@ class BookLifecycle(
if (selected) thumbnailBookRepository.markSelected(thumbnail)
else thumbnailsHouseKeeping(thumbnail.bookId)
eventPublisher.publishEvent(DomainEvent.ThumbnailBookAdded(thumbnail.copy(selected = selected)))
val newThumbnail = thumbnail.copy(selected = selected)
eventPublisher.publishEvent(DomainEvent.ThumbnailBookAdded(newThumbnail))
return newThumbnail
}
fun deleteThumbnailForBook(thumbnail: ThumbnailBook) {

View File

@ -77,7 +77,7 @@ class ReadListLifecycle(
}
}
fun addThumbnail(thumbnail: ThumbnailReadList) {
fun addThumbnail(thumbnail: ThumbnailReadList): ThumbnailReadList {
when (thumbnail.type) {
ThumbnailReadList.Type.USER_UPLOADED -> {
thumbnailReadListRepository.insert(thumbnail)
@ -88,6 +88,7 @@ class ReadListLifecycle(
}
eventPublisher.publishEvent(DomainEvent.ThumbnailReadListAdded(thumbnail))
return thumbnail
}
fun markSelectedThumbnail(thumbnail: ThumbnailReadList) {

View File

@ -73,7 +73,7 @@ class SeriesCollectionLifecycle(
}
}
fun addThumbnail(thumbnail: ThumbnailSeriesCollection) {
fun addThumbnail(thumbnail: ThumbnailSeriesCollection): ThumbnailSeriesCollection {
when (thumbnail.type) {
ThumbnailSeriesCollection.Type.USER_UPLOADED -> {
thumbnailSeriesCollectionRepository.insert(thumbnail)
@ -84,6 +84,7 @@ class SeriesCollectionLifecycle(
}
eventPublisher.publishEvent(DomainEvent.ThumbnailSeriesCollectionAdded(thumbnail))
return thumbnail
}
fun markSelectedThumbnail(thumbnail: ThumbnailSeriesCollection) {

View File

@ -263,7 +263,7 @@ class SeriesLifecycle(
return null
}
fun addThumbnailForSeries(thumbnail: ThumbnailSeries, markSelected: MarkSelectedPreference) {
fun addThumbnailForSeries(thumbnail: ThumbnailSeries, markSelected: MarkSelectedPreference): ThumbnailSeries {
// delete existing thumbnail with the same url
if (thumbnail.url != null) {
thumbnailsSeriesRepository.findAllBySeriesId(thumbnail.seriesId)
@ -284,7 +284,9 @@ class SeriesLifecycle(
if (selected) thumbnailsSeriesRepository.markSelected(thumbnail)
eventPublisher.publishEvent(DomainEvent.ThumbnailSeriesAdded(thumbnail.copy(selected = selected)))
val newThumbnail = thumbnail.copy(selected = selected)
eventPublisher.publishEvent(DomainEvent.ThumbnailSeriesAdded(newThumbnail))
return newThumbnail
}
fun deleteThumbnailForSeries(thumbnail: ThumbnailSeries) {

View File

@ -303,19 +303,18 @@ class BookController(
@PostMapping(value = ["api/v1/books/{bookId}/thumbnails"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
@PreAuthorize("hasRole('$ROLE_ADMIN')")
@ResponseStatus(HttpStatus.ACCEPTED)
fun addUserUploadedBookThumbnail(
@AuthenticationPrincipal principal: KomgaPrincipal,
@PathVariable(name = "bookId") bookId: String,
@RequestParam("file") file: MultipartFile,
@RequestParam("selected") selected: Boolean = true,
) {
): ThumbnailBookDto {
val book = bookRepository.findByIdOrNull(bookId) ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
if (!contentDetector.isImage(file.inputStream.buffered().use { contentDetector.detectMediaType(it) }))
throw ResponseStatusException(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
bookLifecycle.addThumbnailForBook(
return bookLifecycle.addThumbnailForBook(
ThumbnailBook(
bookId = book.id,
thumbnail = file.bytes,
@ -323,7 +322,7 @@ class BookController(
selected = selected,
),
if (selected) MarkSelectedPreference.YES else MarkSelectedPreference.NO,
)
).toDto()
}
@PutMapping("api/v1/books/{bookId}/thumbnails/{thumbnailId}/selected")

View File

@ -165,26 +165,25 @@ class ReadListController(
@PostMapping(value = ["{id}/thumbnails"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
@PreAuthorize("hasRole('$ROLE_ADMIN')")
@ResponseStatus(HttpStatus.ACCEPTED)
fun addUserUploadedReadListThumbnail(
@AuthenticationPrincipal principal: KomgaPrincipal,
@PathVariable(name = "id") id: String,
@RequestParam("file") file: MultipartFile,
@RequestParam("selected") selected: Boolean = true,
) {
): ThumbnailReadListDto {
readListRepository.findByIdOrNull(id, principal.user.getAuthorizedLibraryIds(null), principal.user.restrictions)?.let { readList ->
if (!contentDetector.isImage(file.inputStream.buffered().use { contentDetector.detectMediaType(it) }))
throw ResponseStatusException(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
readListLifecycle.addThumbnail(
return readListLifecycle.addThumbnail(
ThumbnailReadList(
readListId = readList.id,
thumbnail = file.bytes,
type = ThumbnailReadList.Type.USER_UPLOADED,
selected = selected,
),
)
).toDto()
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
}

View File

@ -144,26 +144,25 @@ class SeriesCollectionController(
@PostMapping(value = ["{id}/thumbnails"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
@PreAuthorize("hasRole('$ROLE_ADMIN')")
@ResponseStatus(HttpStatus.ACCEPTED)
fun addUserUploadedCollectionThumbnail(
@AuthenticationPrincipal principal: KomgaPrincipal,
@PathVariable(name = "id") id: String,
@RequestParam("file") file: MultipartFile,
@RequestParam("selected") selected: Boolean = true,
) {
): ThumbnailSeriesCollectionDto {
collectionRepository.findByIdOrNull(id, principal.user.getAuthorizedLibraryIds(null))?.let { collection ->
if (!contentDetector.isImage(file.inputStream.buffered().use { contentDetector.detectMediaType(it) }))
throw ResponseStatusException(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
collectionLifecycle.addThumbnail(
return collectionLifecycle.addThumbnail(
ThumbnailSeriesCollection(
collectionId = collection.id,
thumbnail = file.bytes,
type = ThumbnailSeriesCollection.Type.USER_UPLOADED,
selected = selected,
),
)
).toDto()
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
}

View File

@ -377,24 +377,23 @@ class SeriesController(
@PostMapping(value = ["v1/series/{seriesId}/thumbnails"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
@PreAuthorize("hasRole('$ROLE_ADMIN')")
@ResponseStatus(HttpStatus.ACCEPTED)
fun postUserUploadedSeriesThumbnail(
@PathVariable(name = "seriesId") seriesId: String,
@RequestParam("file") file: MultipartFile,
@RequestParam("selected") selected: Boolean = true,
) {
): SeriesThumbnailDto {
val series = seriesRepository.findByIdOrNull(seriesId) ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
if (!contentDetector.isImage(file.inputStream.buffered().use { contentDetector.detectMediaType(it) }))
throw ResponseStatusException(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
seriesLifecycle.addThumbnailForSeries(
return seriesLifecycle.addThumbnailForSeries(
ThumbnailSeries(
seriesId = series.id,
thumbnail = file.bytes,
type = ThumbnailSeries.Type.USER_UPLOADED,
),
if (selected) MarkSelectedPreference.YES else MarkSelectedPreference.NO,
)
).toDto()
}
@PutMapping("v1/series/{seriesId}/thumbnails/{thumbnailId}/selected")