可以直接导出为链接,方便分享

pull/1295/head
gedoor 3 years ago
parent da6f786e72
commit fd09a251a3
  1. 38
      app/src/main/java/io/legado/app/help/DirectLinkUpload.kt
  2. 7
      app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt
  3. 18
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  4. 2
      app/src/main/java/io/legado/app/ui/config/DirectLinkUploadConfig.kt
  5. 2
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  6. 2
      app/src/main/res/values-es-rES/strings.xml
  7. 2
      app/src/main/res/values-ja-rJP/strings.xml
  8. 2
      app/src/main/res/values-pt-rBR/strings.xml
  9. 2
      app/src/main/res/values-zh-rHK/strings.xml
  10. 2
      app/src/main/res/values-zh-rTW/strings.xml
  11. 2
      app/src/main/res/values-zh/strings.xml
  12. 2
      app/src/main/res/values/strings.xml
  13. 5
      app/src/main/res/xml/pref_config_other.xml

@ -1,9 +1,7 @@
package io.legado.app.help package io.legado.app.help
import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.postMultipart
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.RuleData import io.legado.app.model.analyzeRule.RuleData
object DirectLinkUpload { object DirectLinkUpload {
@ -12,24 +10,33 @@ object DirectLinkUpload {
private const val downloadUrlRuleKey = "directLinkDownloadUrlRule" private const val downloadUrlRuleKey = "directLinkDownloadUrlRule"
suspend fun upLoad(fileName: String, byteArray: ByteArray): String { suspend fun upLoad(fileName: String, byteArray: ByteArray): String {
val res = okHttpClient.newCallStrResponse { val url = getUploadUrl()
url("https://shuyuan.miaogongzi.site/upload.php") if (url.isNullOrBlank()) {
val fileRequest = mapOf( error("上传url未配置")
Pair("fileName", fileName),
Pair("file", byteArray),
Pair("contentType", "application/json")
)
postMultipart(
"multipart/form-data",
mapOf(Pair("file", fileRequest))
)
} }
val downloadUrlRule = getDownloadUrlRule()
if (downloadUrlRule.isNullOrBlank()) {
error("下载地址规则未配置")
}
val analyzeUrl = AnalyzeUrl(url)
val res = analyzeUrl.upload(fileName, byteArray, "application/json")
val analyzeRule = AnalyzeRule(RuleData()).setContent(res.body, res.url) val analyzeRule = AnalyzeRule(RuleData()).setContent(res.body, res.url)
return analyzeRule.getString("tag.b@text") val downloadUrl = analyzeRule.getString(downloadUrlRule)
if (downloadUrl.isBlank()) {
error("上传失败")
}
return downloadUrl
} }
fun getUploadUrl(): String? { fun getUploadUrl(): String? {
return CacheManager.get(uploadUrlKey) return CacheManager.get(uploadUrlKey)
?: """http://lk1.wancient.com/shuyuan,{
"method":"POST",
"body": {
"file": "fileRequest"
},
"type": "multipart/form-data"
}""".trimMargin()
} }
fun putUploadUrl(url: String) { fun putUploadUrl(url: String) {
@ -38,6 +45,7 @@ object DirectLinkUpload {
fun getDownloadUrlRule(): String? { fun getDownloadUrlRule(): String? {
return CacheManager.get(uploadUrlKey) return CacheManager.get(uploadUrlKey)
?: "http://lk1.wancient.com/shuyuan/{{$.data}}"
} }
fun putDownloadUrlRule(rule: String) { fun putDownloadUrlRule(rule: String) {

@ -118,8 +118,11 @@ fun Request.Builder.postForm(form: Map<String, String>, encoded: Boolean = false
post(formBody.build()) post(formBody.build())
} }
fun Request.Builder.postMultipart(type: String, form: Map<String, Any>) { fun Request.Builder.postMultipart(type: String?, form: Map<String, Any>) {
val multipartBody = MultipartBody.Builder().setType(type.toMediaType()) val multipartBody = MultipartBody.Builder()
type?.let {
multipartBody.setType(type.toMediaType())
}
form.forEach { form.forEach {
when (val value = it.value) { when (val value = it.value) {
is Map<*, *> -> { is Map<*, *> -> {

@ -19,6 +19,7 @@ import java.net.URLEncoder
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
import javax.script.SimpleBindings import javax.script.SimpleBindings
import kotlin.collections.HashMap
/** /**
* Created by GKF on 2018/1/24. * Created by GKF on 2018/1/24.
@ -314,6 +315,23 @@ class AnalyzeUrl(
}.bytes() }.bytes()
} }
suspend fun upload(fileName: String, file: ByteArray, contentType: String): StrResponse {
return getProxyClient(proxy).newCallStrResponse(retry) {
url(url)
val bodyMap = GSON.fromJsonObject<HashMap<String, Any>>(body)!!
bodyMap.forEach { entry ->
if (entry.value.toString() == "fileRequest") {
bodyMap[entry.key] = mapOf(
Pair("fileName", fileName),
Pair("file", file),
Pair("contentType", contentType)
)
}
}
postMultipart(type, bodyMap)
}
}
private fun setCookie(tag: String?) { private fun setCookie(tag: String?) {
if (tag != null) { if (tag != null) {
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)

@ -8,6 +8,7 @@ import io.legado.app.R
import io.legado.app.base.BaseDialogFragment import io.legado.app.base.BaseDialogFragment
import io.legado.app.databinding.DialogDirectLinkUploadConfigBinding import io.legado.app.databinding.DialogDirectLinkUploadConfigBinding
import io.legado.app.help.DirectLinkUpload import io.legado.app.help.DirectLinkUpload
import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import io.legado.app.utils.windowSize import io.legado.app.utils.windowSize
@ -35,6 +36,7 @@ class DirectLinkUploadConfig : BaseDialogFragment() {
} }
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
binding.toolBar.setBackgroundColor(primaryColor)
binding.editUploadUrl.setText(DirectLinkUpload.getUploadUrl()) binding.editUploadUrl.setText(DirectLinkUpload.getUploadUrl())
binding.editDownloadUrlRule.setText(DirectLinkUpload.getDownloadUrlRule()) binding.editDownloadUrlRule.setText(DirectLinkUpload.getDownloadUrlRule())
binding.tvCancel.onClick { binding.tvCancel.onClick {

@ -85,7 +85,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
putPrefInt(PreferKey.webPort, it) putPrefInt(PreferKey.webPort, it)
} }
PreferKey.cleanCache -> clearCache() PreferKey.cleanCache -> clearCache()
"uploadRule" -> DirectLinkUploadConfig().show(childFragmentManager, "uploadRuleConfig")
} }
return super.onPreferenceTreeClick(preference) return super.onPreferenceTreeClick(preference)
} }

@ -869,4 +869,6 @@
<string name="sort_by_respondTime">Ordenar por tiempo de respuesta</string> <string name="sort_by_respondTime">Ordenar por tiempo de respuesta</string>
<string name="export_success">导出成功</string> <string name="export_success">导出成功</string>
<string name="path">路径</string> <string name="path">路径</string>
<string name="direct_link_upload_rule">直链上传规则</string>
<string name="direct_link_upload_rule_summary">用于导出书源书单时生成直链url</string>
</resources> </resources>

@ -869,4 +869,6 @@
<string name="sort_by_respondTime">Sort by respond time</string> <string name="sort_by_respondTime">Sort by respond time</string>
<string name="export_success">导出成功</string> <string name="export_success">导出成功</string>
<string name="path">路径</string> <string name="path">路径</string>
<string name="direct_link_upload_rule">直链上传规则</string>
<string name="direct_link_upload_rule_summary">用于导出书源书单时生成直链url</string>
</resources> </resources>

@ -869,5 +869,7 @@
<string name="sort_by_respondTime">Classificar por tempo de resposta</string> <string name="sort_by_respondTime">Classificar por tempo de resposta</string>
<string name="export_success">导出成功</string> <string name="export_success">导出成功</string>
<string name="path">路径</string> <string name="path">路径</string>
<string name="direct_link_upload_rule">直链上传规则</string>
<string name="direct_link_upload_rule_summary">用于导出书源书单时生成直链url</string>
</resources> </resources>

@ -866,5 +866,7 @@
<string name="sort_by_respondTime">響應時間排序</string> <string name="sort_by_respondTime">響應時間排序</string>
<string name="export_success">导出成功</string> <string name="export_success">导出成功</string>
<string name="path">路径</string> <string name="path">路径</string>
<string name="direct_link_upload_rule">直链上传规则</string>
<string name="direct_link_upload_rule_summary">用于导出书源书单时生成直链url</string>
</resources> </resources>

@ -867,5 +867,7 @@
<string name="sort_by_respondTime">響應時間排序</string> <string name="sort_by_respondTime">響應時間排序</string>
<string name="export_success">导出成功</string> <string name="export_success">导出成功</string>
<string name="path">路径</string> <string name="path">路径</string>
<string name="direct_link_upload_rule">直链上传规则</string>
<string name="direct_link_upload_rule_summary">用于导出书源书单时生成直链url</string>
</resources> </resources>

@ -868,5 +868,7 @@
<string name="sort_by_respondTime">响应时间排序</string> <string name="sort_by_respondTime">响应时间排序</string>
<string name="export_success">导出成功</string> <string name="export_success">导出成功</string>
<string name="path">路径</string> <string name="path">路径</string>
<string name="direct_link_upload_rule">直链上传规则</string>
<string name="direct_link_upload_rule_summary">用于导出书源书单时生成直链url</string>
</resources> </resources>

@ -869,5 +869,7 @@
<string name="download_url_rule">下载URL规则</string> <string name="download_url_rule">下载URL规则</string>
<string name="export_success">导出成功</string> <string name="export_success">导出成功</string>
<string name="path">路径</string> <string name="path">路径</string>
<string name="direct_link_upload_rule">直链上传规则</string>
<string name="direct_link_upload_rule_summary">用于导出书源书单时生成直链url</string>
</resources> </resources>

@ -56,6 +56,11 @@
android:key="userAgent" android:key="userAgent"
android:title="UserAgent" /> android:title="UserAgent" />
<io.legado.app.ui.widget.prefs.Preference
android:key="uploadRule"
android:title="@string/direct_link_upload_rule"
android:summary="@string/direct_link_upload_rule_summary" />
<io.legado.app.ui.widget.prefs.SwitchPreference <io.legado.app.ui.widget.prefs.SwitchPreference
android:key="Cronet" android:key="Cronet"
android:defaultValue="false" android:defaultValue="false"

Loading…
Cancel
Save