优化
This commit is contained in:
@@ -19,6 +19,7 @@ import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.withContext
|
||||
import splitties.init.appCtx
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
@@ -153,9 +154,11 @@ object Backup {
|
||||
path.isNullOrBlank() -> {
|
||||
copyBackup(context.getExternalFilesDir(null)!!, "backup.zip")
|
||||
}
|
||||
|
||||
path.isContentScheme() -> {
|
||||
copyBackup(context, Uri.parse(path), "backup.zip")
|
||||
}
|
||||
|
||||
else -> {
|
||||
copyBackup(File(path), "backup.zip")
|
||||
}
|
||||
@@ -168,8 +171,10 @@ object Backup {
|
||||
private fun writeListToJson(list: List<Any>, fileName: String, path: String) {
|
||||
if (list.isNotEmpty()) {
|
||||
val file = FileUtils.createFileIfNotExist(path + File.separator + fileName)
|
||||
FileOutputStream(file).use {
|
||||
GSON.writeToOutputStream(it, list)
|
||||
FileOutputStream(file).use { fos ->
|
||||
BufferedOutputStream(fos, 64 * 1024).use {
|
||||
GSON.writeToOutputStream(it, list)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
loadChapter(it, scope)
|
||||
}
|
||||
}.onError {
|
||||
AppLog.put("获取数据信息失败\n${it.localizedMessage}", it)
|
||||
AppLog.put("获取书籍信息失败\n${it.localizedMessage}", it)
|
||||
context.toastOnUi(R.string.error_get_book_info)
|
||||
}
|
||||
} ?: let {
|
||||
@@ -305,7 +305,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
fun importArchiveBook(
|
||||
archiveFileUri: Uri,
|
||||
archiveEntryName: String,
|
||||
|
||||
@@ -7,10 +7,10 @@ import io.legado.app.api.controller.BookController
|
||||
import io.legado.app.api.controller.BookSourceController
|
||||
import io.legado.app.api.controller.ReplaceRuleController
|
||||
import io.legado.app.api.controller.RssSourceController
|
||||
import io.legado.app.help.coroutine.Coroutine
|
||||
import io.legado.app.service.WebService
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.web.utils.AssetsWeb
|
||||
import splitties.init.appCtx
|
||||
import java.io.*
|
||||
|
||||
class HttpServer(port: Int) : NanoHTTPD(port) {
|
||||
@@ -33,6 +33,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
|
||||
//response.addHeader("Access-Control-Max-Age", "3600");
|
||||
return response
|
||||
}
|
||||
|
||||
Method.POST -> {
|
||||
val files = HashMap<String, String>()
|
||||
session.parseBody(files)
|
||||
@@ -56,6 +57,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
Method.GET -> {
|
||||
val parameters = session.parameters
|
||||
|
||||
@@ -75,6 +77,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
|
||||
@@ -97,29 +100,24 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
|
||||
byteArray.size.toLong()
|
||||
)
|
||||
} else {
|
||||
try {
|
||||
val data = returnData.data
|
||||
if (data is List<*> && data.size > 3000) {
|
||||
throw OutOfMemoryError()
|
||||
val data = returnData.data
|
||||
if (data is List<*> && data.size > 1000) {
|
||||
val pis = PipedInputStream(1024 * 1024)
|
||||
Coroutine.async {
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
PipedOutputStream(pis).use { out ->
|
||||
BufferedWriter(OutputStreamWriter(out, "UTF-8")).use {
|
||||
GSON.toJson(returnData, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
newFixedLengthResponse(GSON.toJson(returnData))
|
||||
} catch (e: OutOfMemoryError) {
|
||||
val path = FileUtils.getPath(
|
||||
appCtx.externalFiles,
|
||||
"book_cache",
|
||||
"bookSources.json"
|
||||
)
|
||||
val file = FileUtils.createFileIfNotExist(path)
|
||||
BufferedWriter(FileWriter(file), 128 * 1024).use {
|
||||
GSON.toJson(returnData, it)
|
||||
}
|
||||
val fis = FileInputStream(file)
|
||||
newFixedLengthResponse(
|
||||
newChunkedResponse(
|
||||
Response.Status.OK,
|
||||
"application/json",
|
||||
fis,
|
||||
fis.available().toLong()
|
||||
pis
|
||||
)
|
||||
} else {
|
||||
newFixedLengthResponse(GSON.toJson(returnData))
|
||||
}
|
||||
}
|
||||
response.addHeader("Access-Control-Allow-Methods", "GET, POST")
|
||||
|
||||
Reference in New Issue
Block a user