添加刷新目录的API

pull/1006/head
gedoor 4 years ago
parent d8214817aa
commit 334356ce92
  1. 15
      app/src/main/java/io/legado/app/api/ReaderProvider.kt
  2. 59
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  3. 29
      app/src/main/java/io/legado/app/web/HttpServer.kt

@ -10,7 +10,7 @@ import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import com.google.gson.Gson
import io.legado.app.api.controller.BookshelfController
import io.legado.app.api.controller.BookController
import io.legado.app.api.controller.SourceController
import java.util.*
@ -19,7 +19,8 @@ import java.util.*
*/
class ReaderProvider : ContentProvider() {
private enum class RequestCode {
SaveSource, SaveSources, SaveBook, DeleteSources, GetSource, GetSources, GetBookshelf, GetChapterList, GetBookContent
SaveSource, SaveSources, SaveBook, DeleteSources, GetSource, GetSources,
GetBookshelf, RefreshToc, GetChapterList, GetBookContent
}
private val postBodyKey = "json"
@ -33,6 +34,7 @@ class ReaderProvider : ContentProvider() {
addURI(authority, "source/query", RequestCode.GetSource.ordinal)
addURI(authority, "sources/query", RequestCode.GetSources.ordinal)
addURI(authority, "books/query", RequestCode.GetBookshelf.ordinal)
addURI(authority, "book/refreshToc/query", RequestCode.RefreshToc.ordinal)
addURI(authority, "book/chapter/query", RequestCode.GetChapterList.ordinal)
addURI(authority, "book/content/query", RequestCode.GetBookContent.ordinal)
}
@ -65,7 +67,7 @@ class ReaderProvider : ContentProvider() {
SourceController.saveSource(values.getAsString(postBodyKey))
}
RequestCode.SaveBook -> values?.let {
BookshelfController.saveBook(values.getAsString(postBodyKey))
BookController.saveBook(values.getAsString(postBodyKey))
}
RequestCode.SaveSources -> values?.let {
SourceController.saveSources(values.getAsString(postBodyKey))
@ -91,9 +93,10 @@ class ReaderProvider : ContentProvider() {
return if (sMatcher.match(uri) < 0) null else when (RequestCode.values()[sMatcher.match(uri)]) {
RequestCode.GetSource -> SimpleCursor(SourceController.getSource(map))
RequestCode.GetSources -> SimpleCursor(SourceController.sources)
RequestCode.GetBookshelf -> SimpleCursor(BookshelfController.bookshelf)
RequestCode.GetBookContent -> SimpleCursor(BookshelfController.getBookContent(map))
RequestCode.GetChapterList -> SimpleCursor(BookshelfController.getChapterList(map))
RequestCode.GetBookshelf -> SimpleCursor(BookController.bookshelf)
RequestCode.GetBookContent -> SimpleCursor(BookController.getBookContent(map))
RequestCode.RefreshToc -> SimpleCursor(BookController.refreshToc(map))
RequestCode.GetChapterList -> SimpleCursor(BookController.getChapterList(map))
else -> throw IllegalStateException(
"Unexpected value: " + RequestCode.values()[sMatcher.match(uri)].name
)

@ -1,10 +1,12 @@
package io.legado.app.api.controller
import io.legado.app.R
import io.legado.app.api.ReturnData
import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.help.BookHelp
import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.webBook.WebBook
import io.legado.app.service.help.ReadBook
import io.legado.app.utils.GSON
@ -14,8 +16,11 @@ import io.legado.app.utils.getPrefInt
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
object BookshelfController {
object BookController {
/**
* 书架所有书籍
*/
val bookshelf: ReturnData
get() {
val books = appDb.bookDao.all
@ -35,6 +40,55 @@ object BookshelfController {
}
}
/**
* 更新目录
*/
fun refreshToc(parameters: Map<String, List<String>>): ReturnData {
val returnData = ReturnData()
try {
val bookUrl = parameters["url"]?.getOrNull(0)
if (bookUrl.isNullOrEmpty()) {
return returnData.setErrorMsg("参数url不能为空,请指定书籍地址")
}
val book = appDb.bookDao.getBook(bookUrl)
?: return returnData.setErrorMsg("bookUrl不对")
if (book.isLocalBook()) {
val toc = LocalBook.getChapterList(book)
appDb.bookChapterDao.delByBook(book.bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray())
appDb.bookDao.update(book)
return if (toc.isEmpty()) {
returnData.setErrorMsg(appCtx.getString(R.string.error_load_toc))
} else {
returnData.setData(toc)
}
} else {
val bookSource = appDb.bookSourceDao.getBookSource(book.origin)
?: return returnData.setErrorMsg("未找到对应书源,请换源")
val webBook = WebBook(bookSource)
val toc = runBlocking {
if (book.tocUrl.isBlank()) {
webBook.getBookInfoAwait(this, book)
}
webBook.getChapterListAwait(this, book)
}
appDb.bookChapterDao.delByBook(book.bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray())
appDb.bookDao.update(book)
return if (toc.isEmpty()) {
returnData.setErrorMsg(appCtx.getString(R.string.error_load_toc))
} else {
returnData.setData(toc)
}
}
} catch (e: Exception) {
return returnData.setErrorMsg(e.localizedMessage ?: "refresh toc error")
}
}
/**
* 获取目录
*/
fun getChapterList(parameters: Map<String, List<String>>): ReturnData {
val bookUrl = parameters["url"]?.getOrNull(0)
val returnData = ReturnData()
@ -45,6 +99,9 @@ object BookshelfController {
return returnData.setData(chapterList)
}
/**
* 获取正文
*/
fun getBookContent(parameters: Map<String, List<String>>): ReturnData {
val bookUrl = parameters["url"]?.getOrNull(0)
val index = parameters["index"]?.getOrNull(0)?.toInt()

@ -3,7 +3,7 @@ package io.legado.app.web
import com.google.gson.Gson
import fi.iki.elonen.NanoHTTPD
import io.legado.app.api.ReturnData
import io.legado.app.api.controller.BookshelfController
import io.legado.app.api.controller.BookController
import io.legado.app.api.controller.SourceController
import io.legado.app.web.utils.AssetsWeb
import java.util.*
@ -32,25 +32,26 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
session.parseBody(files)
val postData = files["postData"]
when (uri) {
"/saveSource" -> returnData = SourceController.saveSource(postData)
"/saveSources" -> returnData = SourceController.saveSources(postData)
"/saveBook" -> returnData = BookshelfController.saveBook(postData)
"/deleteSources" -> returnData = SourceController.deleteSources(postData)
returnData = when (uri) {
"/saveSource" -> SourceController.saveSource(postData)
"/saveSources" -> SourceController.saveSources(postData)
"/saveBook" -> BookController.saveBook(postData)
"/deleteSources" -> SourceController.deleteSources(postData)
else -> null
}
}
"GET" -> {
val parameters = session.parameters
when (uri) {
"/getSource" -> returnData = SourceController.getSource(parameters)
"/getSources" -> returnData = SourceController.sources
"/getBookshelf" -> returnData = BookshelfController.bookshelf
"/getChapterList" ->
returnData = BookshelfController.getChapterList(parameters)
"/getBookContent" ->
returnData = BookshelfController.getBookContent(parameters)
returnData = when (uri) {
"/getSource" -> SourceController.getSource(parameters)
"/getSources" -> SourceController.sources
"/getBookshelf" -> BookController.bookshelf
"/getChapterList" -> BookController.getChapterList(parameters)
"/refreshToc" -> BookController.refreshToc(parameters)
"/getBookContent" -> BookController.getBookContent(parameters)
else -> null
}
}
}

Loading…
Cancel
Save