Merge remote-tracking branch 'origin/master'

This commit is contained in:
kunfei
2023-03-13 12:24:03 +08:00
7 changed files with 34 additions and 76 deletions
+11 -2
View File
@@ -124,9 +124,14 @@ eval(String(java.cacheFile(url)))
删除缓存文件 删除缓存文件
cache.delete(java.md5Encode16(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 * base64
> flags参数可省略,默认Base64.NO_WRAP,查看[flags参数说明](https://blog.csdn.net/zcmain/article/details/97051870) > flags参数可省略,默认Base64.NO_WRAP,查看[flags参数说明](https://blog.csdn.net/zcmain/article/details/97051870)
@@ -159,8 +164,12 @@ java.hexEncodeToString(utf8: String)
``` ```
//文件下载,content为十六进制字符串,url用于生成文件名,返回文件路径 //文件下载,content为十六进制字符串,url用于生成文件名,返回文件路径
downloadFile(content: String, url: String): String downloadFile(content: String, url: String): String
downloadFile(url: String): String
//文件解压,zipPath为压缩文件路径,返回解压路径 //文件解压,zipPath为压缩文件路径,返回解压路径
unArchiveFile(zipPath: String): String
unzipFile(zipPath: String): String unzipFile(zipPath: String): String
unrarFile(zipPath: String): String
un7zFile(zipPath: String): String
//文件夹内所有文件读取 //文件夹内所有文件读取
getTxtInFolder(unzipPath: String): String getTxtInFolder(unzipPath: String): String
//读取文本文件 //读取文本文件
@@ -497,16 +497,7 @@ interface JsExtensions : JsEncodeUtils {
* @return 相对路径 * @return 相对路径
*/ */
fun unzipFile(zipPath: String): String { fun unzipFile(zipPath: String): String {
if (zipPath.isEmpty()) return "" return unArchiveFile(zipPath)
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)
} }
/** /**
* js实现7Zip压缩文件解压 * js实现7Zip压缩文件解压
@@ -514,16 +505,7 @@ interface JsExtensions : JsEncodeUtils {
* @return 相对路径 * @return 相对路径
*/ */
fun un7zFile(zipPath: String): String { fun un7zFile(zipPath: String): String {
if (zipPath.isEmpty()) return "" return unArchiveFile(zipPath)
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)
} }
/** /**
* js实现Rar压缩文件解压 * js实现Rar压缩文件解压
@@ -531,16 +513,7 @@ interface JsExtensions : JsEncodeUtils {
* @return 相对路径 * @return 相对路径
*/ */
fun unrarFile(zipPath: String): String { fun unrarFile(zipPath: String): String {
if (zipPath.isEmpty()) return "" return unArchiveFile(zipPath)
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)
} }
/** /**
* js实现压缩文件解压 * js实现压缩文件解压
@@ -549,29 +522,8 @@ interface JsExtensions : JsEncodeUtils {
*/ */
fun unArchiveFile(zipPath: String): String { fun unArchiveFile(zipPath: String): String {
if (zipPath.isEmpty()) return "" if (zipPath.isEmpty()) return ""
val unzipPath = FileUtils.getPath( return ArchiveUtils.deCompress(zipPath).toString()
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), .substring(FileUtils.getCachePath().length)
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)
} }
/** /**
@@ -178,13 +178,7 @@ object BookHelp {
} }
fun getImageSuffix(src: String): String { fun getImageSuffix(src: String): String {
var suffix = src.substringAfterLast(".").substringBefore(",") return UrlUtil.getSuffix(src, "jpg")
//检查截取的后缀字符是否合法 [a-zA-Z0-9]
val fileSuffixRegex = Regex("^[a-z0-9]+$", RegexOption.IGNORE_CASE)
if (suffix.length > 5 || !suffix.matches(fileSuffixRegex)) {
suffix = "jpg"
}
return suffix
} }
@Throws(IOException::class, FileNotFoundException::class) @Throws(IOException::class, FileNotFoundException::class)
@@ -2,7 +2,6 @@ package io.legado.app.lib.webdav
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Server.WebDavConfig import io.legado.app.data.entities.Server.WebDavConfig
import io.legado.app.exception.NoStackTraceException
import okhttp3.Credentials import okhttp3.Credentials
import java.nio.charset.Charset import java.nio.charset.Charset
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
@@ -24,7 +23,7 @@ data class Authorization(
} }
constructor(serverID: Long?): this("","") { constructor(serverID: Long?): this("","") {
serverID ?: throw NoStackTraceException("Unexpected server ID") serverID ?: throw WebDavException("Unexpected server ID")
appDb.serverDao.get(serverID)?.getWebDavConfig()?.run { appDb.serverDao.get(serverID)?.getWebDavConfig()?.run {
data = Credentials.basic(username, password, charset) data = Credentials.basic(username, password, charset)
} ?: throw WebDavException("Unexpected WebDav Authorization") } ?: throw WebDavException("Unexpected WebDav Authorization")
@@ -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 { fun parseFileSuffix(url: String): String {
val analyzeUrl = AnalyzeUrl(url) return UrlUtil.getSuffix(url, "ext")
val urlNoOption = analyzeUrl.url
val lastPath = urlNoOption.substringAfterLast("/")
val fileType = lastPath.substringAfterLast(".")
val type = analyzeUrl.type
return type ?: fileType
} }
fun saveBookFile( fun saveBookFile(
@@ -336,10 +329,10 @@ object LocalBook {
// 兼容旧版链接 // 兼容旧版链接
val webdav: WebDav = kotlin.runCatching { val webdav: WebDav = kotlin.runCatching {
WebDav.fromPath(webDavUrl) WebDav.fromPath(webDavUrl)
}.onFailure { }.getOrElse {
AppWebDav.defaultBookWebDav AppWebDav.defaultBookWebDav
?: throw WebDavException("Unexpected defaultBookWebDav") ?: throw WebDavException("Unexpected defaultBookWebDav")
}.getOrThrow() }
val uri = runBlocking { val uri = runBlocking {
saveBookFile(webdav.downloadInputStream(), localBook.originName) saveBookFile(webdav.downloadInputStream(), localBook.originName)
} }
@@ -17,8 +17,7 @@ fun File.exists(vararg subDirFiles: String): Boolean {
@Throws(Exception::class) @Throws(Exception::class)
fun File.listFileDocs(filter: FileDocFilter? = null): ArrayList<FileDoc> { fun File.listFileDocs(filter: FileDocFilter? = null): ArrayList<FileDoc> {
val docList = arrayListOf<FileDoc>() val docList = arrayListOf<FileDoc>()
listFiles() listFiles {
listFiles()?.forEach {
val item = FileDoc( val item = FileDoc(
it.name, it.name,
it.isDirectory, it.isDirectory,
@@ -1,5 +1,7 @@
package io.legado.app.utils package io.legado.app.utils
import java.util.regex.Pattern
object UrlUtil { object UrlUtil {
fun replaceReservedChar(text: String): String { fun replaceReservedChar(text: String): String {
@@ -24,5 +26,15 @@ object UrlUtil {
.replace("|", "%7C") .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
}
}
} }