优化
This commit is contained in:
@@ -16,9 +16,11 @@ import okhttp3.RequestBody.Companion.asRequestBody
|
|||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import okhttp3.ResponseBody
|
import okhttp3.ResponseBody
|
||||||
|
import okhttp3.internal.http.RealResponseBody
|
||||||
|
import okio.buffer
|
||||||
|
import okio.source
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
import java.util.zip.ZipInputStream
|
import java.util.zip.ZipInputStream
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
@@ -77,36 +79,37 @@ suspend fun Call.await(): Response = suspendCancellableCoroutine { block ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun ResponseBody.text(encode: String? = null): String {
|
fun ResponseBody.text(encode: String? = null): String {
|
||||||
return unCompress {
|
val responseBytes = Utf8BomUtils.removeUTF8BOM(bytes())
|
||||||
val responseBytes = Utf8BomUtils.removeUTF8BOM(it.readBytes())
|
var charsetName: String? = encode
|
||||||
var charsetName: String? = encode
|
|
||||||
|
|
||||||
charsetName?.let {
|
charsetName?.let {
|
||||||
return@unCompress String(responseBytes, Charset.forName(charsetName))
|
return String(responseBytes, Charset.forName(charsetName))
|
||||||
}
|
|
||||||
|
|
||||||
//根据http头判断
|
|
||||||
contentType()?.charset()?.let { charset ->
|
|
||||||
return@unCompress String(responseBytes, charset)
|
|
||||||
}
|
|
||||||
|
|
||||||
//根据内容判断
|
|
||||||
charsetName = EncodingDetect.getHtmlEncode(responseBytes)
|
|
||||||
return@unCompress String(responseBytes, Charset.forName(charsetName))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//根据http头判断
|
||||||
|
contentType()?.charset()?.let { charset ->
|
||||||
|
return String(responseBytes, charset)
|
||||||
|
}
|
||||||
|
|
||||||
|
//根据内容判断
|
||||||
|
charsetName = EncodingDetect.getHtmlEncode(responseBytes)
|
||||||
|
return String(responseBytes, Charset.forName(charsetName))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> ResponseBody.unCompress(success: (inputStream: InputStream) -> T): T {
|
fun ResponseBody.decompressed(): ResponseBody {
|
||||||
return if (contentType() == "application/zip".toMediaType()) {
|
val contentType = contentType()?.toString()
|
||||||
byteStream().use { byteStream ->
|
if (contentType != "application/zip") {
|
||||||
ZipInputStream(byteStream).use {
|
return this
|
||||||
it.nextEntry
|
|
||||||
success.invoke(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
byteStream().use(success)
|
|
||||||
}
|
}
|
||||||
|
val source = ZipInputStream(byteStream()).apply {
|
||||||
|
try {
|
||||||
|
nextEntry
|
||||||
|
} catch (e: Exception) {
|
||||||
|
close()
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}.source().buffer()
|
||||||
|
return RealResponseBody(contentType, contentLength(), source)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Request.Builder.addHeaders(headers: Map<String, String>) {
|
fun Request.Builder.addHeaders(headers: Map<String, String>) {
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ import io.legado.app.data.entities.BookSourcePart
|
|||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.book.ContentProcessor
|
import io.legado.app.help.book.ContentProcessor
|
||||||
import io.legado.app.help.config.AppConfig
|
import io.legado.app.help.config.AppConfig
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.unCompress
|
|
||||||
import io.legado.app.help.source.SourceHelp
|
import io.legado.app.help.source.SourceHelp
|
||||||
import io.legado.app.utils.GSON
|
import io.legado.app.utils.GSON
|
||||||
import io.legado.app.utils.fromJsonArray
|
import io.legado.app.utils.fromJsonArray
|
||||||
@@ -194,7 +194,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.unCompress {
|
}.decompressed().byteStream().use {
|
||||||
GSON.fromJsonArray<BookSource>(it).getOrThrow().let { list ->
|
GSON.fromJsonArray<BookSource>(it).getOrThrow().let { list ->
|
||||||
val source = list.firstOrNull() ?: return@let
|
val source = list.firstOrNull() ?: return@let
|
||||||
if (source.bookSourceUrl.isEmpty()) {
|
if (source.bookSourceUrl.isEmpty()) {
|
||||||
|
|||||||
@@ -9,10 +9,16 @@ import io.legado.app.constant.AppLog
|
|||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.DictRule
|
import io.legado.app.data.entities.DictRule
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.text
|
import io.legado.app.help.http.text
|
||||||
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
|
||||||
|
|
||||||
class ImportDictRuleViewModel(app: Application) : BaseViewModel(app) {
|
class ImportDictRuleViewModel(app: Application) : BaseViewModel(app) {
|
||||||
|
|
||||||
@@ -94,7 +100,7 @@ class ImportDictRuleViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.text().let {
|
}.decompressed().text().let {
|
||||||
importSourceAwait(it)
|
importSourceAwait(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import io.legado.app.constant.AppLog
|
|||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.HttpTTS
|
import io.legado.app.data.entities.HttpTTS
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.text
|
import io.legado.app.help.http.text
|
||||||
@@ -96,7 +97,7 @@ class ImportHttpTtsViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.text().let {
|
}.decompressed().text().let {
|
||||||
importSourceAwait(it)
|
importSourceAwait(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import io.legado.app.data.appDb
|
|||||||
import io.legado.app.data.entities.ReplaceRule
|
import io.legado.app.data.entities.ReplaceRule
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.ReplaceAnalyzer
|
import io.legado.app.help.ReplaceAnalyzer
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.text
|
import io.legado.app.help.http.text
|
||||||
@@ -95,10 +96,12 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
val rules = ReplaceAnalyzer.jsonToReplaceRules(text).getOrThrow()
|
val rules = ReplaceAnalyzer.jsonToReplaceRules(text).getOrThrow()
|
||||||
allRules.addAll(rules)
|
allRules.addAll(rules)
|
||||||
}
|
}
|
||||||
|
|
||||||
text.isJsonObject() -> {
|
text.isJsonObject() -> {
|
||||||
val rule = ReplaceAnalyzer.jsonToReplaceRule(text).getOrThrow()
|
val rule = ReplaceAnalyzer.jsonToReplaceRule(text).getOrThrow()
|
||||||
allRules.add(rule)
|
allRules.add(rule)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> throw NoStackTraceException("格式不对")
|
else -> throw NoStackTraceException("格式不对")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,7 +114,7 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.text("utf-8").let {
|
}.decompressed().text("utf-8").let {
|
||||||
importAwait(it)
|
importAwait(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ import io.legado.app.data.appDb
|
|||||||
import io.legado.app.data.entities.RssSource
|
import io.legado.app.data.entities.RssSource
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.config.AppConfig
|
import io.legado.app.help.config.AppConfig
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.unCompress
|
|
||||||
import io.legado.app.help.source.SourceHelp
|
import io.legado.app.help.source.SourceHelp
|
||||||
import io.legado.app.utils.GSON
|
import io.legado.app.utils.GSON
|
||||||
import io.legado.app.utils.fromJsonArray
|
import io.legado.app.utils.fromJsonArray
|
||||||
@@ -153,7 +153,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.unCompress { body ->
|
}.decompressed().byteStream().use { body ->
|
||||||
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
||||||
for (item in items) {
|
for (item in items) {
|
||||||
if (!item.containsKey("sourceUrl")) {
|
if (!item.containsKey("sourceUrl")) {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import io.legado.app.constant.AppConst
|
|||||||
import io.legado.app.constant.AppLog
|
import io.legado.app.constant.AppLog
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.config.ThemeConfig
|
import io.legado.app.help.config.ThemeConfig
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.text
|
import io.legado.app.help.http.text
|
||||||
@@ -92,7 +93,7 @@ class ImportThemeViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.text().let {
|
}.decompressed().text().let {
|
||||||
importSourceAwait(it)
|
importSourceAwait(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,16 @@ import io.legado.app.constant.AppLog
|
|||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.TxtTocRule
|
import io.legado.app.data.entities.TxtTocRule
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.text
|
import io.legado.app.help.http.text
|
||||||
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
|
||||||
|
|
||||||
class ImportTxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
|
class ImportTxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
|
||||||
|
|
||||||
@@ -95,7 +101,7 @@ class ImportTxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.text().let {
|
}.decompressed().text().let {
|
||||||
importSourceAwait(it)
|
importSourceAwait(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import androidx.core.net.toUri
|
|||||||
import io.legado.app.R
|
import io.legado.app.R
|
||||||
import io.legado.app.constant.AppConst
|
import io.legado.app.constant.AppConst
|
||||||
import io.legado.app.help.config.ReadBookConfig
|
import io.legado.app.help.config.ReadBookConfig
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.text
|
import io.legado.app.help.http.text
|
||||||
@@ -24,7 +25,7 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.text("utf-8")
|
}.decompressed().text("utf-8")
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
success.invoke(it)
|
success.invoke(it)
|
||||||
}.onError {
|
}.onError {
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ import io.legado.app.data.appDb
|
|||||||
import io.legado.app.data.entities.HttpTTS
|
import io.legado.app.data.entities.HttpTTS
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.DefaultData
|
import io.legado.app.help.DefaultData
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
|
||||||
import io.legado.app.help.http.okHttpClient
|
|
||||||
import io.legado.app.help.http.text
|
|
||||||
import io.legado.app.utils.isJsonArray
|
import io.legado.app.utils.isJsonArray
|
||||||
import io.legado.app.utils.isJsonObject
|
import io.legado.app.utils.isJsonObject
|
||||||
import io.legado.app.utils.readText
|
import io.legado.app.utils.readText
|
||||||
@@ -31,20 +28,6 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun importOnLine(url: String) {
|
|
||||||
execute {
|
|
||||||
okHttpClient.newCallResponseBody {
|
|
||||||
url(url)
|
|
||||||
}.text("utf-8").let { json ->
|
|
||||||
import(json)
|
|
||||||
}
|
|
||||||
}.onSuccess {
|
|
||||||
context.toastOnUi("导入成功")
|
|
||||||
}.onError {
|
|
||||||
context.toastOnUi("导入失败")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun importLocal(uri: Uri) {
|
fun importLocal(uri: Uri) {
|
||||||
execute {
|
execute {
|
||||||
import(uri.readText(context))
|
import(uri.readText(context))
|
||||||
@@ -62,11 +45,13 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
|
|||||||
appDb.httpTTSDao.insert(*it.toTypedArray())
|
appDb.httpTTSDao.insert(*it.toTypedArray())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
text.isJsonObject() -> {
|
text.isJsonObject() -> {
|
||||||
HttpTTS.fromJson(text).getOrThrow().let {
|
HttpTTS.fromJson(text).getOrThrow().let {
|
||||||
appDb.httpTTSDao.insert(it)
|
appDb.httpTTSDao.insert(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
throw NoStackTraceException("格式不对")
|
throw NoStackTraceException("格式不对")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,6 @@ import io.legado.app.base.BaseViewModel
|
|||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.TxtTocRule
|
import io.legado.app.data.entities.TxtTocRule
|
||||||
import io.legado.app.help.DefaultData
|
import io.legado.app.help.DefaultData
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
|
||||||
import io.legado.app.help.http.okHttpClient
|
|
||||||
import io.legado.app.help.http.text
|
|
||||||
import io.legado.app.utils.GSON
|
|
||||||
import io.legado.app.utils.fromJsonArray
|
|
||||||
|
|
||||||
class TxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
|
class TxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
|
||||||
|
|
||||||
@@ -37,22 +32,6 @@ class TxtTocRuleViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun importOnLine(url: String, finally: (msg: String) -> Unit) {
|
|
||||||
execute {
|
|
||||||
okHttpClient.newCallResponseBody {
|
|
||||||
url(url)
|
|
||||||
}.text("utf-8").let { json ->
|
|
||||||
GSON.fromJsonArray<TxtTocRule>(json).getOrThrow().let {
|
|
||||||
appDb.txtTocRuleDao.insert(*it.toTypedArray())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.onSuccess {
|
|
||||||
finally("导入成功")
|
|
||||||
}.onError {
|
|
||||||
finally("导入失败\n${it.localizedMessage}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toTop(vararg rules: TxtTocRule) {
|
fun toTop(vararg rules: TxtTocRule) {
|
||||||
execute {
|
execute {
|
||||||
val minOrder = appDb.txtTocRuleDao.minOrder - 1
|
val minOrder = appDb.txtTocRuleDao.minOrder - 1
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import io.legado.app.data.entities.Book
|
|||||||
import io.legado.app.data.entities.BookSourcePart
|
import io.legado.app.data.entities.BookSourcePart
|
||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.coroutine.Coroutine
|
import io.legado.app.help.coroutine.Coroutine
|
||||||
|
import io.legado.app.help.http.decompressed
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
import io.legado.app.help.http.text
|
import io.legado.app.help.http.text
|
||||||
@@ -132,7 +133,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
|
|||||||
text.isAbsUrl() -> {
|
text.isAbsUrl() -> {
|
||||||
okHttpClient.newCallResponseBody {
|
okHttpClient.newCallResponseBody {
|
||||||
url(text)
|
url(text)
|
||||||
}.text().let {
|
}.decompressed().text().let {
|
||||||
importBookshelf(it, groupId)
|
importBookshelf(it, groupId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user