mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -206,17 +206,17 @@ fun Book.isSameNameAuthor(other: Any?): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
fun Book.getExportFileName(): String {
|
||||
fun Book.getExportFileName(suffix: String): String {
|
||||
val jsStr = AppConfig.bookExportFileName
|
||||
if (jsStr.isNullOrBlank()) {
|
||||
return "${name} 作者:${getRealAuthor()}"
|
||||
return "${name} 作者:${getRealAuthor()}.$suffix"
|
||||
}
|
||||
val bindings = SimpleBindings()
|
||||
bindings["name"] = name
|
||||
bindings["author"] = getRealAuthor()
|
||||
return kotlin.runCatching {
|
||||
AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString()
|
||||
AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString() + "." + suffix
|
||||
}.onFailure {
|
||||
AppLog.put("导出书名规则错误,使用默认规则\n${it.localizedMessage}", it)
|
||||
}.getOrDefault("${name} 作者:${getRealAuthor()}")
|
||||
}.getOrDefault("${name} 作者:${getRealAuthor()}.$suffix")
|
||||
}
|
||||
@@ -9,10 +9,8 @@ import androidx.lifecycle.MutableLiveData
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.request.target.CustomTarget
|
||||
import com.bumptech.glide.request.transition.Transition
|
||||
import com.script.SimpleBindings
|
||||
import io.legado.app.R
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.constant.AppConst
|
||||
import io.legado.app.constant.AppLog
|
||||
import io.legado.app.constant.AppPattern
|
||||
import io.legado.app.data.appDb
|
||||
@@ -22,6 +20,7 @@ import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.help.AppWebDav
|
||||
import io.legado.app.help.book.BookHelp
|
||||
import io.legado.app.help.book.ContentProcessor
|
||||
import io.legado.app.help.book.getExportFileName
|
||||
import io.legado.app.help.book.isLocal
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.help.coroutine.Coroutine
|
||||
@@ -78,23 +77,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getExportFileName(book: Book): String {
|
||||
val jsStr = AppConfig.bookExportFileName
|
||||
if (jsStr.isNullOrBlank()) {
|
||||
return "${book.name} 作者:${book.getRealAuthor()}"
|
||||
}
|
||||
val bindings = SimpleBindings()
|
||||
bindings["name"] = book.name
|
||||
bindings["author"] = book.getRealAuthor()
|
||||
return kotlin.runCatching {
|
||||
AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString()
|
||||
}.onFailure {
|
||||
context.toastOnUi("书名规则错误\n${it.localizedMessage}")
|
||||
}.getOrDefault("${book.name} 作者:${book.getRealAuthor()}")
|
||||
}
|
||||
|
||||
fun exportFileExist(path: String, book: Book): Boolean {
|
||||
val fileName = getExportFileName(book)
|
||||
val fileName = book.getExportFileName("txt")
|
||||
return if (path.isContentScheme()) {
|
||||
val uri = Uri.parse(path)
|
||||
val doc = DocumentFile.fromTreeUri(context, uri) ?: return false
|
||||
@@ -141,7 +125,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
private suspend fun export(doc: DocumentFile, book: Book) {
|
||||
val filename = "${getExportFileName(book)}.txt"
|
||||
val filename = book.getExportFileName("txt")
|
||||
DocumentUtils.delete(doc, filename)
|
||||
val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename)
|
||||
?: throw NoStackTraceException("创建文档失败,请尝试重新设置导出文件夹")
|
||||
@@ -167,7 +151,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
|
||||
private suspend fun export(file: File, book: Book) {
|
||||
val filename = "${getExportFileName(book)}.txt"
|
||||
val filename = book.getExportFileName("txt")
|
||||
val bookPath = FileUtils.getPath(file, filename)
|
||||
val bookFile = FileUtils.createFileWithReplace(bookPath)
|
||||
getAllContents(book) { text, srcList ->
|
||||
@@ -305,7 +289,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
private suspend fun exportEpub(doc: DocumentFile, book: Book) {
|
||||
val filename = "${getExportFileName(book)}.epub"
|
||||
val filename = book.getExportFileName("epub")
|
||||
DocumentUtils.delete(doc, filename)
|
||||
val epubBook = EpubBook()
|
||||
epubBook.version = "2.0"
|
||||
@@ -331,7 +315,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
|
||||
private suspend fun exportEpub(file: File, book: Book) {
|
||||
val filename = "${getExportFileName(book)}.epub"
|
||||
val filename = book.getExportFileName("epub")
|
||||
val epubBook = EpubBook()
|
||||
epubBook.version = "2.0"
|
||||
//set metadata
|
||||
|
||||
@@ -297,9 +297,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
fun importBook(file: File, success: ((Book) -> Unit)? = null) {
|
||||
execute {
|
||||
val suffix = file.name.substringAfterLast(".")
|
||||
LocalBook.saveBookFile(
|
||||
FileInputStream(file),
|
||||
bookData.value!!.getExportFileName()
|
||||
bookData.value!!.getExportFileName(suffix)
|
||||
)
|
||||
}.onSuccess {
|
||||
val book = changeToLocalBook(LocalBook.importFile(it))
|
||||
|
||||
Reference in New Issue
Block a user