mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
563f7d4ed0
commit
93e40d49f6
@ -21,8 +21,12 @@ object DirectLinkUpload {
|
||||
const val ruleFileName = "directLinkUploadRule.json"
|
||||
|
||||
@Throws(NoStackTraceException::class)
|
||||
suspend fun upLoad(fileName: String, file: Any, contentType: String): String {
|
||||
val rule = getRule()
|
||||
suspend fun upLoad(
|
||||
fileName: String,
|
||||
file: Any,
|
||||
contentType: String,
|
||||
rule: Rule = getRule()
|
||||
): String {
|
||||
val url = rule.uploadUrl
|
||||
if (url.isBlank()) {
|
||||
throw NoStackTraceException("上传url未配置")
|
||||
|
@ -18,7 +18,9 @@ import okhttp3.Response
|
||||
import okhttp3.ResponseBody
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.nio.charset.Charset
|
||||
import java.util.zip.ZipInputStream
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
|
||||
@ -92,6 +94,19 @@ fun ResponseBody.text(encode: String? = null): String {
|
||||
return String(responseBytes, Charset.forName(charsetName))
|
||||
}
|
||||
|
||||
fun ResponseBody.unCompress(success: (inputStream: InputStream) -> Unit) {
|
||||
if (contentType() == "application/zip".toMediaType()) {
|
||||
byteStream().use { byteStream ->
|
||||
ZipInputStream(byteStream).use {
|
||||
it.nextEntry
|
||||
success.invoke(it)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
byteStream().use(success)
|
||||
}
|
||||
}
|
||||
|
||||
fun Request.Builder.addHeaders(headers: Map<String, String>) {
|
||||
headers.forEach {
|
||||
addHeader(it.key, it.value)
|
||||
|
@ -17,7 +17,15 @@ import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.help.http.newCallResponseBody
|
||||
import io.legado.app.help.http.okHttpClient
|
||||
import io.legado.app.help.source.SourceHelp
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonArray
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
import io.legado.app.utils.inputStream
|
||||
import io.legado.app.utils.isAbsUrl
|
||||
import io.legado.app.utils.isJsonArray
|
||||
import io.legado.app.utils.isJsonObject
|
||||
import io.legado.app.utils.isUri
|
||||
import io.legado.app.utils.splitNotBlank
|
||||
|
||||
|
||||
class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
@ -179,13 +187,13 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
} else {
|
||||
url(url)
|
||||
}
|
||||
}.byteStream().use { body ->
|
||||
GSON.fromJsonArray<BookSource>(body).getOrThrow().let {
|
||||
val source = it.firstOrNull() ?: return@let
|
||||
}.byteStream().use {
|
||||
GSON.fromJsonArray<BookSource>(it).getOrThrow().let { list ->
|
||||
val source = list.firstOrNull() ?: return@let
|
||||
if (source.bookSourceUrl.isEmpty()) {
|
||||
throw NoStackTraceException("不是书源")
|
||||
}
|
||||
allSources.addAll(it)
|
||||
allSources.addAll(list)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,14 @@ import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.help.http.newCallResponseBody
|
||||
import io.legado.app.help.http.okHttpClient
|
||||
import io.legado.app.help.source.SourceHelp
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonArray
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
import io.legado.app.utils.isAbsUrl
|
||||
import io.legado.app.utils.isJsonArray
|
||||
import io.legado.app.utils.isJsonObject
|
||||
import io.legado.app.utils.jsonPath
|
||||
import io.legado.app.utils.splitNotBlank
|
||||
|
||||
class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
var isAddGroup = false
|
||||
@ -141,7 +148,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
} else {
|
||||
url(url)
|
||||
}
|
||||
}.byteStream().let { body ->
|
||||
}.byteStream().use { body ->
|
||||
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
||||
for (item in items) {
|
||||
if (!item.containsKey("sourceUrl")) {
|
||||
|
@ -15,6 +15,7 @@ import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.applyTint
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
import io.legado.app.utils.getClipText
|
||||
import io.legado.app.utils.longToast
|
||||
import io.legado.app.utils.sendToClip
|
||||
import io.legado.app.utils.setLayout
|
||||
import io.legado.app.utils.toastOnUi
|
||||
@ -41,8 +42,7 @@ class DirectLinkUploadConfig : BaseDialogFragment(R.layout.dialog_direct_link_up
|
||||
dismiss()
|
||||
}
|
||||
binding.tvFooterLeft.onClick {
|
||||
DirectLinkUpload.delConfig()
|
||||
dismiss()
|
||||
test()
|
||||
}
|
||||
binding.tvOk.onClick {
|
||||
getRule()?.let { rule ->
|
||||
@ -58,6 +58,7 @@ class DirectLinkUploadConfig : BaseDialogFragment(R.layout.dialog_direct_link_up
|
||||
R.id.menu_copy_rule -> getRule()?.let { rule ->
|
||||
requireContext().sendToClip(GSON.toJson(rule))
|
||||
}
|
||||
|
||||
R.id.menu_paste_rule -> runCatching {
|
||||
requireContext().getClipText()!!.let {
|
||||
val rule = GSON.fromJsonObject<DirectLinkUpload.Rule>(it).getOrThrow()
|
||||
@ -103,4 +104,15 @@ class DirectLinkUploadConfig : BaseDialogFragment(R.layout.dialog_direct_link_up
|
||||
}
|
||||
}
|
||||
|
||||
private fun test() {
|
||||
val rule = getRule() ?: return
|
||||
execute {
|
||||
DirectLinkUpload.upLoad("test.json", "{}", "application/json", rule)
|
||||
}.onError {
|
||||
longToast(it.localizedMessage!!)
|
||||
}.onSuccess {
|
||||
longToast(it)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -92,7 +92,7 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="12dp"
|
||||
android:text="@string/btn_default_s"
|
||||
android:text="@string/test"
|
||||
tools:ignore="RtlHardcoded" />
|
||||
|
||||
<LinearLayout
|
||||
|
@ -1114,4 +1114,5 @@
|
||||
<string name="shrink_database_summary">减小数据库文件的大小</string>
|
||||
<string name="is_compress">是否压缩</string>
|
||||
<string name="sort_desc">反序</string>
|
||||
<string name="test">测试</string>
|
||||
</resources>
|
||||
|
@ -1117,4 +1117,5 @@
|
||||
<string name="shrink_database_summary">减小数据库文件的大小</string>
|
||||
<string name="is_compress">是否压缩</string>
|
||||
<string name="sort_desc">反序</string>
|
||||
<string name="test">测试</string>
|
||||
</resources>
|
||||
|
@ -1117,4 +1117,5 @@
|
||||
<string name="shrink_database_summary">减小数据库文件的大小</string>
|
||||
<string name="is_compress">是否压缩</string>
|
||||
<string name="sort_desc">反序</string>
|
||||
<string name="test">测试</string>
|
||||
</resources>
|
||||
|
@ -1114,4 +1114,5 @@
|
||||
<string name="shrink_database_summary">减小数据库文件的大小</string>
|
||||
<string name="is_compress">是否压缩</string>
|
||||
<string name="sort_desc">反序</string>
|
||||
<string name="test">测试</string>
|
||||
</resources>
|
||||
|
@ -1116,4 +1116,5 @@
|
||||
<string name="shrink_database_summary">减小数据库文件的大小</string>
|
||||
<string name="is_compress">是否压缩</string>
|
||||
<string name="sort_desc">反序</string>
|
||||
<string name="test">测试</string>
|
||||
</resources>
|
||||
|
@ -1116,4 +1116,5 @@
|
||||
<string name="shrink_database_summary">减小数据库文件的大小</string>
|
||||
<string name="is_compress">是否压缩</string>
|
||||
<string name="sort_desc">反序</string>
|
||||
<string name="test">测试</string>
|
||||
</resources>
|
||||
|
@ -1117,4 +1117,5 @@
|
||||
<string name="shrink_database_summary">减小数据库文件的大小</string>
|
||||
<string name="is_compress">是否压缩</string>
|
||||
<string name="sort_desc">反序</string>
|
||||
<string name="test">测试</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user