From e6e6df4fe21361457e93110b8383f96526bf615a Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 12 Aug 2021 09:54:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 4 ++ .../assets/web/uploadBook/js/html5_fun.js | 71 ++++++++++--------- .../app/api/controller/BookController.kt | 23 +++--- .../main/java/io/legado/app/web/HttpServer.kt | 2 +- 4 files changed, 51 insertions(+), 49 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index edce5391c..bc9e39cd6 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -14,6 +14,10 @@ **2021/08/09** +1. web传书可以使用 + +**2021/08/09** + 1. 修复选择文字不能选择单个文字的bug 2. 分组可选择封面 diff --git a/app/src/main/assets/web/uploadBook/js/html5_fun.js b/app/src/main/assets/web/uploadBook/js/html5_fun.js index 929515fad..199682632 100644 --- a/app/src/main/assets/web/uploadBook/js/html5_fun.js +++ b/app/src/main/assets/web/uploadBook/js/html5_fun.js @@ -123,41 +123,46 @@ try { //正在上传 isUploading = true; //设置上传的数据 - var fd = new FormData(); - fd.append("Filename", file.name); - fd.append("Filedata", file); - //设置当前的上传对象 - currUploadfile = file; - - if (XHR.readyState > 0) { - XHR = new XMLHttpRequest(); + var reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = function (e) { + var data = e.target.result; + var fd = new FormData(); + fd.append("fileName", file.name); + fd.append("fileData", data); + //设置当前的上传对象 + currUploadfile = file; + + if (XHR.readyState > 0) { + XHR = new XMLHttpRequest(); + } + + XHR.upload.addEventListener("progress", progress, false); + XHR.upload.addEventListener("load", requestLoad, false); + XHR.upload.addEventListener("error", error, false); + XHR.upload.addEventListener("abort", abort, false); + XHR.upload.addEventListener("loadend", loadend, false); + XHR.upload.addEventListener("loadstart", loadstart, false); + XHR.open("POST", config.url); + XHR.send(fd); + XHR.onreadystatechange = function () { + + //只要上传完成不管成功失败 + if (XHR.readyState == 4) { + console.log("onreadystatechange ", XHR.status, +new Date()); + + if (XHR.status == 200) { + uploadSuccess(currUploadfile, {}, XHR.status) + } else { + uploadError() + } + + //进行下一个上传 + nextUpload() + } + }; } - XHR.upload.addEventListener("progress", progress, false); - XHR.upload.addEventListener("load", requestLoad, false); - XHR.upload.addEventListener("error", error, false); - XHR.upload.addEventListener("abort", abort, false); - XHR.upload.addEventListener("loadend", loadend, false); - XHR.upload.addEventListener("loadstart", loadstart, false); - XHR.open("POST", config.url); - XHR.send(fd); - XHR.onreadystatechange = function () { - - //只要上传完成不管成功失败 - if (XHR.readyState == 4) { - console.log("onreadystatechange ", XHR.status, +new Date()); - - if (XHR.status == 200) { - uploadSuccess(currUploadfile, {}, XHR.status) - } else { - uploadError() - } - - //进行下一个上传 - nextUpload() - } - }; - } //请求完成,无论失败或成功 diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index 4de2b34d6..7634c65c9 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -1,7 +1,7 @@ package io.legado.app.api.controller +import android.util.Base64 import androidx.core.graphics.drawable.toBitmap -import fi.iki.elonen.NanoHTTPD import io.legado.app.R import io.legado.app.api.ReturnData import io.legado.app.constant.PreferKey @@ -19,7 +19,6 @@ import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.utils.* import kotlinx.coroutines.runBlocking import splitties.init.appCtx -import java.io.FileOutputStream object BookController { @@ -192,22 +191,16 @@ object BookController { } } - fun addLocalBook(session: NanoHTTPD.IHTTPSession): ReturnData { + fun addLocalBook(parameters: Map>): ReturnData { val returnData = ReturnData() try { - val fileName = session.parameters["file"]?.firstOrNull() - val contentLength = session.headers["content-length"]?.toInt() - fileName ?: let { - return returnData.setErrorMsg("文件名为空") - } - contentLength ?: let { - return returnData.setErrorMsg("文件长度为空") - } + val fileName = parameters["fileName"]?.firstOrNull() + ?: return returnData.setErrorMsg("fileName 不能为空") + val fileData = parameters["fileData"]?.firstOrNull() + ?: return returnData.setErrorMsg("fileData 不能为空") val file = FileUtils.createFileIfNotExist(LocalBook.cacheFolder, fileName) - val outputStream = FileOutputStream(file) - session.inputStream.copyTo(outputStream, contentLength) - outputStream.close() - session.inputStream.close() + val fileBytes = Base64.decode(fileData.substringAfter("base64,"), Base64.DEFAULT) + file.writeBytes(fileBytes) val nameAuthor = LocalBook.analyzeNameAuthor(fileName) val book = Book( bookUrl = file.absolutePath, diff --git a/app/src/main/java/io/legado/app/web/HttpServer.kt b/app/src/main/java/io/legado/app/web/HttpServer.kt index c89f6a60d..7a1bf50fe 100644 --- a/app/src/main/java/io/legado/app/web/HttpServer.kt +++ b/app/src/main/java/io/legado/app/web/HttpServer.kt @@ -42,7 +42,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) { "/saveSources" -> SourceController.saveSources(postData) "/saveBook" -> BookController.saveBook(postData) "/deleteSources" -> SourceController.deleteSources(postData) - "/addLocalBook" -> BookController.addLocalBook(session) + "/addLocalBook" -> BookController.addLocalBook(session.parameters) else -> null } }