mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
761a560d0c
commit
ccac347519
@ -77,25 +77,27 @@ suspend fun Call.await(): Response = suspendCancellableCoroutine { block ->
|
||||
}
|
||||
|
||||
fun ResponseBody.text(encode: String? = null): String {
|
||||
val responseBytes = Utf8BomUtils.removeUTF8BOM(bytes())
|
||||
return unCompress {
|
||||
val responseBytes = Utf8BomUtils.removeUTF8BOM(it.readBytes())
|
||||
var charsetName: String? = encode
|
||||
|
||||
charsetName?.let {
|
||||
return String(responseBytes, Charset.forName(charsetName))
|
||||
return@unCompress String(responseBytes, Charset.forName(charsetName))
|
||||
}
|
||||
|
||||
//根据http头判断
|
||||
contentType()?.charset()?.let {
|
||||
return String(responseBytes, it)
|
||||
contentType()?.charset()?.let { charset ->
|
||||
return@unCompress String(responseBytes, charset)
|
||||
}
|
||||
|
||||
//根据内容判断
|
||||
charsetName = EncodingDetect.getHtmlEncode(responseBytes)
|
||||
return String(responseBytes, Charset.forName(charsetName))
|
||||
return@unCompress String(responseBytes, Charset.forName(charsetName))
|
||||
}
|
||||
}
|
||||
|
||||
fun ResponseBody.unCompress(success: (inputStream: InputStream) -> Unit) {
|
||||
if (contentType() == "application/zip".toMediaType()) {
|
||||
fun <T> ResponseBody.unCompress(success: (inputStream: InputStream) -> T): T {
|
||||
return if (contentType() == "application/zip".toMediaType()) {
|
||||
byteStream().use { byteStream ->
|
||||
ZipInputStream(byteStream).use {
|
||||
it.nextEntry
|
||||
@ -152,18 +154,22 @@ fun Request.Builder.postMultipart(type: String?, form: Map<String, Any>) {
|
||||
is File -> {
|
||||
file.asRequestBody(mediaType)
|
||||
}
|
||||
|
||||
is ByteArray -> {
|
||||
file.toRequestBody(mediaType)
|
||||
}
|
||||
|
||||
is String -> {
|
||||
file.toRequestBody(mediaType)
|
||||
}
|
||||
|
||||
else -> {
|
||||
GSON.toJson(file).toRequestBody(mediaType)
|
||||
}
|
||||
}
|
||||
multipartBody.addFormDataPart(it.key, fileName, requestBody)
|
||||
}
|
||||
|
||||
else -> multipartBody.addFormDataPart(it.key, it.value.toString())
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import io.legado.app.help.book.ContentProcessor
|
||||
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.http.unCompress
|
||||
import io.legado.app.help.source.SourceHelp
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonArray
|
||||
@ -187,7 +188,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
} else {
|
||||
url(url)
|
||||
}
|
||||
}.byteStream().use {
|
||||
}.unCompress {
|
||||
GSON.fromJsonArray<BookSource>(it).getOrThrow().let { list ->
|
||||
val source = list.firstOrNull() ?: return@let
|
||||
if (source.bookSourceUrl.isEmpty()) {
|
||||
|
@ -14,6 +14,7 @@ import io.legado.app.exception.NoStackTraceException
|
||||
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.http.unCompress
|
||||
import io.legado.app.help.source.SourceHelp
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonArray
|
||||
@ -148,7 +149,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
} else {
|
||||
url(url)
|
||||
}
|
||||
}.byteStream().use { body ->
|
||||
}.unCompress { body ->
|
||||
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
||||
for (item in items) {
|
||||
if (!item.containsKey("sourceUrl")) {
|
||||
|
@ -33,6 +33,7 @@ object ZipUtils {
|
||||
zipOutputStream.putNextEntry(ZipEntry(fileName))
|
||||
zipOutputStream.write(byteArray)
|
||||
zipOutputStream.closeEntry()
|
||||
zipOutputStream.finish()
|
||||
return zipOutputStream.use {
|
||||
byteOut.use {
|
||||
byteOut.toByteArray()
|
||||
@ -183,11 +184,11 @@ object ZipUtils {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
BufferedInputStream(FileInputStream(srcFile)).use { `is` ->
|
||||
BufferedInputStream(FileInputStream(srcFile)).use {
|
||||
val entry = ZipEntry(rootPath1)
|
||||
entry.comment = comment
|
||||
zos.putNextEntry(entry)
|
||||
zos.write(`is`.readBytes())
|
||||
it.copyTo(zos)
|
||||
zos.closeEntry()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user