mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
a8c843a32c
commit
5bbd453d9f
@ -193,9 +193,9 @@ dependencies {
|
||||
implementation('com.github.bumptech.glide:glide:4.12.0')
|
||||
|
||||
//webServer
|
||||
implementation('org.nanohttpd:nanohttpd:2.3.1')
|
||||
implementation('org.nanohttpd:nanohttpd-websocket:2.3.1')
|
||||
implementation('org.nanohttpd:nanohttpd-apache-fileupload:2.3.1')
|
||||
def nanoHttpdVersion = "2.3.1"
|
||||
implementation("org.nanohttpd:nanohttpd:$nanoHttpdVersion")
|
||||
implementation("org.nanohttpd:nanohttpd-websocket:$nanoHttpdVersion")
|
||||
|
||||
//二维码
|
||||
implementation('com.github.jenly1314:zxing-lite:2.1.1')
|
||||
|
@ -126,7 +126,6 @@ try {
|
||||
var fd = new FormData();
|
||||
fd.append("Filename", file.name);
|
||||
fd.append("Filedata", file);
|
||||
fd.append("Upload", "Submit Query");
|
||||
//设置当前的上传对象
|
||||
currUploadfile = file;
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
package io.legado.app.api.controller
|
||||
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import fi.iki.elonen.NanoFileUpload
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
import io.legado.app.R
|
||||
import io.legado.app.api.ReturnData
|
||||
@ -19,8 +18,8 @@ import io.legado.app.service.help.ReadBook
|
||||
import io.legado.app.ui.widget.image.CoverImageView
|
||||
import io.legado.app.utils.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory
|
||||
import splitties.init.appCtx
|
||||
import java.io.FileOutputStream
|
||||
|
||||
object BookController {
|
||||
|
||||
@ -193,32 +192,37 @@ object BookController {
|
||||
}
|
||||
}
|
||||
|
||||
private val uploader by lazy {
|
||||
val dif = DiskFileItemFactory(0, LocalBook.cacheFolder)
|
||||
NanoFileUpload(dif)
|
||||
}
|
||||
|
||||
fun addLocalBook(session: NanoHTTPD.IHTTPSession, postData: String?): ReturnData {
|
||||
fun addLocalBook(session: NanoHTTPD.IHTTPSession): ReturnData {
|
||||
val returnData = ReturnData()
|
||||
try {
|
||||
uploader.parseRequest(session).forEach {
|
||||
val path = FileUtils.getPath(LocalBook.cacheFolder, it.name)
|
||||
val nameAuthor = LocalBook.analyzeNameAuthor(it.name)
|
||||
val book = Book(
|
||||
bookUrl = path,
|
||||
name = nameAuthor.first,
|
||||
author = nameAuthor.second,
|
||||
originName = it.name,
|
||||
coverUrl = FileUtils.getPath(
|
||||
appCtx.externalFiles,
|
||||
"covers",
|
||||
"${MD5Utils.md5Encode16(path)}.jpg"
|
||||
)
|
||||
)
|
||||
if (book.isEpub()) EpubFile.upBookInfo(book)
|
||||
if (book.isUmd()) UmdFile.upBookInfo(book)
|
||||
appDb.bookDao.insert(book)
|
||||
val fileName = session.parameters["file"]?.firstOrNull()
|
||||
val contentLength = session.headers["content-length"]?.toInt()
|
||||
fileName ?: let {
|
||||
return returnData.setErrorMsg("文件名为空")
|
||||
}
|
||||
contentLength ?: let {
|
||||
return returnData.setErrorMsg("文件长度为空")
|
||||
}
|
||||
val file = FileUtils.createFileIfNotExist(LocalBook.cacheFolder, fileName)
|
||||
val outputStream = FileOutputStream(file)
|
||||
session.inputStream.copyTo(outputStream, contentLength)
|
||||
outputStream.close()
|
||||
session.inputStream.close()
|
||||
val nameAuthor = LocalBook.analyzeNameAuthor(fileName)
|
||||
val book = Book(
|
||||
bookUrl = file.absolutePath,
|
||||
name = nameAuthor.first,
|
||||
author = nameAuthor.second,
|
||||
originName = fileName,
|
||||
coverUrl = FileUtils.getPath(
|
||||
appCtx.externalFiles,
|
||||
"covers",
|
||||
"${MD5Utils.md5Encode16(file.absolutePath)}.jpg"
|
||||
)
|
||||
)
|
||||
if (book.isEpub()) EpubFile.upBookInfo(book)
|
||||
if (book.isUmd()) UmdFile.upBookInfo(book)
|
||||
appDb.bookDao.insert(book)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return returnData.setErrorMsg(
|
||||
|
@ -18,6 +18,8 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
|
||||
|
||||
override fun serve(session: IHTTPSession): Response {
|
||||
var returnData: ReturnData? = null
|
||||
val ct = ContentType(session.headers["content-type"]).tryUTF8()
|
||||
session.headers["content-type"] = ct.contentTypeHeader
|
||||
var uri = session.uri
|
||||
|
||||
try {
|
||||
@ -40,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, postData)
|
||||
"/addLocalBook" -> BookController.addLocalBook(session)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user