mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
package io.legado.app.help.book
|
||||
|
||||
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.BookType
|
||||
import io.legado.app.data.appDb
|
||||
@@ -201,4 +204,19 @@ fun Book.isSameNameAuthor(other: Any?): Boolean {
|
||||
return name == other.name && author == other.author
|
||||
}
|
||||
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) {
|
||||
/* 解压筛选后再选择导入项 */
|
||||
viewModel.importOrDownloadWebFile<Uri>(webFile) { uri ->
|
||||
viewModel.deCompress(uri) {
|
||||
if (it.size == 1) {
|
||||
viewModel.importBook(it[0])
|
||||
viewModel.deCompress(uri) { files ->
|
||||
if (files.size == 1) {
|
||||
viewModel.importBook(files[0]) {
|
||||
onClick?.invoke(it)
|
||||
}
|
||||
} else {
|
||||
showDecompressFileImportAlert(it)
|
||||
showDecompressFileImportAlert(files)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -560,7 +562,8 @@ class BookInfoActivity :
|
||||
}
|
||||
|
||||
private fun showDecompressFileImportAlert(
|
||||
files: List<File>
|
||||
files: List<File>,
|
||||
success: ((Book) -> Unit)? = null
|
||||
) {
|
||||
if (files.isEmpty()) {
|
||||
toastOnUi(R.string.unsupport_archivefile_entry)
|
||||
@@ -571,7 +574,9 @@ class BookInfoActivity :
|
||||
R.string.import_select_book,
|
||||
selectorNames
|
||||
) { _, _, 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)
|
||||
}
|
||||
}.onError {
|
||||
AppLog.put("DeCompress Error:\n${it.localizedMessage}", it)
|
||||
context.toastOnUi("DeCompress Error:\n${it.localizedMessage}")
|
||||
}.onSuccess {
|
||||
onSuccess.invoke(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun importBook(file: File) {
|
||||
val uri = LocalBook.saveBookFile(
|
||||
FileInputStream(file),
|
||||
file.name
|
||||
)
|
||||
changeToLocalBook(LocalBook.importFile(uri))
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
fun importBook(file: File, success: ((Book) -> Unit)? = null) {
|
||||
execute {
|
||||
LocalBook.saveBookFile(
|
||||
FileInputStream(file),
|
||||
bookData.value!!.getExportFileName()
|
||||
)
|
||||
}.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>) {
|
||||
|
||||
@@ -61,7 +61,7 @@ object RarUtils {
|
||||
}
|
||||
|
||||
@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("解决路径不能为空")
|
||||
val files = arrayListOf<File>()
|
||||
var entry: FileHeader?
|
||||
|
||||
@@ -68,7 +68,7 @@ object SevenZipUtils {
|
||||
}
|
||||
|
||||
@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("解决路径不能为空")
|
||||
val files = arrayListOf<File>()
|
||||
var entry: SevenZArchiveEntry?
|
||||
|
||||
@@ -4,6 +4,8 @@ import io.legado.app.utils.DebugLog
|
||||
import io.legado.app.utils.printOnDebug
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.apache.commons.compress.archivers.ArchiveEntry
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream
|
||||
import java.io.*
|
||||
import java.util.zip.*
|
||||
|
||||
@@ -192,22 +194,22 @@ object ZipUtils {
|
||||
|
||||
@Throws(SecurityException::class)
|
||||
fun unZipToPath(inputStream: InputStream, path: String): List<File> {
|
||||
return ZipInputStream(inputStream).use {
|
||||
return ZipArchiveInputStream(inputStream).use {
|
||||
unZipToPath(it, File(path))
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(SecurityException::class)
|
||||
fun unZipToPath(inputStream: InputStream, dir: File): List<File> {
|
||||
return ZipInputStream(inputStream).use {
|
||||
return ZipArchiveInputStream(inputStream).use {
|
||||
unZipToPath(it, dir)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(SecurityException::class)
|
||||
fun unZipToPath(zipInputStream: ZipInputStream, dir: File): List<File> {
|
||||
private fun unZipToPath(zipInputStream: ZipArchiveInputStream, dir: File): List<File> {
|
||||
val files = arrayListOf<File>()
|
||||
var entry: ZipEntry?
|
||||
var entry: ArchiveEntry?
|
||||
while (zipInputStream.nextEntry.also { entry = it } != null) {
|
||||
val entryFile = File(dir, entry!!.name)
|
||||
if (!entryFile.canonicalPath.startsWith(dir.canonicalPath)) {
|
||||
|
||||
Reference in New Issue
Block a user