pull/1890/head
kunfei 3 years ago
parent f89dd56fdc
commit e26c2dcbbf
  1. 22
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 21
      app/src/main/java/io/legado/app/help/storage/AppWebDav.kt
  3. 1
      app/src/main/java/io/legado/app/web/HttpServer.kt

@ -5,6 +5,7 @@ import io.legado.app.api.ReturnData
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookProgress
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.CacheManager import io.legado.app.help.CacheManager
@ -209,6 +210,27 @@ object BookController {
return returnData.setErrorMsg("格式不对") return returnData.setErrorMsg("格式不对")
} }
/**
* 保存进度
*/
fun saveBookProgress(postData: String?): ReturnData {
val returnData = ReturnData()
GSON.fromJsonObject<BookProgress>(postData).getOrNull()?.let { bookProgress ->
appDb.bookDao.getBook(bookProgress.name, bookProgress.author)?.let { book ->
book.durChapterIndex = bookProgress.durChapterIndex
book.durChapterPos = bookProgress.durChapterPos
appDb.bookDao.update(book)
AppWebDav.uploadBookProgress(bookProgress)
if (ReadBook.book?.bookUrl == book.bookUrl) {
ReadBook.book = book
ReadBook.durChapterIndex = book.durChapterIndex
}
return returnData.setData("")
}
}
return returnData.setErrorMsg("格式不对")
}
/** /**
* 添加本地书籍 * 添加本地书籍
*/ */

@ -194,15 +194,28 @@ object AppWebDav {
Coroutine.async { Coroutine.async {
val bookProgress = BookProgress(book) val bookProgress = BookProgress(book)
val json = GSON.toJson(bookProgress) val json = GSON.toJson(bookProgress)
val url = getProgressUrl(book) val url = getProgressUrl(book.name, book.author)
WebDav(url, authorization).upload(json.toByteArray(), "application/json") WebDav(url, authorization).upload(json.toByteArray(), "application/json")
}.onError { }.onError {
AppLog.put("上传进度失败\n${it.localizedMessage}") AppLog.put("上传进度失败\n${it.localizedMessage}")
} }
} }
private fun getProgressUrl(book: Book): String { fun uploadBookProgress(bookProgress: BookProgress) {
return bookProgressUrl + book.name + "_" + book.author + ".json" val authorization = authorization ?: return
if (!syncBookProgress) return
if (!NetworkUtils.isAvailable()) return
Coroutine.async {
val json = GSON.toJson(bookProgress)
val url = getProgressUrl(bookProgress.name, bookProgress.author)
WebDav(url, authorization).upload(json.toByteArray(), "application/json")
}.onError {
AppLog.put("上传进度失败\n${it.localizedMessage}")
}
}
private fun getProgressUrl(name: String, author: String): String {
return bookProgressUrl + name + "_" + author + ".json"
} }
/** /**
@ -210,7 +223,7 @@ object AppWebDav {
*/ */
suspend fun getBookProgress(book: Book): BookProgress? { suspend fun getBookProgress(book: Book): BookProgress? {
authorization?.let { authorization?.let {
val url = getProgressUrl(book) val url = getProgressUrl(book.name, book.author)
kotlin.runCatching { kotlin.runCatching {
WebDav(url, it).download().let { byteArray -> WebDav(url, it).download().let { byteArray ->
val json = String(byteArray) val json = String(byteArray)

@ -42,6 +42,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
"/saveBookSources" -> BookSourceController.saveSources(postData) "/saveBookSources" -> BookSourceController.saveSources(postData)
"/deleteBookSources" -> BookSourceController.deleteSources(postData) "/deleteBookSources" -> BookSourceController.deleteSources(postData)
"/saveBook" -> BookController.saveBook(postData) "/saveBook" -> BookController.saveBook(postData)
"/saveBookProgress" -> BookController.saveBookProgress(postData)
"/addLocalBook" -> BookController.addLocalBook(session.parameters) "/addLocalBook" -> BookController.addLocalBook(session.parameters)
"/saveReadConfig" -> BookController.saveWebReadConfig(postData) "/saveReadConfig" -> BookController.saveWebReadConfig(postData)
"/saveRssSource" -> RssSourceController.saveSource(postData) "/saveRssSource" -> RssSourceController.saveSource(postData)

Loading…
Cancel
Save