优化
This commit is contained in:
@@ -77,25 +77,27 @@ suspend fun Call.await(): Response = suspendCancellableCoroutine { block ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun ResponseBody.text(encode: String? = null): String {
|
fun ResponseBody.text(encode: String? = null): String {
|
||||||
val responseBytes = Utf8BomUtils.removeUTF8BOM(bytes())
|
return unCompress {
|
||||||
var charsetName: String? = encode
|
val responseBytes = Utf8BomUtils.removeUTF8BOM(it.readBytes())
|
||||||
|
var charsetName: String? = encode
|
||||||
|
|
||||||
charsetName?.let {
|
charsetName?.let {
|
||||||
return String(responseBytes, Charset.forName(charsetName))
|
return@unCompress 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 {
|
|
||||||
return String(responseBytes, it)
|
|
||||||
}
|
|
||||||
|
|
||||||
//根据内容判断
|
|
||||||
charsetName = EncodingDetect.getHtmlEncode(responseBytes)
|
|
||||||
return String(responseBytes, Charset.forName(charsetName))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ResponseBody.unCompress(success: (inputStream: InputStream) -> Unit) {
|
fun <T> ResponseBody.unCompress(success: (inputStream: InputStream) -> T): T {
|
||||||
if (contentType() == "application/zip".toMediaType()) {
|
return if (contentType() == "application/zip".toMediaType()) {
|
||||||
byteStream().use { byteStream ->
|
byteStream().use { byteStream ->
|
||||||
ZipInputStream(byteStream).use {
|
ZipInputStream(byteStream).use {
|
||||||
it.nextEntry
|
it.nextEntry
|
||||||
@@ -152,18 +154,22 @@ fun Request.Builder.postMultipart(type: String?, form: Map<String, Any>) {
|
|||||||
is File -> {
|
is File -> {
|
||||||
file.asRequestBody(mediaType)
|
file.asRequestBody(mediaType)
|
||||||
}
|
}
|
||||||
|
|
||||||
is ByteArray -> {
|
is ByteArray -> {
|
||||||
file.toRequestBody(mediaType)
|
file.toRequestBody(mediaType)
|
||||||
}
|
}
|
||||||
|
|
||||||
is String -> {
|
is String -> {
|
||||||
file.toRequestBody(mediaType)
|
file.toRequestBody(mediaType)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
GSON.toJson(file).toRequestBody(mediaType)
|
GSON.toJson(file).toRequestBody(mediaType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
multipartBody.addFormDataPart(it.key, fileName, requestBody)
|
multipartBody.addFormDataPart(it.key, fileName, requestBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> multipartBody.addFormDataPart(it.key, it.value.toString())
|
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.config.AppConfig
|
||||||
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
|
||||||
@@ -187,7 +188,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.byteStream().use {
|
}.unCompress {
|
||||||
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()) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ 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.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
|
||||||
@@ -148,7 +149,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.byteStream().use { body ->
|
}.unCompress { 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")) {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ object ZipUtils {
|
|||||||
zipOutputStream.putNextEntry(ZipEntry(fileName))
|
zipOutputStream.putNextEntry(ZipEntry(fileName))
|
||||||
zipOutputStream.write(byteArray)
|
zipOutputStream.write(byteArray)
|
||||||
zipOutputStream.closeEntry()
|
zipOutputStream.closeEntry()
|
||||||
|
zipOutputStream.finish()
|
||||||
return zipOutputStream.use {
|
return zipOutputStream.use {
|
||||||
byteOut.use {
|
byteOut.use {
|
||||||
byteOut.toByteArray()
|
byteOut.toByteArray()
|
||||||
@@ -183,11 +184,11 @@ object ZipUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
BufferedInputStream(FileInputStream(srcFile)).use { `is` ->
|
BufferedInputStream(FileInputStream(srcFile)).use {
|
||||||
val entry = ZipEntry(rootPath1)
|
val entry = ZipEntry(rootPath1)
|
||||||
entry.comment = comment
|
entry.comment = comment
|
||||||
zos.putNextEntry(entry)
|
zos.putNextEntry(entry)
|
||||||
zos.write(`is`.readBytes())
|
it.copyTo(zos)
|
||||||
zos.closeEntry()
|
zos.closeEntry()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user