缓存导出文件名扩展 #229
This commit is contained in:
@@ -32,6 +32,7 @@ import java.time.temporal.ChronoUnit
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
import androidx.core.net.toUri
|
||||
|
||||
|
||||
val Book.isAudio: Boolean
|
||||
@@ -113,7 +114,7 @@ fun Book.getLocalUri(): Uri {
|
||||
return uri
|
||||
}
|
||||
uri = if (bookUrl.isUri()) {
|
||||
Uri.parse(bookUrl)
|
||||
bookUrl.toUri()
|
||||
} else {
|
||||
Uri.fromFile(File(bookUrl))
|
||||
}
|
||||
@@ -148,7 +149,7 @@ fun Book.getLocalUri(): Uri {
|
||||
// 查找添加本地选择的目录
|
||||
if (!importBookDir.isNullOrBlank() && defaultBookDir != importBookDir) {
|
||||
val treeUri = if (importBookDir.isUri()) {
|
||||
Uri.parse(importBookDir)
|
||||
importBookDir.toUri()
|
||||
} else {
|
||||
Uri.fromFile(File(importBookDir))
|
||||
}
|
||||
@@ -170,7 +171,7 @@ fun Book.getLocalUri(): Uri {
|
||||
fun Book.getArchiveUri(): Uri? {
|
||||
val defaultBookDir = AppConfig.defaultBookTreeUri
|
||||
return if (isArchive && !defaultBookDir.isNullOrBlank()) {
|
||||
FileDoc.fromUri(Uri.parse(defaultBookDir), true)
|
||||
FileDoc.fromUri(defaultBookDir.toUri(), true)
|
||||
.find(archiveName)?.uri
|
||||
} else {
|
||||
null
|
||||
@@ -308,20 +309,43 @@ fun Book.isSameNameAuthor(other: Any?): Boolean {
|
||||
}
|
||||
|
||||
fun Book.getExportFileName(suffix: String): String {
|
||||
val jsStr = AppConfig.bookExportFileName
|
||||
if (jsStr.isNullOrBlank()) {
|
||||
val template = AppConfig.bookExportFileName
|
||||
if (template.isNullOrBlank()) {
|
||||
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")
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -509,10 +509,10 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
||||
@SuppressLint("SetTextI18n")
|
||||
private fun alertExportFileName() {
|
||||
alert(R.string.export_file_name) {
|
||||
val message = "Variable: name, author."
|
||||
val message = "支持变量:name(书籍名),author(作者),group(分组),source(书源名), 使用半角逗号分割,按照输入排序导出,其他字段视作备注。"
|
||||
setMessage(message)
|
||||
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
|
||||
editView.hint = "file name js"
|
||||
editLayout.hint = "name,author,xxx"
|
||||
editView.setText(AppConfig.bookExportFileName)
|
||||
}
|
||||
customView { alertBinding.root }
|
||||
@@ -523,6 +523,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun getTypeName(): String {
|
||||
return exportTypes.getOrElse(AppConfig.exportType) {
|
||||
exportTypes[0]
|
||||
|
||||
Reference in New Issue
Block a user