Add right-hand page gestures and managed Android imports
This commit is contained in:
1 parent
a2f417d147
commit
e6be02719d
11 files changed
+142
-52
No files matched your search
@@ -34,6 +34,7 @@ data class LibraryImportResult(
|
||||
class LibraryScanner(
|
||||
private val database: H2LibraryDatabase,
|
||||
private val log: (String) -> Unit = {},
|
||||
private val objectStore: ManagedBookObjectStore? = null,
|
||||
) {
|
||||
fun scanSubtree(root: File, onProgress: (LibraryScanSummary) -> Unit = {}): LibraryScanSummary {
|
||||
require(root.isDirectory) { "Scan root is not a directory: ${root.path}" }
|
||||
@@ -100,9 +101,15 @@ class LibraryScanner(
|
||||
sizeBytes: Long?,
|
||||
lastModifiedMillis: Long?,
|
||||
): LibraryImportResult {
|
||||
val managed = objectStore?.importBytes(bytes)
|
||||
val savedUri = managed?.let { objectStore.fileForObject(it.objectId).absolutePath } ?: storageUri
|
||||
val storageKind = if (managed != null) BookFileStorageKind.MANAGED_FILE else BookFileStorageKind.EXTERNAL_URI
|
||||
val rawSha256 = bytes.sha256Hex()
|
||||
if (sizeBytes != null) {
|
||||
database.files.findByOriginalFilenameSizeAndRawSha256(displayName, sizeBytes, rawSha256).firstOrNull()?.let {
|
||||
if (managed != null) database.files.upsert(it.copy(
|
||||
storageKind = storageKind, storageUri = savedUri, updatedAt = System.currentTimeMillis(),
|
||||
))
|
||||
return LibraryImportResult(fileId = it.id, imported = false)
|
||||
}
|
||||
}
|
||||
@@ -122,8 +129,8 @@ class LibraryScanner(
|
||||
mimeType = displayName.bookMimeType(),
|
||||
sizeBytes = sizeBytes,
|
||||
originalFilename = displayName,
|
||||
storageKind = BookFileStorageKind.EXTERNAL_URI,
|
||||
storageUri = storageUri,
|
||||
storageKind = storageKind,
|
||||
storageUri = savedUri,
|
||||
lastModifiedMillis = lastModifiedMillis,
|
||||
lastSeenAt = now,
|
||||
importedAt = now,
|
||||
@@ -205,8 +212,8 @@ class LibraryScanner(
|
||||
mimeType = displayName.bookMimeType(),
|
||||
sizeBytes = sizeBytes,
|
||||
originalFilename = displayName,
|
||||
storageKind = BookFileStorageKind.EXTERNAL_URI,
|
||||
storageUri = storageUri,
|
||||
storageKind = storageKind,
|
||||
storageUri = savedUri,
|
||||
lastModifiedMillis = lastModifiedMillis,
|
||||
lastSeenAt = now,
|
||||
importedAt = now,
|
||||
|
||||
+8
-1
@@ -1,6 +1,7 @@
|
||||
package net.sergeych.toread.storage.jdbc
|
||||
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.security.MessageDigest
|
||||
|
||||
data class ManagedBookObject(
|
||||
@@ -16,6 +17,12 @@ class ManagedBookObjectStore(
|
||||
|
||||
fun importFile(source: File): ManagedBookObject {
|
||||
require(source.isFile) { "Book source is not a file: ${source.path}" }
|
||||
return source.inputStream().use(::importStream)
|
||||
}
|
||||
|
||||
fun importBytes(bytes: ByteArray): ManagedBookObject = bytes.inputStream().use(::importStream)
|
||||
|
||||
private fun importStream(input: InputStream): ManagedBookObject {
|
||||
objectsDirectory.mkdirs()
|
||||
|
||||
val tempFile = File.createTempFile("book-object-", ".tmp", objectsDirectory)
|
||||
@@ -23,7 +30,7 @@ class ManagedBookObjectStore(
|
||||
var size = 0L
|
||||
|
||||
try {
|
||||
source.inputStream().use { input ->
|
||||
run {
|
||||
tempFile.outputStream().use { output ->
|
||||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||
while (true) {
|
||||
|
||||
@@ -9,6 +9,36 @@ import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class LibraryScannerTest {
|
||||
@Test
|
||||
fun managedImportSurvivesSourceDeletionAndRepairsExistingLink() {
|
||||
val root = Files.createTempDirectory("yereed-managed-import").toFile()
|
||||
val databasePath = File(root, "db/library").also { it.parentFile.mkdirs() }.absolutePath
|
||||
val source = File(root, "source.fb2.zip")
|
||||
source.writeBytes(File(findProjectRoot(), "test_books/Maraini_Zapiski-Terezy-Numy.G7vc8A.872381.fb2.zip").readBytes())
|
||||
val bytes = source.readBytes()
|
||||
val store = ManagedBookObjectStore(File(root, "library"))
|
||||
try {
|
||||
val id = H2LibraryDatabase.openFile(databasePath).useTestDatabase { db ->
|
||||
val linked = LibraryScanner(db).importExternalFileAndGetResult(
|
||||
bytes, source.name, "content://unavailable.provider/book", bytes.size.toLong(), null,
|
||||
)
|
||||
val copied = LibraryScanner(db, objectStore = store).importExternalFileAndGetResult(
|
||||
bytes, source.name, source.absolutePath, bytes.size.toLong(), null,
|
||||
)
|
||||
assertEquals(linked.fileId, copied.fileId)
|
||||
copied.fileId
|
||||
}
|
||||
source.delete()
|
||||
H2LibraryDatabase.openFile(databasePath).useTestDatabase { db ->
|
||||
val saved = assertNotNull(db.files.get(id))
|
||||
assertEquals(net.sergeych.toread.storage.BookFileStorageKind.MANAGED_FILE, saved.storageKind)
|
||||
assertTrue(bytes.contentEquals(File(assertNotNull(saved.storageUri)).readBytes()))
|
||||
}
|
||||
} finally {
|
||||
root.deleteRecursively()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scansFb2ZipSubtreeIntoLinkedLibraryRecords() {
|
||||
val root = findProjectRoot()
|
||||
@@ -132,3 +162,6 @@ class LibraryScannerTest {
|
||||
error("Could not locate project root")
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> H2LibraryDatabase.useTestDatabase(block: (H2LibraryDatabase) -> T): T =
|
||||
try { block(this) } finally { close() }
|
||||
Reference in new issue
Block a user