|
|
@ -3,25 +3,27 @@ package io.legado.app.help |
|
|
|
import io.legado.app.exception.NoStackTraceException |
|
|
|
import io.legado.app.exception.NoStackTraceException |
|
|
|
import io.legado.app.model.analyzeRule.AnalyzeRule |
|
|
|
import io.legado.app.model.analyzeRule.AnalyzeRule |
|
|
|
import io.legado.app.model.analyzeRule.AnalyzeUrl |
|
|
|
import io.legado.app.model.analyzeRule.AnalyzeUrl |
|
|
|
import io.legado.app.utils.jsonPath |
|
|
|
import io.legado.app.utils.ACache |
|
|
|
import io.legado.app.utils.readString |
|
|
|
import io.legado.app.utils.GSON |
|
|
|
|
|
|
|
import io.legado.app.utils.fromJsonObject |
|
|
|
import splitties.init.appCtx |
|
|
|
import splitties.init.appCtx |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Suppress("MemberVisibilityCanBePrivate") |
|
|
|
object DirectLinkUpload { |
|
|
|
object DirectLinkUpload { |
|
|
|
|
|
|
|
|
|
|
|
private const val uploadUrlKey = "directLinkUploadUrl" |
|
|
|
const val ruleFileName = "directLinkUploadRule.json" |
|
|
|
private const val downloadUrlRuleKey = "directLinkDownloadUrlRule" |
|
|
|
|
|
|
|
private const val summaryKey = "directSummary" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Throws(NoStackTraceException::class) |
|
|
|
@Throws(NoStackTraceException::class) |
|
|
|
suspend fun upLoad(fileName: String, file: Any, contentType: String): String { |
|
|
|
suspend fun upLoad(fileName: String, file: Any, contentType: String): String { |
|
|
|
val url = getUploadUrl() |
|
|
|
val rule = defaultRule ?: getConfigRule() |
|
|
|
if (url.isNullOrBlank()) { |
|
|
|
rule ?: throw NoStackTraceException("直链上传规则未配置") |
|
|
|
|
|
|
|
val url = rule.uploadUrl |
|
|
|
|
|
|
|
if (url.isBlank()) { |
|
|
|
throw NoStackTraceException("上传url未配置") |
|
|
|
throw NoStackTraceException("上传url未配置") |
|
|
|
} |
|
|
|
} |
|
|
|
val downloadUrlRule = getDownloadUrlRule() |
|
|
|
val downloadUrlRule = rule.downloadUrlRule |
|
|
|
if (downloadUrlRule.isNullOrBlank()) { |
|
|
|
if (downloadUrlRule.isBlank()) { |
|
|
|
throw NoStackTraceException("下载地址规则未配置") |
|
|
|
throw NoStackTraceException("下载地址规则未配置") |
|
|
|
} |
|
|
|
} |
|
|
|
val analyzeUrl = AnalyzeUrl(url) |
|
|
|
val analyzeUrl = AnalyzeUrl(url) |
|
|
@ -34,49 +36,36 @@ object DirectLinkUpload { |
|
|
|
return downloadUrl |
|
|
|
return downloadUrl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private val ruleDoc by lazy { |
|
|
|
private val defaultRule: Rule? by lazy { |
|
|
|
val json = String( |
|
|
|
val json = String( |
|
|
|
appCtx.assets.open("defaultData${File.separator}directLinkUpload.json") |
|
|
|
appCtx.assets.open("defaultData${File.separator}directLinkUpload.json") |
|
|
|
.readBytes() |
|
|
|
.readBytes() |
|
|
|
) |
|
|
|
) |
|
|
|
jsonPath.parse(json) |
|
|
|
GSON.fromJsonObject<Rule>(json).getOrNull() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getUploadUrl(): String? { |
|
|
|
fun getConfigRule(): Rule? { |
|
|
|
return CacheManager.get(uploadUrlKey) |
|
|
|
val json = ACache.get(cacheDir = false).getAsString(ruleFileName) |
|
|
|
?: ruleDoc.readString("$.UploadUrl") |
|
|
|
return GSON.fromJsonObject<Rule>(json).getOrNull() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun putUploadUrl(url: String) { |
|
|
|
fun putConfigRule(uploadUrl: String, downloadUrlRule: String, summary: String?) { |
|
|
|
CacheManager.put(uploadUrlKey, url) |
|
|
|
val rule = Rule(uploadUrl, downloadUrlRule, summary) |
|
|
|
|
|
|
|
ACache.get(cacheDir = false).put(ruleFileName, GSON.toJson(rule)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getDownloadUrlRule(): String? { |
|
|
|
fun delConfigRule() { |
|
|
|
return CacheManager.get(downloadUrlRuleKey) |
|
|
|
ACache.get(cacheDir = false).remove(ruleFileName) |
|
|
|
?: ruleDoc.readString("$.DownloadUrlRule") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun putDownloadUrlRule(rule: String) { |
|
|
|
|
|
|
|
CacheManager.put(downloadUrlRuleKey, rule) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getSummary(): String? { |
|
|
|
fun getSummary(): String? { |
|
|
|
return CacheManager.get(summaryKey) |
|
|
|
return getConfigRule()?.summary |
|
|
|
?: ruleDoc.readString("summary") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun putSummary(summary: String?) { |
|
|
|
data class Rule( |
|
|
|
if (summary != null) { |
|
|
|
var uploadUrl: String, |
|
|
|
CacheManager.put(summaryKey, summary) |
|
|
|
var downloadUrlRule: String, |
|
|
|
} else { |
|
|
|
var summary: String? |
|
|
|
CacheManager.delete(summaryKey) |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun delete() { |
|
|
|
|
|
|
|
CacheManager.delete(uploadUrlKey) |
|
|
|
|
|
|
|
CacheManager.delete(downloadUrlRuleKey) |
|
|
|
|
|
|
|
CacheManager.delete(summaryKey) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |