This commit is contained in:
kunfei
2023-03-14 13:20:39 +08:00
parent ccda998fae
commit 91d7d49424
3 changed files with 12 additions and 27 deletions
@@ -206,17 +206,17 @@ fun Book.isSameNameAuthor(other: Any?): Boolean {
return false return false
} }
fun Book.getExportFileName(): String { fun Book.getExportFileName(suffix: String): String {
val jsStr = AppConfig.bookExportFileName val jsStr = AppConfig.bookExportFileName
if (jsStr.isNullOrBlank()) { if (jsStr.isNullOrBlank()) {
return "${name} 作者:${getRealAuthor()}" return "${name} 作者:${getRealAuthor()}.$suffix"
} }
val bindings = SimpleBindings() val bindings = SimpleBindings()
bindings["name"] = name bindings["name"] = name
bindings["author"] = getRealAuthor() bindings["author"] = getRealAuthor()
return kotlin.runCatching { return kotlin.runCatching {
AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString() AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString() + "." + suffix
}.onFailure { }.onFailure {
AppLog.put("导出书名规则错误,使用默认规则\n${it.localizedMessage}", it) 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.Glide
import com.bumptech.glide.request.target.CustomTarget import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition import com.bumptech.glide.request.transition.Transition
import com.script.SimpleBindings
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb 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.AppWebDav
import io.legado.app.help.book.BookHelp import io.legado.app.help.book.BookHelp
import io.legado.app.help.book.ContentProcessor 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.book.isLocal
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.coroutine.Coroutine 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 { fun exportFileExist(path: String, book: Book): Boolean {
val fileName = getExportFileName(book) val fileName = book.getExportFileName("txt")
return if (path.isContentScheme()) { return if (path.isContentScheme()) {
val uri = Uri.parse(path) val uri = Uri.parse(path)
val doc = DocumentFile.fromTreeUri(context, uri) ?: return false val doc = DocumentFile.fromTreeUri(context, uri) ?: return false
@@ -141,7 +125,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
private suspend fun export(doc: DocumentFile, book: Book) { private suspend fun export(doc: DocumentFile, book: Book) {
val filename = "${getExportFileName(book)}.txt" val filename = book.getExportFileName("txt")
DocumentUtils.delete(doc, filename) DocumentUtils.delete(doc, filename)
val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename) val bookDoc = DocumentUtils.createFileIfNotExist(doc, filename)
?: throw NoStackTraceException("创建文档失败,请尝试重新设置导出文件夹") ?: throw NoStackTraceException("创建文档失败,请尝试重新设置导出文件夹")
@@ -167,7 +151,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
} }
private suspend fun export(file: File, book: Book) { 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 bookPath = FileUtils.getPath(file, filename)
val bookFile = FileUtils.createFileWithReplace(bookPath) val bookFile = FileUtils.createFileWithReplace(bookPath)
getAllContents(book) { text, srcList -> getAllContents(book) { text, srcList ->
@@ -305,7 +289,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
private suspend fun exportEpub(doc: DocumentFile, book: Book) { private suspend fun exportEpub(doc: DocumentFile, book: Book) {
val filename = "${getExportFileName(book)}.epub" val filename = book.getExportFileName("epub")
DocumentUtils.delete(doc, filename) DocumentUtils.delete(doc, filename)
val epubBook = EpubBook() val epubBook = EpubBook()
epubBook.version = "2.0" epubBook.version = "2.0"
@@ -331,7 +315,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
private suspend fun exportEpub(file: File, book: Book) { private suspend fun exportEpub(file: File, book: Book) {
val filename = "${getExportFileName(book)}.epub" val filename = book.getExportFileName("epub")
val epubBook = EpubBook() val epubBook = EpubBook()
epubBook.version = "2.0" epubBook.version = "2.0"
//set metadata //set metadata
@@ -297,9 +297,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
fun importBook(file: File, success: ((Book) -> Unit)? = null) { fun importBook(file: File, success: ((Book) -> Unit)? = null) {
execute { execute {
val suffix = file.name.substringAfterLast(".")
LocalBook.saveBookFile( LocalBook.saveBookFile(
FileInputStream(file), FileInputStream(file),
bookData.value!!.getExportFileName() bookData.value!!.getExportFileName(suffix)
) )
}.onSuccess { }.onSuccess {
val book = changeToLocalBook(LocalBook.importFile(it)) val book = changeToLocalBook(LocalBook.importFile(it))