From 795f4e327e07e024d00e90c45413dcd040c016ef Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 15 Jul 2021 23:02:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 4 ++ .../java/io/legado/app/help/JsExtensions.kt | 51 ++++++++++++++++--- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 00a06a045..ac1946a60 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -8,6 +8,10 @@ * 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则出现问题。先关闭替换净化并刷新,再观察是否正常。如果正常说明净化规则存在误杀,如果关闭后仍然出现相关问题,请点击源链接查看原文与正文是否相同,如果不同,再进行反馈。 * 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源! +**2021/07/16** +1. js扩展函数添加删除本地文件方法 +2. js扩展函数对于文件的读写删操作都是相对路径,只能操作阅读缓存内的文件,/android/data/{package}/cache/... + **2021/07/15** 1. 添加js函数来修复开启js沙箱后某些书源失效。by ag2s20150909 ```kotlin 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 1f464db47..084058044 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -23,6 +23,11 @@ import java.util.* import java.util.zip.ZipEntry import java.util.zip.ZipInputStream +/** + * js扩展类, 在js中通过java变量调用 + * 所有对于文件的读写删操作都是相对路径,只能操作阅读缓存内的文件 + * /android/data/{package}/cache/... + */ @Keep @Suppress("unused") interface JsExtensions { @@ -253,20 +258,51 @@ interface JsExtensions { } /** - * 读取本地文件 + * 获取本地文件 + * @param path 相对路径 + * @return File */ - fun readFile(path: String): ByteArray { - return File(path).readBytes() + fun getFile(path: String): File { + val cachePath = appCtx.eCacheDir.path + val aPath = if (path.startsWith(File.separator)) { + cachePath + path + } else { + cachePath + File.separator + path + } + return File(aPath) + } + + fun readFile(path: String): ByteArray? { + val file = getFile(path) + if (file.exists()) { + return file.readBytes() + } + return null } fun readTxtFile(path: String): String { - val f = File(path) - val charsetName = EncodingDetect.getEncode(f) - return String(f.readBytes(), charset(charsetName)) + val file = getFile(path) + if (file.exists()) { + val charsetName = EncodingDetect.getEncode(file) + return String(file.readBytes(), charset(charsetName)) + } + return "" } fun readTxtFile(path: String, charsetName: String): String { - return String(File(path).readBytes(), charset(charsetName)) + val file = getFile(path) + if (file.exists()) { + return String(file.readBytes(), charset(charsetName)) + } + return "" + } + + /** + * 删除本地文件 + */ + fun deleteFile(path: String) { + val file = getFile(path) + FileUtils.delete(file) } /** @@ -405,7 +441,6 @@ interface JsExtensions { transformation: String, iv: String ): ByteArray? { - return EncoderUtils.decryptAES( data = str.encodeToByteArray(), key = key.encodeToByteArray(),