Explorar o código

缓存导出文件名扩展 #229

HapeLee hai 1 ano
pai
achega
610744754b

+ 38 - 14
app/src/main/java/io/legado/app/help/book/BookExtensions.kt

@@ -32,6 +32,7 @@ import java.time.temporal.ChronoUnit
 import java.util.concurrent.ConcurrentHashMap
 import java.util.concurrent.ConcurrentHashMap
 import kotlin.math.max
 import kotlin.math.max
 import kotlin.math.min
 import kotlin.math.min
+import androidx.core.net.toUri
 
 
 
 
 val Book.isAudio: Boolean
 val Book.isAudio: Boolean
@@ -113,7 +114,7 @@ fun Book.getLocalUri(): Uri {
         return uri
         return uri
     }
     }
     uri = if (bookUrl.isUri()) {
     uri = if (bookUrl.isUri()) {
-        Uri.parse(bookUrl)
+        bookUrl.toUri()
     } else {
     } else {
         Uri.fromFile(File(bookUrl))
         Uri.fromFile(File(bookUrl))
     }
     }
@@ -148,7 +149,7 @@ fun Book.getLocalUri(): Uri {
     // 查找添加本地选择的目录
     // 查找添加本地选择的目录
     if (!importBookDir.isNullOrBlank() && defaultBookDir != importBookDir) {
     if (!importBookDir.isNullOrBlank() && defaultBookDir != importBookDir) {
         val treeUri = if (importBookDir.isUri()) {
         val treeUri = if (importBookDir.isUri()) {
-            Uri.parse(importBookDir)
+            importBookDir.toUri()
         } else {
         } else {
             Uri.fromFile(File(importBookDir))
             Uri.fromFile(File(importBookDir))
         }
         }
@@ -170,7 +171,7 @@ fun Book.getLocalUri(): Uri {
 fun Book.getArchiveUri(): Uri? {
 fun Book.getArchiveUri(): Uri? {
     val defaultBookDir = AppConfig.defaultBookTreeUri
     val defaultBookDir = AppConfig.defaultBookTreeUri
     return if (isArchive && !defaultBookDir.isNullOrBlank()) {
     return if (isArchive && !defaultBookDir.isNullOrBlank()) {
-        FileDoc.fromUri(Uri.parse(defaultBookDir), true)
+        FileDoc.fromUri(defaultBookDir.toUri(), true)
             .find(archiveName)?.uri
             .find(archiveName)?.uri
     } else {
     } else {
         null
         null
@@ -308,20 +309,43 @@ fun Book.isSameNameAuthor(other: Any?): Boolean {
 }
 }
 
 
 fun Book.getExportFileName(suffix: String): String {
 fun Book.getExportFileName(suffix: String): String {
-    val jsStr = AppConfig.bookExportFileName
-    if (jsStr.isNullOrBlank()) {
+    val template = AppConfig.bookExportFileName
+    if (template.isNullOrBlank()) {
         return "$name 作者:${getRealAuthor()}.$suffix"
         return "$name 作者:${getRealAuthor()}.$suffix"
     }
     }
-    val bindings = buildScriptBindings { bindings ->
-        bindings["epubIndex"] = ""// 兼容老版本,修复可能存在的错误
-        bindings["name"] = name
-        bindings["author"] = getRealAuthor()
+    return try {
+        val fields = template.split(",").map { it.trim() }.filter { it.isNotEmpty() }
+        val parts = fields.map { field ->
+            when (field.lowercase()) {
+                "name" -> name
+                "author" -> getRealAuthor()
+                "group" -> group
+                "source" -> originName
+                else -> field
+            }
+        }
+        val fileName = buildString {
+            var prevIsVariable = false
+            parts.forEachIndexed { index, part ->
+                val isVariable = part == name || part == getRealAuthor() || part == group || part == originName
+
+                if (index > 0) {
+                    if (prevIsVariable && isVariable) {
+                        append("-")
+                    } else if (!isVariable) {
+                        append("_")
+                    }
+                }
+
+                append(part)
+                prevIsVariable = isVariable
+            }
+        }
+        "$fileName.$suffix"
+    } catch (e: Exception) {
+        AppLog.put("导出书名规则错误,使用默认规则\n${e.localizedMessage}", e)
+        "$name 作者:${getRealAuthor()}.$suffix"
     }
     }
-    return kotlin.runCatching {
-        RhinoScriptEngine.eval(jsStr, bindings).toString() + "." + suffix
-    }.onFailure {
-        AppLog.put("导出书名规则错误,使用默认规则\n${it.localizedMessage}", it)
-    }.getOrDefault("$name 作者:${getRealAuthor()}.$suffix")
 }
 }
 
 
 /**
 /**

+ 3 - 2
app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt

@@ -509,10 +509,10 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
     @SuppressLint("SetTextI18n")
     @SuppressLint("SetTextI18n")
     private fun alertExportFileName() {
     private fun alertExportFileName() {
         alert(R.string.export_file_name) {
         alert(R.string.export_file_name) {
-            val message = "Variable: name, author."
+            val message = "支持变量:name(书籍名),author(作者),group(分组),source(书源名), 使用半角逗号分割,按照输入排序导出,其他字段视作备注。"
             setMessage(message)
             setMessage(message)
             val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
             val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
-                editView.hint = "file name js"
+                editLayout.hint = "name,author,xxx"
                 editView.setText(AppConfig.bookExportFileName)
                 editView.setText(AppConfig.bookExportFileName)
             }
             }
             customView { alertBinding.root }
             customView { alertBinding.root }
@@ -523,6 +523,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
         }
         }
     }
     }
 
 
+
     private fun getTypeName(): String {
     private fun getTypeName(): String {
         return exportTypes.getOrElse(AppConfig.exportType) {
         return exportTypes.getOrElse(AppConfig.exportType) {
             exportTypes[0]
             exportTypes[0]

+ 1 - 1
app/version.properties

@@ -1,4 +1,4 @@
 VERSION_MAJOR=3
 VERSION_MAJOR=3
 VERSION_MINOR=26
 VERSION_MINOR=26
 VERSION_PATCH=3
 VERSION_PATCH=3
-VERSION_SUFFIX=-beta.3
+VERSION_SUFFIX=-beta.4