diff --git a/app/src/main/java/io/legado/app/constant/AppPattern.kt b/app/src/main/java/io/legado/app/constant/AppPattern.kt
index e9a508939..d31e69f23 100644
--- a/app/src/main/java/io/legado/app/constant/AppPattern.kt
+++ b/app/src/main/java/io/legado/app/constant/AppPattern.kt
@@ -6,8 +6,11 @@ object AppPattern {
val JS_PATTERN: Pattern =
Pattern.compile("([\\w\\W]*?|@js:[\\w\\W]*$)", Pattern.CASE_INSENSITIVE)
val EXP_PATTERN: Pattern = Pattern.compile("\\{\\{([\\w\\W]*?)\\}\\}")
+ val imgPattern =
+ Pattern.compile("", Pattern.CASE_INSENSITIVE)
- val authorRegex = "作\\s*者\\s*[::]".toRegex()
-
+ val nameRegex = Regex("\\s+作\\s*者.*")
+ val authorRegex = Regex(".*?作\\s*?者[::]")
+ val fileNameRegex = Regex("[\\\\/:*?\"<>|.]")
val splitGroupRegex = Regex("[,;]")
}
\ No newline at end of file
diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt
index 33d5d309b..c6759c237 100644
--- a/app/src/main/java/io/legado/app/data/entities/Book.kt
+++ b/app/src/main/java/io/legado/app/data/entities/Book.kt
@@ -11,6 +11,7 @@ import io.legado.app.constant.BookType
import io.legado.app.help.AppConfig
import io.legado.app.service.help.ReadBook
import io.legado.app.utils.GSON
+import io.legado.app.utils.MD5Utils
import io.legado.app.utils.fromJsonObject
import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize
@@ -115,6 +116,10 @@ data class Book(
return charset(charset ?: "UTF-8")
}
+ fun getFolderName(): String {
+ return name.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(bookUrl)
+ }
+
fun toSearchBook(): SearchBook {
return SearchBook(
name = name,
diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt
index 4f25f85ed..b0e5575f8 100644
--- a/app/src/main/java/io/legado/app/help/BookHelp.kt
+++ b/app/src/main/java/io/legado/app/help/BookHelp.kt
@@ -2,6 +2,7 @@ package io.legado.app.help
import com.hankcs.hanlp.HanLP
import io.legado.app.App
+import io.legado.app.constant.AppPattern
import io.legado.app.constant.EventBus
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
@@ -16,19 +17,12 @@ import kotlinx.coroutines.withContext
import org.apache.commons.text.similarity.JaccardSimilarity
import org.jetbrains.anko.toast
import java.io.File
-import java.util.regex.Pattern
import kotlin.math.min
object BookHelp {
private const val cacheFolderName = "book_cache"
private const val cacheImageFolderName = "images"
private val downloadDir: File = App.INSTANCE.externalFilesDir
- private val srcPattern =
- Pattern.compile("", Pattern.CASE_INSENSITIVE)
-
- fun bookFolderName(book: Book): String {
- return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl)
- }
fun formatChapterName(bookChapter: BookChapter): String {
return String.format(
@@ -51,7 +45,7 @@ object BookHelp {
Coroutine.async {
val bookFolderNames = arrayListOf()
App.db.bookDao().all.forEach {
- bookFolderNames.add(bookFolderName(it))
+ bookFolderNames.add(it.getFolderName())
}
val file = FileUtils.getDirFile(downloadDir, cacheFolderName)
file.listFiles()?.forEach { bookFile ->
@@ -69,11 +63,11 @@ object BookHelp {
FileUtils.createFileIfNotExist(
downloadDir,
formatChapterName(bookChapter),
- subDirs = arrayOf(cacheFolderName, bookFolderName(book))
+ subDirs = arrayOf(cacheFolderName, book.getFolderName())
).writeText(content)
//保存图片
content.split("\n").forEach {
- val matcher = srcPattern.matcher(it)
+ val matcher = AppPattern.imgPattern.matcher(it)
if (matcher.find()) {
var src = matcher.group(1)
src = NetworkUtils.getAbsoluteURL(bookChapter.url, src)
@@ -91,7 +85,7 @@ object BookHelp {
FileUtils.createFileIfNotExist(
downloadDir,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}",
- subDirs = arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName)
+ subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName)
).writeBytes(it)
}
}
@@ -100,7 +94,7 @@ object BookHelp {
return FileUtils.getFile(
downloadDir,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}",
- subDirs = arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName)
+ subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName)
)
}
@@ -116,7 +110,7 @@ object BookHelp {
val fileNameList = arrayListOf()
FileUtils.createFolderIfNotExist(
downloadDir,
- subDirs = arrayOf(cacheFolderName, bookFolderName(book))
+ subDirs = arrayOf(cacheFolderName, book.getFolderName())
).list()?.let {
fileNameList.addAll(it)
}
@@ -130,7 +124,7 @@ object BookHelp {
FileUtils.exists(
downloadDir,
formatChapterName(bookChapter),
- subDirs = arrayOf(cacheFolderName, bookFolderName(book))
+ subDirs = arrayOf(cacheFolderName, book.getFolderName())
)
}
}
@@ -142,7 +136,7 @@ object BookHelp {
val file = FileUtils.getFile(
downloadDir,
formatChapterName(bookChapter),
- subDirs = arrayOf(cacheFolderName, bookFolderName(book))
+ subDirs = arrayOf(cacheFolderName, book.getFolderName())
)
if (file.exists()) {
return file.readText()
@@ -158,24 +152,20 @@ object BookHelp {
FileUtils.createFileIfNotExist(
downloadDir,
formatChapterName(bookChapter),
- subDirs = arrayOf(cacheFolderName, bookFolderName(book))
+ subDirs = arrayOf(cacheFolderName, book.getFolderName())
).delete()
}
}
- private fun formatFolderName(folderName: String): String {
- return folderName.replace("[\\\\/:*?\"<>|.]".toRegex(), "")
- }
-
fun formatBookName(name: String): String {
return name
- .replace("\\s+作\\s*者.*".toRegex(), "")
+ .replace(AppPattern.nameRegex, "")
.trim { it <= ' ' }
}
fun formatBookAuthor(author: String): String {
return author
- .replace(".*?作\\s*?者[::]".toRegex(), "")
+ .replace(AppPattern.authorRegex, "")
.trim { it <= ' ' }
}
diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
index 3dbc0a5e1..849c69b84 100644
--- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
+++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
@@ -7,6 +7,7 @@ import android.text.StaticLayout
import android.text.TextPaint
import android.text.TextUtils
import io.legado.app.App
+import io.legado.app.constant.AppPattern
import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
@@ -18,7 +19,6 @@ import io.legado.app.ui.book.read.page.entities.TextLine
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.*
import java.util.*
-import java.util.regex.Pattern
@Suppress("DEPRECATION")
@@ -38,8 +38,6 @@ object ChapterProvider {
var typeface: Typeface = Typeface.SANS_SERIF
lateinit var titlePaint: TextPaint
lateinit var contentPaint: TextPaint
- private val srcPattern =
- Pattern.compile("", Pattern.CASE_INSENSITIVE)
init {
upStyle()
@@ -62,7 +60,7 @@ object ChapterProvider {
var durY = 0f
textPages.add(TextPage())
contents.forEachIndexed { index, text ->
- val matcher = srcPattern.matcher(text)
+ val matcher = AppPattern.imgPattern.matcher(text)
if (matcher.find()) {
var src = matcher.group(1)
if (!book.isEpub()) {