From 993909114a3f0567d820c6a9594ecb47ff605d6d Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 16 Jul 2021 09:29:55 +0800 Subject: [PATCH] =?UTF-8?q?js=E5=8F=AA=E8=83=BD=E8=AE=BF=E9=97=AEcache?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E4=B8=8B=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 2 +- .../java/io/legado/app/help/JsExtensions.kt | 93 ++++++++++--------- .../io/legado/app/model/localBook/EpubFile.kt | 4 +- .../legado/app/model/localBook/LocalBook.kt | 2 +- .../io/legado/app/model/localBook/UmdFile.kt | 7 +- .../ui/book/info/edit/BookInfoEditActivity.kt | 4 +- .../ui/book/read/config/BgTextConfigDialog.kt | 18 ++-- .../app/ui/config/OtherConfigFragment.kt | 4 +- .../app/ui/config/ThemeConfigFragment.kt | 4 +- .../app/ui/widget/font/FontSelectDialog.kt | 2 +- .../io/legado/app/utils/ContextExtensions.kt | 4 +- .../java/io/legado/app/utils/FileUtils.kt | 3 +- 12 files changed, 74 insertions(+), 73 deletions(-) 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 41dc7376e..1cf95b2f2 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -23,7 +23,7 @@ import kotlin.math.min object BookHelp { private const val cacheFolderName = "book_cache" private const val cacheImageFolderName = "images" - private val downloadDir: File = appCtx.externalFilesDir + private val downloadDir: File = appCtx.externalFiles private val downloadImages = CopyOnWriteArraySet() fun clearCache() { diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 084058044..04915cf77 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -85,6 +85,7 @@ interface JsExtensions { /** * 实现16进制字符串转文件 + * @return 相对路径 */ fun downloadFile(content: String, url: String): String { val type = AnalyzeUrl(url).type ?: return "" @@ -99,45 +100,7 @@ interface JsExtensions { zipFile.writeBytes(it) } } - return zipPath - } - - /** - * js实现压缩文件解压 - */ - fun unzipFile(zipPath: String): String { - if (zipPath.isEmpty()) return "" - val unzipPath = FileUtils.getPath( - FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), - FileUtils.getNameExcludeExtension(zipPath) - ) - FileUtils.deleteFile(unzipPath) - val zipFile = FileUtils.createFileIfNotExist(zipPath) - val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath) - ZipUtils.unzipFile(zipFile, unzipFolder) - FileUtils.deleteFile(zipPath) - return unzipPath - } - - /** - * js实现文件夹内所有文件读取 - */ - fun getTxtInFolder(unzipPath: String): String { - if (unzipPath.isEmpty()) return "" - val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath) - val contents = StringBuilder() - unzipFolder.listFiles().let { - if (it != null) { - for (f in it) { - val charsetName = EncodingDetect.getEncode(f) - contents.append(String(f.readBytes(), charset(charsetName))) - .append("\n") - } - contents.deleteCharAt(contents.length - 1) - } - } - FileUtils.deleteFile(unzipPath) - return contents.toString() + return zipPath.substring(FileUtils.getCachePath().length) } /** @@ -263,7 +226,7 @@ interface JsExtensions { * @return File */ fun getFile(path: String): File { - val cachePath = appCtx.eCacheDir.path + val cachePath = appCtx.externalCache.absolutePath val aPath = if (path.startsWith(File.separator)) { cachePath + path } else { @@ -306,13 +269,43 @@ interface JsExtensions { } /** - * 解析字体,返回字体解析类 + * js实现压缩文件解压 + * @param zipPath 相对路径 + * @return 相对路径 */ - fun queryBase64TTF(base64: String?): QueryTTF? { - base64DecodeToByteArray(base64)?.let { - return QueryTTF(it) + fun unzipFile(zipPath: String): String { + if (zipPath.isEmpty()) return "" + val unzipPath = FileUtils.getPath( + FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), + FileUtils.getNameExcludeExtension(zipPath) + ) + FileUtils.deleteFile(unzipPath) + val zipFile = getFile(zipPath) + val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath) + ZipUtils.unzipFile(zipFile, unzipFolder) + FileUtils.deleteFile(zipPath) + return unzipPath.substring(FileUtils.getCachePath().length) + } + + /** + * js实现文件夹内所有文件读取 + */ + fun getTxtInFolder(unzipPath: String): String { + if (unzipPath.isEmpty()) return "" + val unzipFolder = getFile(unzipPath) + val contents = StringBuilder() + unzipFolder.listFiles().let { + if (it != null) { + for (f in it) { + val charsetName = EncodingDetect.getEncode(f) + contents.append(String(f.readBytes(), charset(charsetName))) + .append("\n") + } + contents.deleteCharAt(contents.length - 1) + } } - return null + FileUtils.deleteFile(unzipPath) + return contents.toString() } /** @@ -368,6 +361,16 @@ interface JsExtensions { return null } + /** + * 解析字体,返回字体解析类 + */ + fun queryBase64TTF(base64: String?): QueryTTF? { + base64DecodeToByteArray(base64)?.let { + return QueryTTF(it) + } + return null + } + /** * 返回字体解析类 * @param str 支持url,本地文件,base64,自动判断,自动缓存 diff --git a/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt b/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt index 5365be900..93f96132f 100644 --- a/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt @@ -9,7 +9,7 @@ import io.legado.app.help.BookHelp import io.legado.app.utils.FileUtils import io.legado.app.utils.HtmlFormatter import io.legado.app.utils.MD5Utils -import io.legado.app.utils.externalFilesDir +import io.legado.app.utils.externalFiles import me.ag2s.epublib.domain.EpubBook import me.ag2s.epublib.epub.EpubReader import org.jsoup.Jsoup @@ -80,7 +80,7 @@ class EpubFile(var book: Book) { epubBook?.let { if (book.coverUrl.isNullOrEmpty()) { book.coverUrl = FileUtils.getPath( - appCtx.externalFilesDir, + appCtx.externalFiles, "covers", "${MD5Utils.md5Encode16(book.bookUrl)}.jpg" ) 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 30c9f65db..46d7ba187 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 @@ -119,7 +119,7 @@ object LocalBook { author = author, originName = fileName, coverUrl = FileUtils.getPath( - appCtx.externalFilesDir, + appCtx.externalFiles, "covers", "${MD5Utils.md5Encode16(path)}.jpg" ) diff --git a/app/src/main/java/io/legado/app/model/localBook/UmdFile.kt b/app/src/main/java/io/legado/app/model/localBook/UmdFile.kt index de6c1586f..0451b44ca 100644 --- a/app/src/main/java/io/legado/app/model/localBook/UmdFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/UmdFile.kt @@ -4,17 +4,16 @@ import android.net.Uri import android.util.Log import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter - import io.legado.app.utils.FileUtils import io.legado.app.utils.MD5Utils -import io.legado.app.utils.externalFilesDir +import io.legado.app.utils.externalFiles import io.legado.app.utils.isContentScheme import me.ag2s.umdlib.domain.UmdBook import me.ag2s.umdlib.umd.UmdReader import splitties.init.appCtx import java.io.File import java.io.InputStream -import java.util.ArrayList +import java.util.* class UmdFile(var book: Book) { companion object { @@ -74,7 +73,7 @@ class UmdFile(var book: Book) { umdBook?.let { if (book.coverUrl.isNullOrEmpty()) { book.coverUrl = FileUtils.getPath( - appCtx.externalFilesDir, + appCtx.externalFiles, "covers", "${MD5Utils.md5Encode16(book.bookUrl)}.jpg" ) diff --git a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt index c8e645b11..805edfe5f 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt @@ -108,7 +108,7 @@ class BookInfoEditActivity : if (uri.isContentScheme()) { val doc = DocumentFile.fromSingleUri(this, uri) doc?.name?.let { - var file = this.externalFilesDir + var file = this.externalFiles file = FileUtils.createFileIfNotExist(file, "covers", it) kotlin.runCatching { DocumentUtils.readBytes(this, doc.uri) @@ -128,7 +128,7 @@ class BookInfoEditActivity : RealPathUtil.getPath(this, uri)?.let { path -> val imgFile = File(path) if (imgFile.exists()) { - var file = this.externalFilesDir + var file = this.externalFiles file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) file.writeBytes(imgFile.readBytes()) coverChangeTo(file.absolutePath) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 224d4b93d..3837b6c22 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -224,7 +224,7 @@ class BgTextConfigDialog : BaseDialogFragment() { } execute { val exportFiles = arrayListOf() - val configDirPath = FileUtils.getPath(requireContext().eCacheDir, "readConfig") + val configDirPath = FileUtils.getPath(requireContext().externalCache, "readConfig") FileUtils.deleteFile(configDirPath) val configDir = FileUtils.createFolderIfNotExist(configDirPath) val configExportPath = FileUtils.getPath(configDir, "readConfig.json") @@ -269,7 +269,7 @@ class BgTextConfigDialog : BaseDialogFragment() { exportFiles.add(bgExportFile) } } - val configZipPath = FileUtils.getPath(requireContext().eCacheDir, configFileName) + val configZipPath = FileUtils.getPath(requireContext().externalCache, configFileName) if (ZipUtils.zipFiles(exportFiles, File(configZipPath))) { if (uri.isContentScheme()) { DocumentFile.fromTreeUri(requireContext(), uri)?.let { treeDoc -> @@ -331,11 +331,11 @@ class BgTextConfigDialog : BaseDialogFragment() { @Suppress("BlockingMethodInNonBlockingContext") private fun importConfig(byteArray: ByteArray) { execute { - val configZipPath = FileUtils.getPath(requireContext().eCacheDir, configFileName) + val configZipPath = FileUtils.getPath(requireContext().externalCache, configFileName) FileUtils.deleteFile(configZipPath) val zipFile = FileUtils.createFileIfNotExist(configZipPath) zipFile.writeBytes(byteArray) - val configDirPath = FileUtils.getPath(requireContext().eCacheDir, "readConfig") + val configDirPath = FileUtils.getPath(requireContext().externalCache, "readConfig") FileUtils.deleteFile(configDirPath) ZipUtils.unzipFile(zipFile, FileUtils.createFolderIfNotExist(configDirPath)) val configDir = FileUtils.createFolderIfNotExist(configDirPath) @@ -344,7 +344,7 @@ class BgTextConfigDialog : BaseDialogFragment() { if (config.textFont.isNotEmpty()) { val fontName = FileUtils.getName(config.textFont) val fontPath = - FileUtils.getPath(requireContext().externalFilesDir, "font", fontName) + FileUtils.getPath(requireContext().externalFiles, "font", fontName) if (!FileUtils.exist(fontPath)) { FileUtils.getFile(configDir, fontName).copyTo(File(fontPath)) } @@ -352,7 +352,7 @@ class BgTextConfigDialog : BaseDialogFragment() { } if (config.bgType == 2) { val bgName = FileUtils.getName(config.bgStr) - val bgPath = FileUtils.getPath(requireContext().externalFilesDir, "bg", bgName) + val bgPath = FileUtils.getPath(requireContext().externalFiles, "bg", bgName) if (!FileUtils.exist(bgPath)) { val bgFile = FileUtils.getFile(configDir, bgName) if (bgFile.exists()) { @@ -362,7 +362,7 @@ class BgTextConfigDialog : BaseDialogFragment() { } if (config.bgTypeNight == 2) { val bgName = FileUtils.getName(config.bgStrNight) - val bgPath = FileUtils.getPath(requireContext().externalFilesDir, "bg", bgName) + val bgPath = FileUtils.getPath(requireContext().externalFiles, "bg", bgName) if (!FileUtils.exist(bgPath)) { val bgFile = FileUtils.getFile(configDir, bgName) if (bgFile.exists()) { @@ -372,7 +372,7 @@ class BgTextConfigDialog : BaseDialogFragment() { } if (config.bgTypeEInk == 2) { val bgName = FileUtils.getName(config.bgStrEInk) - val bgPath = FileUtils.getPath(requireContext().externalFilesDir, "bg", bgName) + val bgPath = FileUtils.getPath(requireContext().externalFiles, "bg", bgName) if (!FileUtils.exist(bgPath)) { val bgFile = FileUtils.getFile(configDir, bgName) if (bgFile.exists()) { @@ -395,7 +395,7 @@ class BgTextConfigDialog : BaseDialogFragment() { val doc = DocumentFile.fromSingleUri(requireContext(), uri) doc?.name?.let { val file = - FileUtils.createFileIfNotExist(requireContext().externalFilesDir, "bg", it) + FileUtils.createFileIfNotExist(requireContext().externalFiles, "bg", it) kotlin.runCatching { DocumentUtils.readBytes(requireContext(), doc.uri) }.getOrNull()?.let { byteArray -> diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index 9a9d7b9bd..5547b76db 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -226,7 +226,7 @@ class OtherConfigFragment : BasePreferenceFragment(), if (uri.isContentScheme()) { val doc = DocumentFile.fromSingleUri(requireContext(), uri) doc?.name?.let { - var file = requireContext().externalFilesDir + var file = requireContext().externalFiles file = FileUtils.createFileIfNotExist(file, "covers", it) kotlin.runCatching { DocumentUtils.readBytes(requireContext(), doc.uri) @@ -247,7 +247,7 @@ class OtherConfigFragment : BasePreferenceFragment(), RealPathUtil.getPath(requireContext(), uri)?.let { path -> val imgFile = File(path) if (imgFile.exists()) { - var file = requireContext().externalFilesDir + var file = requireContext().externalFiles file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) file.writeBytes(imgFile.readBytes()) putPrefString(PreferKey.defaultCover, file.absolutePath) diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 2a5e414ee..94eb8987b 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -225,7 +225,7 @@ class ThemeConfigFragment : BasePreferenceFragment(), if (uri.isContentScheme()) { val doc = DocumentFile.fromSingleUri(requireContext(), uri) doc?.name?.let { - var file = requireContext().externalFilesDir + var file = requireContext().externalFiles file = FileUtils.createFileIfNotExist(file, preferenceKey, it) kotlin.runCatching { DocumentUtils.readBytes(requireContext(), doc.uri) @@ -247,7 +247,7 @@ class ThemeConfigFragment : BasePreferenceFragment(), RealPathUtil.getPath(requireContext(), uri)?.let { path -> val imgFile = File(path) if (imgFile.exists()) { - var file = requireContext().externalFilesDir + var file = requireContext().externalFiles file = FileUtils.createFileIfNotExist(file, preferenceKey, imgFile.name) file.writeBytes(imgFile.readBytes()) putPrefString(preferenceKey, file.absolutePath) diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index c070e4361..22f205101 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -139,7 +139,7 @@ class FontSelectDialog : BaseDialogFragment(), private fun getLocalFonts(): ArrayList { val fontItems = arrayListOf() val fontDir = - FileUtils.createFolderIfNotExist(requireContext().externalFilesDir, "font") + FileUtils.createFolderIfNotExist(requireContext().externalFiles, "font") fontDir.listFiles { pathName -> pathName.name.lowercase(Locale.getDefault()).matches(fontRegex) }?.forEach { diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index f11a7f890..315c76c67 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -224,10 +224,10 @@ val Context.sysBattery: Int return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 } -val Context.externalFilesDir: File +val Context.externalFiles: File get() = this.getExternalFilesDir(null) ?: this.filesDir -val Context.eCacheDir: File +val Context.externalCache: File get() = this.externalCacheDir ?: this.cacheDir fun Context.openUrl(url: String) { diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 1e8c8b502..c0f8e0dda 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -105,8 +105,7 @@ object FileUtils { } fun getCachePath(): String { - return appCtx.externalCacheDir?.absolutePath - ?: appCtx.cacheDir.absolutePath + return appCtx.externalCache.absolutePath } fun getSdCardPath(): String {