This commit is contained in:
kunfei
2023-03-14 12:42:49 +08:00
parent f44e73a974
commit f1a0658677
6 changed files with 52 additions and 18 deletions
@@ -3,6 +3,9 @@
package io.legado.app.help.book package io.legado.app.help.book
import android.net.Uri import android.net.Uri
import com.script.SimpleBindings
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppLog
import io.legado.app.constant.BookSourceType import io.legado.app.constant.BookSourceType
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.appDb import io.legado.app.data.appDb
@@ -202,3 +205,18 @@ fun Book.isSameNameAuthor(other: Any?): Boolean {
} }
return false return false
} }
fun Book.getExportFileName(): String {
val jsStr = AppConfig.bookExportFileName
if (jsStr.isNullOrBlank()) {
return "${name} 作者:${getRealAuthor()}"
}
val bindings = SimpleBindings()
bindings["name"] = name
bindings["author"] = getRealAuthor()
return kotlin.runCatching {
AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString()
}.onFailure {
AppLog.put("导出书名规则错误,使用默认规则\n${it.localizedMessage}", it)
}.getOrDefault("${name} 作者:${getRealAuthor()}")
}
@@ -534,11 +534,13 @@ class BookInfoActivity :
} else if (webFile.isSupportDecompress) { } else if (webFile.isSupportDecompress) {
/* 解压筛选后再选择导入项 */ /* 解压筛选后再选择导入项 */
viewModel.importOrDownloadWebFile<Uri>(webFile) { uri -> viewModel.importOrDownloadWebFile<Uri>(webFile) { uri ->
viewModel.deCompress(uri) { viewModel.deCompress(uri) { files ->
if (it.size == 1) { if (files.size == 1) {
viewModel.importBook(it[0]) viewModel.importBook(files[0]) {
onClick?.invoke(it)
}
} else { } else {
showDecompressFileImportAlert(it) showDecompressFileImportAlert(files)
} }
} }
} }
@@ -560,7 +562,8 @@ class BookInfoActivity :
} }
private fun showDecompressFileImportAlert( private fun showDecompressFileImportAlert(
files: List<File> files: List<File>,
success: ((Book) -> Unit)? = null
) { ) {
if (files.isEmpty()) { if (files.isEmpty()) {
toastOnUi(R.string.unsupport_archivefile_entry) toastOnUi(R.string.unsupport_archivefile_entry)
@@ -571,7 +574,9 @@ class BookInfoActivity :
R.string.import_select_book, R.string.import_select_book,
selectorNames selectorNames
) { _, _, index -> ) { _, _, index ->
viewModel.importBook(files[index]) viewModel.importBook(files[index]) {
success?.invoke(it)
}
} }
} }
@@ -287,18 +287,27 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
AppPattern.bookFileRegex.matches(it.name) AppPattern.bookFileRegex.matches(it.name)
} }
}.onError { }.onError {
AppLog.put("DeCompress Error:\n${it.localizedMessage}", it)
context.toastOnUi("DeCompress Error:\n${it.localizedMessage}") context.toastOnUi("DeCompress Error:\n${it.localizedMessage}")
}.onSuccess { }.onSuccess {
onSuccess.invoke(it) onSuccess.invoke(it)
} }
} }
fun importBook(file: File) { @Suppress("BlockingMethodInNonBlockingContext")
val uri = LocalBook.saveBookFile( fun importBook(file: File, success: ((Book) -> Unit)? = null) {
execute {
LocalBook.saveBookFile(
FileInputStream(file), FileInputStream(file),
file.name bookData.value!!.getExportFileName()
) )
changeToLocalBook(LocalBook.importFile(uri)) }.onSuccess {
val book = changeToLocalBook(LocalBook.importFile(it))
success?.invoke(book)
}.onError {
AppLog.put("ImportBook Error:\n${it.localizedMessage}", it)
context.toastOnUi("ImportBook Error:\n${it.localizedMessage}")
}
} }
fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) { fun changeTo(source: BookSource, book: Book, toc: List<BookChapter>) {
@@ -61,7 +61,7 @@ object RarUtils {
} }
@Throws(NullPointerException::class, SecurityException::class) @Throws(NullPointerException::class, SecurityException::class)
fun unRarToPath(archive: Archive, destDir: File?): List<File> { private fun unRarToPath(archive: Archive, destDir: File?): List<File> {
destDir ?: throw NullPointerException("解决路径不能为空") destDir ?: throw NullPointerException("解决路径不能为空")
val files = arrayListOf<File>() val files = arrayListOf<File>()
var entry: FileHeader? var entry: FileHeader?
@@ -68,7 +68,7 @@ object SevenZipUtils {
} }
@Throws(NullPointerException::class, SecurityException::class) @Throws(NullPointerException::class, SecurityException::class)
fun un7zToPath(sevenZFile: SevenZFile, destDir: File?): List<File> { private fun un7zToPath(sevenZFile: SevenZFile, destDir: File?): List<File> {
destDir ?: throw NullPointerException("解决路径不能为空") destDir ?: throw NullPointerException("解决路径不能为空")
val files = arrayListOf<File>() val files = arrayListOf<File>()
var entry: SevenZArchiveEntry? var entry: SevenZArchiveEntry?
@@ -4,6 +4,8 @@ import io.legado.app.utils.DebugLog
import io.legado.app.utils.printOnDebug import io.legado.app.utils.printOnDebug
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.apache.commons.compress.archivers.ArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream
import java.io.* import java.io.*
import java.util.zip.* import java.util.zip.*
@@ -192,22 +194,22 @@ object ZipUtils {
@Throws(SecurityException::class) @Throws(SecurityException::class)
fun unZipToPath(inputStream: InputStream, path: String): List<File> { fun unZipToPath(inputStream: InputStream, path: String): List<File> {
return ZipInputStream(inputStream).use { return ZipArchiveInputStream(inputStream).use {
unZipToPath(it, File(path)) unZipToPath(it, File(path))
} }
} }
@Throws(SecurityException::class) @Throws(SecurityException::class)
fun unZipToPath(inputStream: InputStream, dir: File): List<File> { fun unZipToPath(inputStream: InputStream, dir: File): List<File> {
return ZipInputStream(inputStream).use { return ZipArchiveInputStream(inputStream).use {
unZipToPath(it, dir) unZipToPath(it, dir)
} }
} }
@Throws(SecurityException::class) @Throws(SecurityException::class)
fun unZipToPath(zipInputStream: ZipInputStream, dir: File): List<File> { private fun unZipToPath(zipInputStream: ZipArchiveInputStream, dir: File): List<File> {
val files = arrayListOf<File>() val files = arrayListOf<File>()
var entry: ZipEntry? var entry: ArchiveEntry?
while (zipInputStream.nextEntry.also { entry = it } != null) { while (zipInputStream.nextEntry.also { entry = it } != null) {
val entryFile = File(dir, entry!!.name) val entryFile = File(dir, entry!!.name)
if (!entryFile.canonicalPath.startsWith(dir.canonicalPath)) { if (!entryFile.canonicalPath.startsWith(dir.canonicalPath)) {