diff --git a/r2-testapp/build.gradle b/r2-testapp/build.gradle index ecb75398..d3e71202 100644 --- a/r2-testapp/build.gradle +++ b/r2-testapp/build.gradle @@ -18,7 +18,7 @@ group = 'com.github.readium' int major = 2 int minor = 1 int patch = 0 -int build = 8 +int build = 10 String type = "" def version = "$major.$minor.$patch" @@ -86,22 +86,22 @@ dependencies { if (findProject(':r2-shared')) { implementation project(':r2-shared') } else { - implementation "com.github.readium:r2-shared-kotlin:1.1.3" + implementation "com.github.readium:r2-shared-kotlin:1.1.4" } if (findProject(':r2-navigator')) { implementation project(':r2-navigator') } else { - implementation "com.github.readium:r2-navigator-kotlin:1.1.2" + implementation "com.github.readium:r2-navigator-kotlin:1.1.3" } if (findProject(':r2-streamer')) { implementation project(':r2-streamer') } else { - implementation "com.github.readium:r2-streamer-kotlin:1.1.2" + implementation "com.github.readium:r2-streamer-kotlin:1.1.3" } if (findProject(':r2-opds')) { implementation project(':r2-opds') } else { - implementation "com.github.readium:r2-opds-kotlin:1.1.1" + implementation "com.github.readium:r2-opds-kotlin:1.1.2" } if (findProject(':r2-lcp')) { implementation project(':r2-lcp') @@ -165,7 +165,7 @@ dependencies { implementation "nl.komponents.kovenant:kovenant-jvm:$KOVENANT_VERSION" implementation "nl.komponents.kovenant:kovenant-functional:$KOVENANT_VERSION" - implementation 'org.zeroturnaround:zt-zip:1.12' + implementation 'org.zeroturnaround:zt-zip:1.13' implementation 'joda-time:joda-time:2.9.9' implementation 'org.slf4j:slf4j-nop:1.7.25' diff --git a/r2-testapp/src/main/java/org/readium/r2/testapp/library/LibraryActivity.kt b/r2-testapp/src/main/java/org/readium/r2/testapp/library/LibraryActivity.kt index 940ff9db..4f73e96c 100644 --- a/r2-testapp/src/main/java/org/readium/r2/testapp/library/LibraryActivity.kt +++ b/r2-testapp/src/main/java/org/readium/r2/testapp/library/LibraryActivity.kt @@ -664,26 +664,73 @@ open class LibraryActivity : AppCompatActivity(), BooksAdapter.RecyclerViewClick } private fun copySamplesFromAssetsToStorage() { - assets.list("Samples")?.filter { it.endsWith(".epub") || it.endsWith(".cbz") }?.let { list -> + assets.list("Samples")?.filter { + it.endsWith(Publication.EXTENSION.EPUB.value) + || it.endsWith(Publication.EXTENSION.CBZ.value) + || it.endsWith(Publication.EXTENSION.AUDIO.value) + || it.endsWith(Publication.EXTENSION.DIVINA.value) + }?.let { list -> for (element in list) { val input = assets.open("Samples/$element") val fileName = UUID.randomUUID().toString() val publicationPath = R2DIRECTORY + fileName - input.toFile(publicationPath) + + when { + element.endsWith(Publication.EXTENSION.DIVINA.value) -> { + val output = File(publicationPath); + if (!output.exists()) { + if (!output.mkdir()) { + throw RuntimeException("Cannot create directory"); + } + } + ZipUtil.unpack(input, output) + } + element.endsWith(Publication.EXTENSION.AUDIO.value) -> { + val output = File(publicationPath) + if (!output.exists()) { + if (!output.mkdir()) { + throw RuntimeException("Cannot create directory") + } + } + ZipUtil.unpack(input, output) + } + else -> input.toFile(publicationPath) + } + val file = File(publicationPath) - if (element.endsWith(".epub")) { - val parser = EpubParser() - val pub = parser.parse(publicationPath) - if (pub != null) { - prepareToServe(pub, fileName, file.absolutePath, add = true, lcp = pub.container.drm?.let { true } - ?: false) + + when { + element.endsWith(Publication.EXTENSION.EPUB.value) -> { + val parser = EpubParser() + val pub = parser.parse(publicationPath) + if (pub != null) { + prepareToServe(pub, fileName, file.absolutePath, add = true, lcp = pub.container.drm?.let { true } + ?: false) + } } - } else if (element.endsWith(".cbz")) { - val parser = CbzParser() - val pub = parser.parse(publicationPath) - if (pub != null) { - prepareToServe(pub, fileName, file.absolutePath, add = true, lcp = pub.container.drm?.let { true } - ?: false) + element.endsWith(Publication.EXTENSION.CBZ.value) -> { + val parser = CbzParser() + val pub = parser.parse(publicationPath) + if (pub != null) { + prepareToServe(pub, fileName, file.absolutePath, add = true, lcp = pub.container.drm?.let { true } + ?: false) + } + } + element.endsWith(Publication.EXTENSION.AUDIO.value) -> { + val parser = AudioBookParser() + val pub = parser.parse(publicationPath) + if (pub != null) { + prepareToServe(pub, fileName, file.absolutePath, add = true, lcp = pub.container.drm?.let { true } + ?: false) + } + } + element.endsWith(Publication.EXTENSION.DIVINA.value) -> { + val parser = DiViNaParser() + val pub = parser.parse(publicationPath) + if (pub != null) { + prepareToServe(pub, fileName, file.absolutePath, add = true, lcp = pub.container.drm?.let { true } + ?: false) + } } } } @@ -714,104 +761,52 @@ open class LibraryActivity : AppCompatActivity(), BooksAdapter.RecyclerViewClick val container = pub.container launch { - if (publication.type == Publication.TYPE.EPUB) { - val publicationIdentifier = publication.metadata.identifier - preferences.edit().putString("$publicationIdentifier-publicationPort", localPort.toString()).apply() - val author = authorName(publication) - if (add) { - var book = Book(fileName, publication.metadata.title, author, absolutePath, null, publication.coverLink?.href, publicationIdentifier, null, Publication.EXTENSION.EPUB) - publication.coverLink?.href?.let { - val blob = ZipUtil.unpackEntry(File(absolutePath), it.removePrefix("/")) - blob?.let { - book = Book(fileName, publication.metadata.title, author, absolutePath, null, publication.coverLink?.href, publicationIdentifier, blob, Publication.EXTENSION.EPUB) - } ?: run { - book = Book(fileName, publication.metadata.title, author, absolutePath, null, publication.coverLink?.href, publicationIdentifier, null, Publication.EXTENSION.EPUB) - } - } ?: run { - book = Book(fileName, publication.metadata.title, author, absolutePath, null, publication.coverLink?.href, publicationIdentifier, null, Publication.EXTENSION.EPUB) + val publicationIdentifier = publication.metadata.identifier + val book: Book = when { + publication.type == Publication.TYPE.EPUB -> { + preferences.edit().putString("$publicationIdentifier-publicationPort", localPort.toString()).apply() + val author = authorName(publication) + val cover = publication.coverLink?.href?.let { + ZipUtil.unpackEntry(File(absolutePath), it.removePrefix("/")) } - database.books.insert(book, false)?.let { - book.id = it - books.add(0, book) - booksAdapter.notifyDataSetChanged() - catalogView.longSnackbar("publication added to your library") - if (!lcp) { - //prepareSyntheticPageList(publication, book) - } - } ?: run { - - showDuplicateBookAlert(book, publication, lcp) - + if (!lcp) { + server.addEpub(publication, container, "/$fileName", applicationContext.filesDir.path + "/" + Injectable.Style.rawValue + "/UserProperties.json") } - - } - if (!lcp) { - server.addEpub(publication, container, "/$fileName", applicationContext.filesDir.path + "/" + Injectable.Style.rawValue + "/UserProperties.json") + Book(fileName, publication.metadata.title, author, absolutePath, null, publication.coverLink?.href, publicationIdentifier, cover, Publication.EXTENSION.EPUB) } - - } else if (publication.type == Publication.TYPE.CBZ) { - if (add) { - publication.coverLink?.href?.let { - val book = Book(fileName, publication.metadata.title, null, absolutePath, null, publication.coverLink?.href, UUID.randomUUID().toString(), container.data(it), Publication.EXTENSION.CBZ) - database.books.insert(book, false)?.let { id -> - book.id = id - books.add(0, book) - booksAdapter.notifyDataSetChanged() - - if (!lcp) { - //prepareSyntheticPageList(publication, book) - } - } ?: run { - - showDuplicateBookAlert(book, publication, lcp) - - } + publication.type == Publication.TYPE.CBZ -> { + val cover = publication.coverLink?.href?.let { + container.data(it) } - } - } else if (publication.type == Publication.TYPE.DiViNa) { - if (add) { - val book = Book(fileName, publication.metadata.title, null, absolutePath, null, publication.coverLink?.href, UUID.randomUUID().toString(), null, Publication.EXTENSION.DIVINA) - database.books.insert(book, false)?.let { - book.id = it - books.add(0, book) - booksAdapter.notifyDataSetChanged() - - } ?: run { - showDuplicateBookAlert(book, publication, lcp) - - } + Book(fileName, publication.metadata.title, null, absolutePath, null, publication.coverLink?.href, publicationIdentifier, cover, Publication.EXTENSION.CBZ) } - } else if (publication.type == Publication.TYPE.AUDIO) { - if (add) { - //Getting book cover from file path to bitmap - val ref = publication.coverLink?.href - val stream = ByteArrayOutputStream() - val coverByteArray = ref?.let { - try { - pub.container.data(ref) - } catch (e: Exception) { - null - } + publication.type == Publication.TYPE.DiViNa -> { + val cover = publication.coverLink?.href?.let { + container.data(it) } - coverByteArray?.let { - val bitmap = BitmapFactory.decodeByteArray(coverByteArray, 0, coverByteArray.size) - bitmap!!.compress(Bitmap.CompressFormat.PNG, 100, stream) + + Book(fileName, publication.metadata.title, null, absolutePath, null, publication.coverLink?.href, publicationIdentifier, cover, Publication.EXTENSION.DIVINA) + } + publication.type == Publication.TYPE.AUDIO -> { + val cover = publication.coverLink?.href?.let { + container.data(it) } //Building book object and adding it to library - val book = Book(fileName, publication.metadata.title, null, absolutePath, null, publication.coverLink?.href, UUID.randomUUID().toString(), stream.toByteArray(), Publication.EXTENSION.AUDIO) - - - database.books.insert(book, false)?.let { - book.id = it - books.add(0, book) - booksAdapter.notifyDataSetChanged() - - } ?: run { - showDuplicateBookAlert(book, publication, lcp) - } + Book(fileName, publication.metadata.title, null, absolutePath, null, publication.coverLink?.href, publicationIdentifier, cover, Publication.EXTENSION.AUDIO) + } + else -> TODO() + } + if (add) { + database.books.insert(book, false)?.let { id -> + book.id = id + books.add(0, book) + booksAdapter.notifyDataSetChanged() + catalogView.longSnackbar("publication added to your library") + } ?: run { + showDuplicateBookAlert(book, publication, lcp) } } }