Horis 1 год назад
Родитель
Сommit
2a9191abe6

+ 38 - 39
app/src/main/java/io/legado/app/help/JsExtensions.kt

@@ -1,6 +1,5 @@
 package io.legado.app.help
 
-import android.net.Uri
 import android.webkit.WebSettings
 import androidx.annotation.Keep
 import cn.hutool.core.codec.Base64
@@ -39,12 +38,8 @@ import io.legado.app.utils.createFileReplace
 import io.legado.app.utils.externalCache
 import io.legado.app.utils.fromJsonObject
 import io.legado.app.utils.isAbsUrl
-import io.legado.app.utils.isContentScheme
-import io.legado.app.utils.isUri
 import io.legado.app.utils.longToastOnUi
 import io.legado.app.utils.mapAsync
-import io.legado.app.utils.readBytes
-import io.legado.app.utils.readText
 import io.legado.app.utils.stackTraceStr
 import io.legado.app.utils.startActivity
 import io.legado.app.utils.toStringArray
@@ -104,14 +99,12 @@ interface JsExtensions : JsEncodeUtils {
             url.toString()
         }
         val analyzeUrl = AnalyzeUrl(urlStr, source = getSource(), coroutineContext = context)
-        return runBlocking(context) {
-            kotlin.runCatching {
-                analyzeUrl.getStrResponseAwait().body
-            }.onFailure {
-                AppLog.put("ajax(${urlStr}) error\n${it.localizedMessage}", it)
-            }.getOrElse {
-                it.stackTraceStr
-            }
+        return kotlin.runCatching {
+            analyzeUrl.getStrResponse().body
+        }.onFailure {
+            AppLog.put("ajax(${urlStr}) error\n${it.localizedMessage}", it)
+        }.getOrElse {
+            it.stackTraceStr
         }
     }
 
