Parcourir la source

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

Xwite il y a 3 ans
Parent
commit
8dc1ac6140

+ 15 - 10
app/src/main/java/io/legado/app/help/JsExtensions.kt

@@ -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(

+ 1 - 0
app/src/main/java/io/legado/app/utils/UrlUtil.kt

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