添加上传书籍API

pull/1172/head
gedoor 3 years ago
parent 751b0d234c
commit 3e523db296
  1. 1
      app/build.gradle
  2. 32
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  3. 48
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  4. 2
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt
  5. 3
      app/src/main/java/io/legado/app/web/HttpServer.kt

@ -199,6 +199,7 @@ dependencies {
//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')
//
implementation('com.github.jenly1314:zxing-lite:2.1.0')

@ -1,6 +1,8 @@
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
import io.legado.app.constant.PreferKey
@ -9,12 +11,15 @@ import io.legado.app.data.entities.Book
import io.legado.app.help.BookHelp
import io.legado.app.help.ContentProcessor
import io.legado.app.help.ImageLoader
import io.legado.app.model.localBook.EpubFile
import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.localBook.UmdFile
import io.legado.app.model.webBook.WebBook
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
object BookController {
@ -188,4 +193,31 @@ object BookController {
}
}
private val bookFileFactory by lazy {
DiskFileItemFactory(0, LocalBook.cacheFolder)
}
fun addLocalBook(session: NanoHTTPD.IHTTPSession): ReturnData {
val returnData = ReturnData()
NanoFileUpload(bookFileFactory).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)
}
return returnData.setData(true)
}
}

@ -19,10 +19,7 @@ import javax.script.SimpleBindings
object LocalBook {
private const val folderName = "bookTxt"
val cacheFolder: File by lazy {
val rootFile = appCtx.getExternalFilesDir(null)
?: appCtx.externalCacheDir
?: appCtx.cacheDir
FileUtils.createFolderIfNotExist(rootFile, folderName)
FileUtils.createFolderIfNotExist(appCtx.externalFiles, folderName)
}
fun getChapterList(book: Book): ArrayList<BookChapter> {
@ -34,7 +31,7 @@ object LocalBook {
UmdFile.getChapterList(book)
}
else -> {
AnalyzeTxtFile().analyze(book)
TextFile().analyze(book)
}
}
}
@ -48,7 +45,7 @@ object LocalBook {
UmdFile.getContent(book, chapter)
}
else -> {
AnalyzeTxtFile.getContent(book, chapter)
TextFile.getContent(book, chapter)
}
}
}
@ -73,8 +70,28 @@ object LocalBook {
path = uri.path!!
File(path).name
})
val tempFileName = fileName.replace(Regex("\\.txt$"), "")
val nameAuthor = analyzeNameAuthor(fileName)
val book = Book(
bookUrl = path,
name = nameAuthor.first,
author = nameAuthor.second,
originName = fileName,
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)
return book
}
fun analyzeNameAuthor(fileName: String): Pair<String, String> {
val tempFileName = fileName.substringBeforeLast(".")
val name: String
val author: String
@ -120,22 +137,7 @@ object LocalBook {
}
}
val book = Book(
bookUrl = path,
name = name,
author = author,
originName = fileName,
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)
return book
return Pair(name, author)
}
fun deleteBook(book: Book, deleteOriginal: Boolean) {

@ -14,7 +14,7 @@ import java.nio.charset.Charset
import java.util.regex.Matcher
import java.util.regex.Pattern
class AnalyzeTxtFile {
class TextFile {
private val tocRules = arrayListOf<TxtTocRule>()
private lateinit var charset: Charset

@ -30,7 +30,6 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
//response.addHeader("Access-Control-Max-Age", "3600");
return response
}
"POST" -> {
val files = HashMap<String, String>()
session.parseBody(files)
@ -41,10 +40,10 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
"/saveSources" -> SourceController.saveSources(postData)
"/saveBook" -> BookController.saveBook(postData)
"/deleteSources" -> SourceController.deleteSources(postData)
"/addLocalBook" -> BookController.addLocalBook(session)
else -> null
}
}
"GET" -> {
val parameters = session.parameters

Loading…
Cancel
Save