@@ -121,7 +114,11 @@ interface JsExtensions : JsEncodeUtils {
     fun ajaxAll(urlList: Array<String>): Array<StrResponse> {
         return runBlocking(context) {
             urlList.asFlow().mapAsync(AppConfig.threadCount) { url ->
-                val analyzeUrl = AnalyzeUrl(url, source = getSource())
+                val analyzeUrl = AnalyzeUrl(
+                    url,
+                    source = getSource(),
+                    coroutineContext = coroutineContext
+                )
                 analyzeUrl.getStrResponseAwait()
             }.flowOn(IO).toList().toTypedArray()
         }
@@ -131,29 +128,34 @@ interface JsExtensions : JsEncodeUtils {
      * 访问网络,返回Response<String>
      */
     fun connect(urlStr: String): StrResponse {
-        return runBlocking(context) {
-            val analyzeUrl = AnalyzeUrl(urlStr, source = getSource())
-            kotlin.runCatching {
-                analyzeUrl.getStrResponseAwait()
-            }.onFailure {
-                AppLog.put("connect(${urlStr}) error\n${it.localizedMessage}", it)
-            }.getOrElse {
-                StrResponse(analyzeUrl.url, it.stackTraceStr)
-            }
+        val analyzeUrl = AnalyzeUrl(
+            urlStr,
+            source = getSource(),
+            coroutineContext = context
+        )
+        return kotlin.runCatching {
+            analyzeUrl.getStrResponse()
+        }.onFailure {
+            AppLog.put("connect(${urlStr}) error\n${it.localizedMessage}", it)
+        }.getOrElse {
+            StrResponse(analyzeUrl.url, it.stackTraceStr)
         }
     }
 
     fun connect(urlStr: String, header: String?): StrResponse {
-        return runBlocking(context) {
-            val headerMap = GSON.fromJsonObject<Map<String, String>>(header).getOrNull()
-            val analyzeUrl = AnalyzeUrl(urlStr, headerMapF = headerMap, source = getSource())
-            kotlin.runCatching {
-                analyzeUrl.getStrResponseAwait()
-            }.onFailure {
-                AppLog.put("ajax($urlStr,$header) error\n${it.localizedMessage}", it)
-            }.getOrElse {
-                StrResponse(analyzeUrl.url, it.stackTraceStr)
-            }
+        val headerMap = GSON.fromJsonObject<Map<String, String>>(header).getOrNull()
+        val analyzeUrl = AnalyzeUrl(
+            urlStr,
+            headerMapF = headerMap,
+            source = getSource(),
+            coroutineContext = context
+        )
+        return kotlin.runCatching {
+            analyzeUrl.getStrResponse()
+        }.onFailure {
+            AppLog.put("ajax($urlStr,$header) error\n${it.localizedMessage}", it)
+        }.getOrElse {
+            StrResponse(analyzeUrl.url, it.stackTraceStr)
         }
     }
 
@@ -247,13 +249,11 @@ interface JsExtensions : JsEncodeUtils {
     }
 
     /**
-     * 可从网络,本地文件(阅读私有缓存目录和书籍保存位置支持相对路径)导入JavaScript脚本
+     * 可从网络,本地文件(阅读私有数据目录相对路径)导入JavaScript脚本
      */
     fun importScript(path: String): String {
         val result = when {
             path.startsWith("http") -> cacheFile(path)
-            path.isUri() -> Uri.parse(path).readText(appCtx)
-            path.startsWith("/storage") -> FileUtils.readText(path)
             else -> readTxtFile(path)
         }
         if (result.isBlank()) throw NoStackTraceException("$path 内容获取失败或者为空")
@@ -561,7 +561,8 @@ interface JsExtensions : JsEncodeUtils {
             cachePath + File.separator + path
         }
         val file = File(aPath)
-        if (!file.canonicalPath.startsWith(cachePath)) {
+        val safePath = appCtx.externalCache.parent!!
+        if (!file.canonicalPath.startsWith(safePath)) {
             throw SecurityException("非法路径")
         }
         return file
@@ -812,8 +813,6 @@ interface JsExtensions : JsEncodeUtils {
                         if (qTTF != null) return qTTF
                     }
                     val font: ByteArray? = when {
-                        data.isContentScheme() -> Uri.parse(data).readBytes(appCtx)
-                        data.startsWith("/storage") -> File(data).readBytes()
                         data.isAbsUrl() -> AnalyzeUrl(
                             data,
                             source = getSource(),

+ 7 - 9
app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt

@@ -791,15 +791,13 @@ class AnalyzeRule(
             ruleData = book,
             coroutineContext = coroutineContext
         )
-        return runBlocking(coroutineContext) {
-            kotlin.runCatching {
-                analyzeUrl.getStrResponseAwait().body
-            }.onFailure {
-                log("ajax(${urlStr}) error\n${it.stackTraceToString()}")
-                it.printOnDebug()
-            }.getOrElse {
-                it.stackTraceStr
-            }
+        return kotlin.runCatching {
+            analyzeUrl.getStrResponse().body
+        }.onFailure {
+            log("ajax(${urlStr}) error\n${it.stackTraceToString()}")
+            it.printOnDebug()
+        }.getOrElse {
+            it.stackTraceStr
         }
     }
 

+ 2 - 3
app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt

@@ -1,7 +1,6 @@
 package io.legado.app.ui.book.audio
 
 import android.annotation.SuppressLint
-import android.app.Activity
 import android.icu.text.SimpleDateFormat
 import android.os.Build
 import android.os.Bundle
@@ -240,7 +239,7 @@ class AudioPlayActivity :
                     AudioPlay.book?.removeType(BookType.notShelf)
                     AudioPlay.book?.save()
                     AudioPlay.inBookshelf = true
-                    setResult(Activity.RESULT_OK)
+                    setResult(RESULT_OK)
                 }
                 noButton { viewModel.removeFromBookshelf { super.finish() } }
             }
@@ -289,7 +288,7 @@ class AudioPlayActivity :
 
         }
         observeEventSticky<Float>(EventBus.AUDIO_SPEED) {
-            binding.tvSpeed.text = String.format("%.1fX", it)
+            binding.tvSpeed.text = String.format(Locale.ROOT, "%.1fX", it)
             binding.tvSpeed.visible()
         }
         observeEventSticky<Int>(EventBus.AUDIO_DS) {

+ 2 - 1
app/src/main/java/io/legado/app/ui/book/manga/ReadMangaViewModel.kt

@@ -31,6 +31,7 @@ import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.flow.onEmpty
 import kotlinx.coroutines.flow.onStart
 import kotlinx.coroutines.flow.take
+import splitties.init.appCtx
 
 class ReadMangaViewModel(application: Application) : BaseViewModel(application) {
 
@@ -111,7 +112,7 @@ class ReadMangaViewModel(application: Application) : BaseViewModel(application)
                 return true
             }.onFailure {
                 //加载章节出错
-                ReadManga.mCallback?.loadFail("加载目录失败")
+                ReadManga.mCallback?.loadFail(appCtx.getString(R.string.error_load_toc))
                 return false
             }
         }