diff --git a/app/build.gradle b/app/build.gradle index f43a57a42..14165a485 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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') 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 659dc2078..fe09727f4 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,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) + } + } diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index ea4ef0929..5bf94834a 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -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 { @@ -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 { + 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) { diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/TextFile.kt similarity index 99% rename from app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt rename to app/src/main/java/io/legado/app/model/localBook/TextFile.kt index 938aa06c8..cec006ac0 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/TextFile.kt @@ -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() private lateinit var charset: Charset 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 c3a5b125e..6678d52cf 100644 --- a/app/src/main/java/io/legado/app/web/HttpServer.kt +++ b/app/src/main/java/io/legado/app/web/HttpServer.kt @@ -30,7 +30,6 @@ class HttpServer(port: Int) : NanoHTTPD(port) { //response.addHeader("Access-Control-Max-Age", "3600"); return response } - "POST" -> { val files = HashMap() 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