fix(js/java.cacheFile): 下载出错时保存类出错信息

This commit is contained in:
Xwite 2023-03-25 11:34:10 +08:00
parent 2e7ce425a6
commit a50ca54068
2 changed files with 16 additions and 10 deletions

View File

@ -195,16 +195,17 @@ interface JsExtensions : JsEncodeUtils {
* 缓存以文本方式保存的文件 .js .txt等
* @param saveTime 缓存时间单位
*/
fun cacheFile(urlStr: String, saveTime: Int): String? {
fun cacheFile(urlStr: String, saveTime: Int): String {
val key = md5Encode16(urlStr)
val cache = CacheManager.getFile(key)
if (cache.isNullOrBlank()) {
log("首次下载 $urlStr")
val value = ajax(urlStr) ?: return null
CacheManager.putFile(key, value, saveTime)
return value
val cahcePath = CacheManager.get(key)
return if (cache.isNullOrBlank()) {
val path = downloadFile(urlStr)
log("首次下载 $urlStr >> $path")
CacheManager.put(key, path, saveTime)
readTxtFile(path)
} else {
readTxtFile(cahcePath)
}
return cache
}
/**
@ -224,12 +225,12 @@ interface JsExtensions : JsEncodeUtils {
/**
* 下载文件
* @param url 下载地址:可带参数type,文件后缀,不带默认zip
* @param url 下载地址:可带参数type
* @return 下载的文件相对路径
*/
fun downloadFile(url: String): String {
val analyzeUrl = AnalyzeUrl(url, source = getSource())
val type = analyzeUrl.type ?: "zip"
val type = UrlUtil.getSuffix(url, analyzeUrl.type)
val path = FileUtils.getPath(
File(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.${type}"
@ -250,6 +251,10 @@ interface JsExtensions : JsEncodeUtils {
* @param url 通过url里的参数来判断文件类型
* @return 相对路径
*/
@Deprecated(
"Depreted",
ReplaceWith("downloadFile(url: String)")
)
fun downloadFile(content: String, url: String): String {
val type = AnalyzeUrl(url, source = getSource()).type ?: return ""
val path = FileUtils.getPath(

View File

@ -142,6 +142,7 @@ object UrlUtil {
) {
path.substringAfterLast("/")
} else {
AppLog.put("getFileNameFromPath: Unexpected file suffix: $suffix")
null
}
}