添加上传书籍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 //webServer
implementation('org.nanohttpd:nanohttpd:2.3.1') implementation('org.nanohttpd:nanohttpd:2.3.1')
implementation('org.nanohttpd:nanohttpd-websocket: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') implementation('com.github.jenly1314:zxing-lite:2.1.0')

@ -1,6 +1,8 @@
package io.legado.app.api.controller package io.legado.app.api.controller
import androidx.core.graphics.drawable.toBitmap 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.R
import io.legado.app.api.ReturnData import io.legado.app.api.ReturnData
import io.legado.app.constant.PreferKey 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.BookHelp
import io.legado.app.help.ContentProcessor import io.legado.app.help.ContentProcessor
import io.legado.app.help.ImageLoader 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.LocalBook
import io.legado.app.model.localBook.UmdFile
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.help.ReadBook import io.legado.app.service.help.ReadBook
import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.apache.commons.fileupload.disk.DiskFileItemFactory
import splitties.init.appCtx import splitties.init.appCtx
object BookController { 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 { object LocalBook {
private const val folderName = "bookTxt" private const val folderName = "bookTxt"
val cacheFolder: File by lazy { val cacheFolder: File by lazy {
val rootFile = appCtx.getExternalFilesDir(null) FileUtils.createFolderIfNotExist(appCtx.externalFiles, folderName)
?: appCtx.externalCacheDir
?: appCtx.cacheDir
FileUtils.createFolderIfNotExist(rootFile, folderName)
} }
fun getChapterList(book: Book): ArrayList<BookChapter> { fun getChapterList(book: Book): ArrayList<BookChapter> {
@ -34,7 +31,7 @@ object LocalBook {
UmdFile.getChapterList(book) UmdFile.getChapterList(book)
} }
else -> { else -> {
AnalyzeTxtFile().analyze(book) TextFile().analyze(book)
} }
} }
} }
@ -48,7 +45,7 @@ object LocalBook {
UmdFile.getContent(book, chapter) UmdFile.getContent(book, chapter)
} }
else -> { else -> {
AnalyzeTxtFile.getContent(book, chapter) TextFile.getContent(book, chapter)
} }
} }
} }
@ -73,8 +70,28 @@ object LocalBook {
path = uri.path!! path = uri.path!!
File(path).name 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 name: String
val author: String val author: String
@ -120,22 +137,7 @@ object LocalBook {
} }
} }
return Pair(name, author)
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
} }
fun deleteBook(book: Book, deleteOriginal: Boolean) { fun deleteBook(book: Book, deleteOriginal: Boolean) {

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

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

Loading…
Cancel
Save