added library database
This commit is contained in:
1 parent
8f8b466e89
commit
02a40c2589
10 files changed
+383
-100
No files matched your search
@@ -3,7 +3,7 @@ package net.sergeych.toread
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.toComposeImageBitmap
|
||||
import net.sergeych.toread.fb2.Fb2Binary
|
||||
import net.sergeych.toread.fb2.Fb2Format
|
||||
import net.sergeych.toread.storage.LibraryFileRecord
|
||||
import net.sergeych.toread.storage.ContentAnchor
|
||||
import net.sergeych.toread.storage.ReadingStateRecord
|
||||
import net.sergeych.toread.storage.jdbc.H2LibraryDatabase
|
||||
@@ -49,32 +49,26 @@ actual suspend fun chooseLibraryScanDirectory(): String? = withContext(Dispatche
|
||||
}
|
||||
|
||||
actual suspend fun loadLibraryItems(): List<LibraryItem> = withContext(Dispatchers.IO) {
|
||||
appendLibraryLog("load library items")
|
||||
loadLibraryItemsPage(Int.MAX_VALUE, 0)
|
||||
}
|
||||
|
||||
actual suspend fun loadLibraryItemsPage(limit: Int, offset: Int): List<LibraryItem> = withContext(Dispatchers.IO) {
|
||||
appendLibraryLog("load library items page limit=$limit offset=$offset")
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
val items = db.files.list().map { file ->
|
||||
val book = file.bookId?.let(db.books::get)
|
||||
val parsed = file.storageUri?.let { uri ->
|
||||
runCatching {
|
||||
File(uri).takeIf { it.isFile }?.readBytes()?.let { Fb2Format.parse(it, uri) }
|
||||
}.getOrNull()
|
||||
}
|
||||
LibraryItem(
|
||||
fileId = file.id,
|
||||
bookId = file.bookId,
|
||||
title = parsed?.title ?: book?.title ?: file.originalFilename ?: file.id,
|
||||
authors = parsed?.authors?.mapNotNull { it.displayName.takeIf(String::isNotBlank) }.orEmpty(),
|
||||
language = parsed?.language ?: book?.language,
|
||||
date = parsed?.date,
|
||||
format = file.format,
|
||||
sizeBytes = file.sizeBytes,
|
||||
storageUri = file.storageUri,
|
||||
lastSeenAt = file.lastSeenAt,
|
||||
coverImage = book?.coverImage ?: parsed?.libraryCoverBinary()?.imageBytes(),
|
||||
coverImageMimeType = book?.coverImageMimeType ?: parsed?.libraryCoverBinary()?.contentType,
|
||||
)
|
||||
}
|
||||
appendLibraryLog("loaded library items count=${items.size}")
|
||||
items
|
||||
db.files.listLibraryFiles(limit, offset).map { it.toLibraryItem() }
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadLibraryItem(fileId: String): LibraryItem? = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
db.files.getLibraryFile(fileId)?.toLibraryItem()
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun loadLibraryItemCover(fileId: String): LibraryCover? = withContext(Dispatchers.IO) {
|
||||
openLibraryDatabase().useLibrary { db ->
|
||||
val bookId = db.files.get(fileId)?.bookId ?: return@useLibrary null
|
||||
db.books.getCover(bookId)?.let { LibraryCover(image = it.image, mimeType = it.mimeType) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,6 +278,20 @@ private fun appendLibraryLog(message: String) {
|
||||
file.appendText("${System.currentTimeMillis()} $message\n")
|
||||
}
|
||||
|
||||
private fun LibraryFileRecord.toLibraryItem(): LibraryItem =
|
||||
LibraryItem(
|
||||
fileId = fileId,
|
||||
bookId = bookId,
|
||||
title = title ?: originalFilename ?: fileId,
|
||||
authors = authors,
|
||||
language = language,
|
||||
date = date,
|
||||
format = format,
|
||||
sizeBytes = sizeBytes,
|
||||
storageUri = storageUri,
|
||||
lastSeenAt = lastSeenAt,
|
||||
)
|
||||
|
||||
private fun libraryLogFile(): File =
|
||||
File(System.getProperty("user.home"), ".toread/toread.log")
|
||||
|
||||
|
||||
Reference in new issue
Block a user