This commit is contained in:
Horis
2025-04-28 10:31:36 +08:00
parent b5bccbcc2a
commit 4c8f13cf57
2 changed files with 13 additions and 5 deletions
@@ -57,7 +57,6 @@ import splitties.init.appCtx
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
import java.io.FileOutputStream
import java.net.URLEncoder import java.net.URLEncoder
import java.nio.charset.Charset import java.nio.charset.Charset
import java.security.MessageDigest import java.security.MessageDigest
@@ -310,16 +309,24 @@ interface JsExtensions : JsEncodeUtils {
* @return 下载的文件相对路径 * @return 下载的文件相对路径
*/ */
fun downloadFile(url: String): String { fun downloadFile(url: String): String {
rhinoContext.ensureActive()
val analyzeUrl = AnalyzeUrl(url, source = getSource(), coroutineContext = context) val analyzeUrl = AnalyzeUrl(url, source = getSource(), coroutineContext = context)
val type = UrlUtil.getSuffix(url, analyzeUrl.type) val type = UrlUtil.getSuffix(url, analyzeUrl.type)
val path = FileUtils.getPath( val path = FileUtils.getPath(
File(FileUtils.getCachePath()), File(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.${type}" "${MD5Utils.md5Encode16(url)}.${type}"
) )
val file = File(path).createFileReplace() val file = File(path)
file.delete()
analyzeUrl.getInputStream().use { iStream -> analyzeUrl.getInputStream().use { iStream ->
FileOutputStream(file).use { oStream -> file.createFileReplace()
iStream.copyTo(oStream) try {
file.outputStream().buffered().use { oStream ->
iStream.copyTo(oStream)
}
} catch (e: Throwable) {
file.delete()
throw e
} }
} }
return path.substring(FileUtils.getCachePath().length) return path.substring(FileUtils.getCachePath().length)
@@ -337,6 +344,7 @@ interface JsExtensions : JsEncodeUtils {
ReplaceWith("downloadFile(url)") ReplaceWith("downloadFile(url)")
) )
fun downloadFile(content: String, url: String): String { fun downloadFile(content: String, url: String): String {
rhinoContext.ensureActive()
val type = AnalyzeUrl(url, source = getSource(), coroutineContext = context).type val type = AnalyzeUrl(url, source = getSource(), coroutineContext = context).type
?: return "" ?: return ""
val path = FileUtils.getPath( val path = FileUtils.getPath(
@@ -31,7 +31,7 @@ class JsURL(url: String, baseUrl: String? = null) {
searchParams = query?.let { _ -> searchParams = query?.let { _ ->
val map = hashMapOf<String, String>() val map = hashMapOf<String, String>()
query.split("&").forEach { query.split("&").forEach {
val x = it.split("=") val x = it.split("=", limit = 2)
map[x[0]] = URLDecoder.decode(x[1], "utf-8") map[x[0]] = URLDecoder.decode(x[1], "utf-8")
} }
map map