|
|
|
@ -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( |
|
|
|
|