This commit is contained in:
kunfei
2023-03-15 19:42:43 +08:00
parent 6e837c4e29
commit 4ba7a4f31f
3 changed files with 20 additions and 13 deletions
@@ -6,6 +6,7 @@ import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.ACache
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject
import splitties.init.appCtx
import java.io.File
@@ -18,7 +19,6 @@ object DirectLinkUpload {
@Throws(NoStackTraceException::class)
suspend fun upLoad(fileName: String, file: Any, contentType: String): String {
val rule = getRule()
rule ?: throw NoStackTraceException("直链上传规则未配置")
val url = rule.uploadUrl
if (url.isBlank()) {
throw NoStackTraceException("上传url未配置")
@@ -37,16 +37,16 @@ object DirectLinkUpload {
return downloadUrl
}
private val defaultRule: Rule? by lazy {
private val defaultRules: List<Rule> by lazy {
val json = String(
appCtx.assets.open("defaultData${File.separator}directLinkUpload.json")
.readBytes()
)
GSON.fromJsonObject<Rule>(json).getOrNull()
GSON.fromJsonArray<Rule>(json).getOrThrow()
}
fun getRule(): Rule? {
return getConfig() ?: defaultRule
fun getRule(): Rule {
return getConfig() ?: defaultRules[0]
}
fun getConfig(): Rule? {
@@ -64,7 +64,7 @@ object DirectLinkUpload {
}
fun getSummary(): String? {
return getRule()?.summary
return getRule().summary
}
@Keep
@@ -34,7 +34,7 @@ object UrlUtil {
*/
fun getFileName(fileUrl: String): String? {
return kotlin.runCatching {
var fileName = ""
var fileName: String
val url = URL(fileUrl)
val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
// head方式
@@ -63,7 +63,7 @@ object UrlUtil {
fun getSuffix(url: String, default: String): String {
val suffix = url.substringAfterLast(".").substringBeforeLast(",")
//检查截取的后缀字符是否合法 [a-zA-Z0-9]
val fileSuffixRegex = Regex("^[a-z0-9]+$", RegexOption.IGNORE_CASE)
val fileSuffixRegex = Regex("^[a-z\\d]+$", RegexOption.IGNORE_CASE)
return if (suffix.length > 5 || !suffix.matches(fileSuffixRegex)) {
default
} else {