diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index f5c43e30d..ffcccab31 100644 --- a/app/src/main/assets/help/jsHelp.md +++ b/app/src/main/assets/help/jsHelp.md @@ -124,9 +124,14 @@ eval(String(java.cacheFile(url))) 删除缓存文件 cache.delete(java.md5Encode16(url)) ``` -* 获取网络zip文件里面的数据 +* 获取网络压缩文件里面指定路径的数据 *可替换Zip Rar 7Z Archive ``` -java.getZipStringContent(url: String, path: String) +java.get*StringContent(url: String, path: String): String + +java.get*StringContent(url: String, path: String, charsetName: String): String + +java.get*ByteArrayContent(url: String, path: String): ByteArray? + ``` * base64 > flags参数可省略,默认Base64.NO_WRAP,查看[flags参数说明](https://blog.csdn.net/zcmain/article/details/97051870) @@ -159,8 +164,12 @@ java.hexEncodeToString(utf8: String) ``` //文件下载,content为十六进制字符串,url用于生成文件名,返回文件路径 downloadFile(content: String, url: String): String +downloadFile(url: String): String //文件解压,zipPath为压缩文件路径,返回解压路径 +unArchiveFile(zipPath: String): String unzipFile(zipPath: String): String +unrarFile(zipPath: String): String +un7zFile(zipPath: String): String //文件夹内所有文件读取 getTxtInFolder(unzipPath: String): String //读取文本文件 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 abb3502a1..bcf5d82d2 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -497,16 +497,7 @@ interface JsExtensions : JsEncodeUtils { * @return 相对路径 */ fun unzipFile(zipPath: String): String { - if (zipPath.isEmpty()) return "" - val unzipPath = FileUtils.getPath( - FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), - FileUtils.getNameExcludeExtension(zipPath) - ) - val unzipFolder = File(unzipPath).createFolderReplace() - val zipFile = getFile(zipPath) - ZipUtils.unzipFile(zipFile, unzipFolder) - FileUtils.delete(zipFile.absolutePath) - return unzipPath.substring(FileUtils.getCachePath().length) + return unArchiveFile(zipPath) } /** * js实现7Zip压缩文件解压 @@ -514,16 +505,7 @@ interface JsExtensions : JsEncodeUtils { * @return 相对路径 */ fun un7zFile(zipPath: String): String { - if (zipPath.isEmpty()) return "" - val unzipPath = FileUtils.getPath( - FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), - FileUtils.getNameExcludeExtension(zipPath) - ) - val unzipFolder = File(unzipPath).createFolderReplace() - val zipFile = getFile(zipPath) - SevenZipUtils.un7zToPath(zipFile, unzipFolder) - FileUtils.delete(zipFile.absolutePath) - return unzipPath.substring(FileUtils.getCachePath().length) + return unArchiveFile(zipPath) } /** * js实现Rar压缩文件解压 @@ -531,16 +513,7 @@ interface JsExtensions : JsEncodeUtils { * @return 相对路径 */ fun unrarFile(zipPath: String): String { - if (zipPath.isEmpty()) return "" - val unzipPath = FileUtils.getPath( - FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), - FileUtils.getNameExcludeExtension(zipPath) - ) - val unzipFolder = File(unzipPath).createFolderReplace() - val zipFile = getFile(zipPath) - RarUtils.unRarToPath(zipFile, unzipFolder) - FileUtils.delete(zipFile.absolutePath) - return unzipPath.substring(FileUtils.getCachePath().length) + return unArchiveFile(zipPath) } /** * js实现压缩文件解压 @@ -549,29 +522,8 @@ interface JsExtensions : JsEncodeUtils { */ fun unArchiveFile(zipPath: String): String { if (zipPath.isEmpty()) return "" - val unzipPath = FileUtils.getPath( - FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), - FileUtils.getNameExcludeExtension(zipPath) - ) - val unzipFolder = File(unzipPath).createFolderReplace() - val zipFile = getFile(zipPath) - when { - zipPath.endsWith(".zip", ignoreCase = true) -> { - ZipUtils.unzipFile(zipFile, unzipFolder) - } - zipPath.endsWith(".rar", ignoreCase = true) -> { - RarUtils.unRarToPath(zipFile, unzipFolder) - } - - zipPath.endsWith(".7z", ignoreCase = true) -> { - SevenZipUtils.un7zToPath(zipFile, unzipFolder) - } - else -> { - log("自动解压未识别类型${zipPath}") - } - } - FileUtils.delete(zipFile.absolutePath) - return unzipPath.substring(FileUtils.getCachePath().length) + return ArchiveUtils.deCompress(zipPath).toString() + .substring(FileUtils.getCachePath().length) } /** diff --git a/app/src/main/java/io/legado/app/help/book/BookHelp.kt b/app/src/main/java/io/legado/app/help/book/BookHelp.kt index dcf8560de..8a458183c 100644 --- a/app/src/main/java/io/legado/app/help/book/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/book/BookHelp.kt @@ -178,13 +178,7 @@ object BookHelp { } fun getImageSuffix(src: String): String { - var suffix = src.substringAfterLast(".").substringBefore(",") - //检查截取的后缀字符是否合法 [a-zA-Z0-9] - val fileSuffixRegex = Regex("^[a-z0-9]+$", RegexOption.IGNORE_CASE) - if (suffix.length > 5 || !suffix.matches(fileSuffixRegex)) { - suffix = "jpg" - } - return suffix + return UrlUtil.getSuffix(src, "jpg") } @Throws(IOException::class, FileNotFoundException::class) diff --git a/app/src/main/java/io/legado/app/lib/webdav/Authorization.kt b/app/src/main/java/io/legado/app/lib/webdav/Authorization.kt index af78adc12..92d2b92a2 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/Authorization.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/Authorization.kt @@ -2,7 +2,6 @@ package io.legado.app.lib.webdav import io.legado.app.data.appDb import io.legado.app.data.entities.Server.WebDavConfig -import io.legado.app.exception.NoStackTraceException import okhttp3.Credentials import java.nio.charset.Charset import java.nio.charset.StandardCharsets @@ -24,7 +23,7 @@ data class Authorization( } constructor(serverID: Long?): this("","") { - serverID ?: throw NoStackTraceException("Unexpected server ID") + serverID ?: throw WebDavException("Unexpected server ID") appDb.serverDao.get(serverID)?.getWebDavConfig()?.run { data = Credentials.basic(username, password, charset) } ?: throw WebDavException("Unexpected WebDav Authorization") 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 bb3274239..6f30c4ce0 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 @@ -266,16 +266,9 @@ object LocalBook { /** * 分析下载文件类书源的下载链接的文件后缀 - * https://www.example.com/download/{fileName}.{type} 含有文件名和后缀 - * https://www.example.com/download/?fileid=1234, {type: "txt"} 规则设置 */ fun parseFileSuffix(url: String): String { - val analyzeUrl = AnalyzeUrl(url) - val urlNoOption = analyzeUrl.url - val lastPath = urlNoOption.substringAfterLast("/") - val fileType = lastPath.substringAfterLast(".") - val type = analyzeUrl.type - return type ?: fileType + return UrlUtil.getSuffix(url, "ext") } fun saveBookFile( @@ -336,10 +329,10 @@ object LocalBook { // 兼容旧版链接 val webdav: WebDav = kotlin.runCatching { WebDav.fromPath(webDavUrl) - }.onFailure { + }.getOrElse { AppWebDav.defaultBookWebDav ?: throw WebDavException("Unexpected defaultBookWebDav") - }.getOrThrow() + } val uri = runBlocking { saveBookFile(webdav.downloadInputStream(), localBook.originName) } diff --git a/app/src/main/java/io/legado/app/utils/FileExtensions.kt b/app/src/main/java/io/legado/app/utils/FileExtensions.kt index 0fe815c2a..2b42e98d3 100644 --- a/app/src/main/java/io/legado/app/utils/FileExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/FileExtensions.kt @@ -17,8 +17,7 @@ fun File.exists(vararg subDirFiles: String): Boolean { @Throws(Exception::class) fun File.listFileDocs(filter: FileDocFilter? = null): ArrayList { val docList = arrayListOf() - listFiles() - listFiles()?.forEach { + listFiles { val item = FileDoc( it.name, it.isDirectory, diff --git a/app/src/main/java/io/legado/app/utils/UrlUtil.kt b/app/src/main/java/io/legado/app/utils/UrlUtil.kt index 5e23e7143..2ed135190 100644 --- a/app/src/main/java/io/legado/app/utils/UrlUtil.kt +++ b/app/src/main/java/io/legado/app/utils/UrlUtil.kt @@ -1,5 +1,7 @@ package io.legado.app.utils +import java.util.regex.Pattern + object UrlUtil { fun replaceReservedChar(text: String): String { @@ -24,5 +26,15 @@ object UrlUtil { .replace("|", "%7C") } + fun getSuffix(url: String, default: String): String { + val suffix = url.ubstringAfterLast(".").substringBeforeLast(",") + //检查截取的后缀字符是否合法 [a-zA-Z0-9] + val fileSuffixRegex = Regex("^[a-z0-9]+$", RegexOption.IGNORE_CASE) + return if (suffix.length > 5 || !suffix.matches(fileSuffixRegex)) { + default + } else { + suffix + } + } } \ No newline at end of file