better display of book path in logs

This commit is contained in:
Gauthier Roebroeck 2019-08-23 14:35:13 +08:00
parent 4639bc1281
commit 20d0981494
5 changed files with 17 additions and 15 deletions

View file

@ -47,6 +47,8 @@ class Book(
value.book = this
field = value
}
override fun toString(): String = url.toURI().path
}
fun Book.path(): Path = Paths.get(this.url.toURI())

View file

@ -24,15 +24,15 @@ class BookManager(
@Transactional
@Async("parseBookTaskExecutor")
fun parseAndPersist(book: Book): Future<Long> {
logger.info { "Parse and persist book: ${book.url}" }
logger.info { "Parse and persist book: $book" }
return AsyncResult(measureTimeMillis {
try {
book.metadata = bookParser.parse(book)
} catch (ex: UnsupportedMediaTypeException) {
logger.info(ex) { "Unsupported media type: ${ex.mediaType}. Book: ${book.url}" }
logger.info(ex) { "Unsupported media type: ${ex.mediaType}. Book: $book" }
book.metadata = BookMetadata(status = Status.UNSUPPORTED, mediaType = ex.mediaType)
} catch (ex: Exception) {
logger.error(ex) { "Error while parsing. Book: ${book.url}" }
logger.error(ex) { "Error while parsing. Book: $book" }
book.metadata = BookMetadata(status = Status.ERROR)
}
bookRepository.save(book)
@ -42,7 +42,7 @@ class BookManager(
@Transactional
@Async("parseBookTaskExecutor")
fun regenerateThumbnailAndPersist(book: Book): Future<Long> {
logger.info { "Regenerate thumbnail and persist book: ${book.url}" }
logger.info { "Regenerate thumbnail and persist book: $book" }
return AsyncResult(measureTimeMillis {
try {
book.metadata = bookParser.regenerateThumbnail(book)

View file

@ -34,28 +34,28 @@ class BookParser(
private val thumbnailFormat = "png"
fun parse(book: Book): BookMetadata {
logger.info { "Trying to parse book: ${book.url}" }
logger.info { "Trying to parse book: $book" }
val mediaType = contentDetector.detectMediaType(book.path())
logger.info { "Detected media type: $mediaType" }
if (!supportedMediaTypes.keys.contains(mediaType))
throw UnsupportedMediaTypeException("Unsupported mime type: $mediaType. File: ${book.url}", mediaType)
throw UnsupportedMediaTypeException("Unsupported mime type: $mediaType. File: $book", mediaType)
val pages = supportedMediaTypes.getValue(mediaType).getPagesList(book.path())
.sortedWith(compareBy(natSortComparator) { it.fileName })
logger.info { "Book has ${pages.size} pages" }
logger.info { "Trying to generate cover for book: ${book.url}" }
logger.info { "Trying to generate cover for book: $book" }
val thumbnail = generateThumbnail(book, mediaType, pages.first().fileName)
return BookMetadata(mediaType = mediaType, status = Status.READY, pages = pages, thumbnail = thumbnail)
}
fun regenerateThumbnail(book: Book): BookMetadata {
logger.info { "Regenerate thumbnail for book: ${book.url}" }
logger.info { "Regenerate thumbnail for book: $book" }
if (book.metadata.status != Status.READY) {
logger.warn { "Book metadata is not ready, cannot generate thumbnail. Book: ${book.url}" }
logger.warn { "Book metadata is not ready, cannot generate thumbnail. Book: $book" }
throw MetadataNotReadyException()
}
@ -81,12 +81,12 @@ class BookParser(
}
}
} catch (ex: Exception) {
logger.warn(ex) { "Could not generate thumbnail for book: ${book.url}" }
logger.warn(ex) { "Could not generate thumbnail for book: $book" }
null
}
fun getPageContent(book: Book, number: Int): ByteArray {
logger.info { "Get page #$number for book: ${book.url}" }
logger.info { "Get page #$number for book: $book" }
if (book.metadata.status != Status.READY) {
logger.warn { "Book metadata is not ready, cannot get pages" }

View file

@ -61,7 +61,7 @@ class LibraryManager(
val existingBook = bookRepository.findByUrl(newBook.url) ?: newBook
if (newBook.fileLastModified != existingBook.fileLastModified) {
logger.info { "Book changed on disk, update and reset metadata status: ${newBook.url}" }
logger.info { "Book changed on disk, update and reset metadata status: ${newBook}" }
existingBook.fileLastModified = newBook.fileLastModified
existingBook.name = newBook.name
existingBook.metadata.reset()

View file

@ -150,7 +150,7 @@ class SerieController(
.contentType(mediaType)
.body(File(book.url.toURI()).readBytes())
} catch (ex: FileNotFoundException) {
logger.warn(ex) { "File not found: ${book.url}" }
logger.warn(ex) { "File not found: $book" }
throw ResponseStatusException(HttpStatus.NOT_FOUND, "File not found, it may have moved")
}
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
@ -197,7 +197,7 @@ class SerieController(
} catch (ex: MetadataNotReadyException) {
throw ResponseStatusException(HttpStatus.NO_CONTENT, "Book cannot be parsed")
} catch (ex: NoSuchFileException) {
logger.warn(ex) { "File not found: ${book.url}" }
logger.warn(ex) { "File not found: $book" }
throw ResponseStatusException(HttpStatus.NOT_FOUND, "File not found, it may have moved")
}
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
@ -236,7 +236,7 @@ fun Book.toDto() =
BookDto(
id = id,
name = name,
url = url.toString(),
url = url.toURI().path,
lastModified = lastModifiedDate?.toUTC(),
metadata = BookMetadataDto(
status = metadata.status.toString(),