pull/2250/head
kunfei 2 years ago
parent 4504676289
commit d8f6575416
  1. 44
      app/src/main/java/io/legado/app/help/JsExtensions.kt

@ -27,6 +27,7 @@ import splitties.init.appCtx
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.net.URLEncoder
import java.nio.charset.Charset
import java.text.SimpleDateFormat
@ -143,7 +144,10 @@ interface JsExtensions {
* 使用内置浏览器打开链接并等待网页结果
*/
fun startBrowserAwait(url: String, title: String): StrResponse {
return StrResponse(url, SourceVerificationHelp.getVerificationResult(getSource(), url, title, true))
return StrResponse(
url,
SourceVerificationHelp.getVerificationResult(getSource(), url, title, true)
)
}
/**
@ -203,6 +207,29 @@ interface JsExtensions {
}
}
/**
* 下载文件
* @param url 下载地址:可带参数type,文件后缀,不带默认zip
* @return 下载的文件相对路径
*/
fun downloadFile(url: String): String {
val analyzeUrl = AnalyzeUrl(url, source = getSource())
val type = analyzeUrl.type ?: "zip"
val path = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.${type}"
)
FileUtils.delete(path)
analyzeUrl.getInputStream().use { iStream ->
val file = FileUtils.createFileIfNotExist(path)
FileOutputStream(file).use { oStream ->
iStream.copyTo(oStream)
}
}
return path.substring(FileUtils.getCachePath().length)
}
/**
* 实现16进制字符串转文件
* @param content 需要转成文件的16进制字符串
@ -211,18 +238,18 @@ interface JsExtensions {
*/
fun downloadFile(content: String, url: String): String {
val type = AnalyzeUrl(url, source = getSource()).type ?: return ""
val zipPath = FileUtils.getPath(
val path = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.${type}"
)
FileUtils.delete(zipPath)
val zipFile = FileUtils.createFileIfNotExist(zipPath)
FileUtils.delete(path)
val file = FileUtils.createFileIfNotExist(path)
StringUtils.hexStringToByte(content).let {
if (it.isNotEmpty()) {
zipFile.writeBytes(it)
file.writeBytes(it)
}
}
return zipPath.substring(FileUtils.getCachePath().length)
return path.substring(FileUtils.getCachePath().length)
}
/**
@ -1030,7 +1057,10 @@ interface JsExtensions {
algorithm: String,
key: String
): String {
return Base64.encodeToString(HMac(algorithm, key.toByteArray()).digest(data), Base64.NO_WRAP)
return Base64.encodeToString(
HMac(algorithm, key.toByteArray()).digest(data),
Base64.NO_WRAP
)
}
fun md5Encode(str: String): String {

Loading…
Cancel
Save