From e26c2dcbbfd2828def1f0603fe9c8d5c6f4e1da7 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 15 May 2022 18:05:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/api/controller/BookController.kt | 22 +++++++++++++++++++ .../io/legado/app/help/storage/AppWebDav.kt | 21 ++++++++++++++---- .../main/java/io/legado/app/web/HttpServer.kt | 1 + 3 files changed, 40 insertions(+), 4 deletions(-) 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 def3c4678..a4571bd74 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 @@ -5,6 +5,7 @@ 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.data.entities.BookProgress import io.legado.app.data.entities.BookSource import io.legado.app.help.BookHelp import io.legado.app.help.CacheManager @@ -209,6 +210,27 @@ object BookController { return returnData.setErrorMsg("格式不对") } + /** + * 保存进度 + */ + fun saveBookProgress(postData: String?): ReturnData { + val returnData = ReturnData() + GSON.fromJsonObject(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("格式不对") + } + /** * 添加本地书籍 */ diff --git a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt b/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt index e219ee59c..d8b38d316 100644 --- a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt +++ b/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt @@ -194,15 +194,28 @@ object AppWebDav { Coroutine.async { val bookProgress = BookProgress(book) val json = GSON.toJson(bookProgress) - val url = getProgressUrl(book) + val url = getProgressUrl(book.name, book.author) WebDav(url, authorization).upload(json.toByteArray(), "application/json") }.onError { AppLog.put("上传进度失败\n${it.localizedMessage}") } } - private fun getProgressUrl(book: Book): String { - return bookProgressUrl + book.name + "_" + book.author + ".json" + fun uploadBookProgress(bookProgress: BookProgress) { + 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? { authorization?.let { - val url = getProgressUrl(book) + val url = getProgressUrl(book.name, book.author) kotlin.runCatching { WebDav(url, it).download().let { byteArray -> val json = String(byteArray) 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 f593c0647..a141b0c9f 100644 --- a/app/src/main/java/io/legado/app/web/HttpServer.kt +++ b/app/src/main/java/io/legado/app/web/HttpServer.kt @@ -42,6 +42,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) { "/saveBookSources" -> BookSourceController.saveSources(postData) "/deleteBookSources" -> BookSourceController.deleteSources(postData) "/saveBook" -> BookController.saveBook(postData) + "/saveBookProgress" -> BookController.saveBookProgress(postData) "/addLocalBook" -> BookController.addLocalBook(session.parameters) "/saveReadConfig" -> BookController.saveWebReadConfig(postData) "/saveRssSource" -> RssSourceController.saveSource(postData)