Просмотр исходного кода

Merge remote-tracking branch 'origin/master'

kunfei 3 лет назад
Родитель
Сommit
9f4f52e51f

+ 2 - 3
app/src/main/assets/help/jsHelp.md

@@ -124,7 +124,7 @@ eval(String(java.cacheFile(url)))
 删除缓存文件
 cache.delete(java.md5Encode16(url))
 ```
-* 获取网络压缩文件里面指定路径的数据 *可替换Zip Rar 7Z Archive
+* 获取网络压缩文件里面指定路径的数据 *可替换Zip Rar 7Z
 ```
 java.get*StringContent(url: String, path: String): String
 
@@ -167,8 +167,7 @@ java.androidId()
 * 文件
 >  所有对于文件的读写删操作都是相对路径,只能操作阅读缓存/android/data/{package}/cache/内的文件
 ```
-//文件下载,content为十六进制字符串,url用于生成文件名,返回文件路径
-downloadFile(content: String, url: String): String
+//文件下载 url用于生成文件名,返回文件路径
 downloadFile(url: String): String
 //文件解压,zipPath为压缩文件路径,返回解压路径
 unArchiveFile(zipPath: String): String

+ 17 - 12
app/src/main/java/io/legado/app/help/JsExtensions.kt

@@ -173,7 +173,7 @@ interface JsExtensions : JsEncodeUtils {
      */
     fun importScript(path: String): String {
         val result = when {
-            path.startsWith("http") -> cacheFile(path) ?: ""
+            path.startsWith("http") -> cacheFile(path)
             path.isUri() -> Uri.parse(path).readText(appCtx)
             path.startsWith("/storage") -> FileUtils.readText(path)
             else -> readTxtFile(path)
@@ -187,7 +187,7 @@ interface JsExtensions : JsEncodeUtils {
      * @param urlStr 网络文件的链接
      * @return 返回缓存后的文件内容
      */
-    fun cacheFile(urlStr: String): String? {
+    fun cacheFile(urlStr: String): String {
         return cacheFile(urlStr, 0)
     }
 
@@ -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 (cahcePath.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(

+ 3 - 1
app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt

@@ -24,7 +24,9 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo
                 val fileDoc = FileDoc.fromUri(uri, false)
                 fileName = fileDoc.name
                 if (fileName.matches(AppPattern.archiveFileRegex)) {
-                    ArchiveUtils.deCompress(fileDoc).forEach {
+                    ArchiveUtils.deCompress(fileDoc, ArchiveUtils.TEMP_PATH) {
+                        it.matches(bookFileRegex)
+                    }.forEach {
                         dispatch(FileDoc.fromFile(it))
                     }
                 } else {

+ 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
         }
     }

+ 4 - 0
rhino/src/main/java/com/script/rhino/RhinoScriptEngine.kt

@@ -164,6 +164,7 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
             val newScope: Scriptable = ExternalScriptable(ctxt, indexedProps)
             newScope.prototype = topLevel
             newScope.put("context", newScope, ctxt)
+            /*
             val cx = Context.enter()
             try {
                 @Language("js")
@@ -195,6 +196,7 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
             } finally {
                 Context.exit()
             }
+            */
             newScope
         }
     }
@@ -303,6 +305,7 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
 
         private const val DEBUG = false
 
+        /*
         @Language("js")
         private val printSource = """
             function print(str, newline) {
@@ -322,6 +325,7 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
               print(str, true);
             }
         """.trimIndent()
+        */
 
         init {
             ContextFactory.initGlobal(object : ContextFactory() {