From ef33753cf62db28298b06725d243fe5662ffff79 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 3 Nov 2020 08:37:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/entities/Book.kt | 32 +++++++++++-------- .../app/model/localBook/AnalyzeTxtFile.kt | 10 ++---- .../legado/app/model/localBook/LocalBook.kt | 11 +++++-- 3 files changed, 29 insertions(+), 24 deletions(-) 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 0d3ef747c..bc25cf242 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 @@ -14,6 +14,7 @@ import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.Parcelize import java.nio.charset.Charset import kotlin.math.max +import kotlin.math.min @Parcelize @TypeConverters(Book.Converters::class) @@ -53,55 +54,55 @@ data class Book( var originOrder: Int = 0, //书源排序 var variable: String? = null, // 自定义书籍变量信息(用于书源规则检索书籍信息) var readConfig: ReadConfig? = null -): Parcelable, BaseBook { - +) : Parcelable, BaseBook { + fun isLocalBook(): Boolean { return origin == BookType.local } - + fun isLocalTxt(): Boolean { return isLocalBook() && originName.endsWith(".txt", true) } - + fun isEpub(): Boolean { return originName.endsWith(".epub", true) } - + fun isOnLineTxt(): Boolean { return !isLocalBook() && type == 0 } - + override fun equals(other: Any?): Boolean { if (other is Book) { return other.bookUrl == bookUrl } return false } - + override fun hashCode(): Int { return bookUrl.hashCode() } - + @delegate:Transient @delegate:Ignore @IgnoredOnParcel override val variableMap by lazy { GSON.fromJsonObject>(variable) ?: HashMap() } - + override fun putVariable(key: String, value: String) { variableMap[key] = value variable = GSON.toJson(variableMap) } - + @Ignore @IgnoredOnParcel override var infoHtml: String? = null - + @Ignore @IgnoredOnParcel override var tocHtml: String? = null - + fun getRealAuthor() = author.replace(AppPattern.authorRegex, "") fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0) @@ -146,7 +147,10 @@ data class Book( } fun getFolderName(): String { - return name.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(bookUrl) + //防止书名过长,只取9位 + var folderName = name.replace(AppPattern.fileNameRegex, "") + folderName = folderName.substring(0, min(9, folderName.length)) + return folderName + MD5Utils.md5Encode16(bookUrl) } fun toSearchBook() = SearchBook( @@ -168,7 +172,7 @@ data class Book( this.infoHtml = this@Book.infoHtml this.tocHtml = this@Book.tocHtml } - + fun changeTo(newBook: Book) { newBook.group = group newBook.order = order diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt index 77689d8a5..76776b090 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt @@ -239,7 +239,7 @@ class AnalyzeTxtFile { } companion object { - private const val folderName = "bookTxt" + private const val BLANK: Byte = 0x0a //默认从文件中获取数据的长度 @@ -247,12 +247,6 @@ class AnalyzeTxtFile { //没有标题的时候,每个章节的最大长度 private const val MAX_LENGTH_WITH_NO_CHAPTER = 10 * 1024 - val cacheFolder: File by lazy { - val rootFile = App.INSTANCE.getExternalFilesDir(null) - ?: App.INSTANCE.externalCacheDir - ?: App.INSTANCE.cacheDir - FileUtils.createFolderIfNotExist(rootFile, folderName) - } fun getContent(book: Book, bookChapter: BookChapter): String { val bookFile = getBookFile(book) @@ -269,7 +263,7 @@ class AnalyzeTxtFile { private fun getBookFile(book: Book): File { if (book.bookUrl.isContentPath()) { val uri = Uri.parse(book.bookUrl) - val bookFile = FileUtils.getFile(cacheFolder, book.originName) + val bookFile = FileUtils.getFile(LocalBook.cacheFolder, book.originName) if (!bookFile.exists()) { bookFile.createNewFile() DocumentUtils.readBytes(App.INSTANCE, uri)?.let { diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 9545cf7cb..5d01504ef 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -11,6 +11,13 @@ import java.io.File object LocalBook { + private const val folderName = "bookTxt" + val cacheFolder: File by lazy { + val rootFile = App.INSTANCE.getExternalFilesDir(null) + ?: App.INSTANCE.externalCacheDir + ?: App.INSTANCE.cacheDir + FileUtils.createFolderIfNotExist(rootFile, folderName) + } fun getChapterList(book: Book): ArrayList { return if (book.isEpub()) { @@ -34,7 +41,7 @@ object LocalBook { path = uri.toString() val doc = DocumentFile.fromSingleUri(App.INSTANCE, uri) doc?.let { - val bookFile = FileUtils.getFile(AnalyzeTxtFile.cacheFolder, it.name!!) + val bookFile = FileUtils.getFile(cacheFolder, it.name!!) if (!bookFile.exists()) { bookFile.createNewFile() doc.readBytes(App.INSTANCE)?.let { bytes -> @@ -82,7 +89,7 @@ object LocalBook { fun deleteBook(book: Book, deleteOriginal: Boolean) { kotlin.runCatching { if (book.isLocalTxt()) { - val bookFile = FileUtils.getFile(AnalyzeTxtFile.cacheFolder, book.originName) + val bookFile = FileUtils.getFile(cacheFolder, book.originName) bookFile.delete() }