Merge branch 'master' of https://github.com/gedoor/legado
commit
04e183e7a2
@ -1,50 +1,50 @@ |
||||
package io.legado.app.help.storage |
||||
|
||||
import io.legado.app.App |
||||
import io.legado.app.data.entities.Book |
||||
import io.legado.app.data.entities.BookProgress |
||||
import io.legado.app.help.coroutine.Coroutine |
||||
import io.legado.app.lib.webdav.WebDav |
||||
import io.legado.app.utils.FileUtils |
||||
import io.legado.app.utils.GSON |
||||
import io.legado.app.utils.fromJsonArray |
||||
import io.legado.app.utils.fromJsonObject |
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext") |
||||
object SyncBookProgress { |
||||
const val fileName = "bookProgress.json" |
||||
private val file = FileUtils.createFileIfNotExist(App.INSTANCE.cacheDir, fileName) |
||||
private val webDavUrl = "${WebDavHelp.rootWebDavUrl}$fileName" |
||||
|
||||
fun uploadBookProgress() { |
||||
private val webDavUrl = "${WebDavHelp.rootWebDavUrl}bookProgress/" |
||||
|
||||
fun uploadBookProgress(book: Book) { |
||||
Coroutine.async { |
||||
val value = App.db.bookDao.allBookProgress |
||||
if (value.isNotEmpty()) { |
||||
val json = GSON.toJson(value) |
||||
file.writeText(json) |
||||
if (WebDavHelp.initWebDav()) { |
||||
WebDav(webDavUrl).upload(file.absolutePath) |
||||
} |
||||
val bookProgress = BookProgress( |
||||
name = book.name, |
||||
author = book.author, |
||||
durChapterIndex = book.durChapterIndex, |
||||
durChapterPos = book.durChapterPos, |
||||
durChapterTime = book.durChapterTime, |
||||
durChapterTitle = book.durChapterTitle |
||||
) |
||||
val json = GSON.toJson(bookProgress) |
||||
val url = getUrl(book) |
||||
if (WebDavHelp.initWebDav()) { |
||||
WebDav(webDavUrl).makeAsDir() |
||||
WebDav(url).upload(json.toByteArray()) |
||||
} |
||||
} |
||||
} |
||||
|
||||
fun downloadBookProgress() { |
||||
Coroutine.async { |
||||
if (WebDavHelp.initWebDav()) { |
||||
WebDav(webDavUrl).downloadTo(file.absolutePath, true) |
||||
if (file.exists()) { |
||||
val json = file.readText() |
||||
GSON.fromJsonArray<BookProgress>(json)?.forEach { |
||||
App.db.bookDao.upBookProgress( |
||||
bookUrl = it.bookUrl, |
||||
durChapterIndex = it.durChapterIndex, |
||||
durChapterPos = it.durChapterPos, |
||||
durChapterTime = it.durChapterTime, |
||||
durChapterTitle = it.durChapterTitle |
||||
) |
||||
} |
||||
fun getBookProgress(book: Book): BookProgress? { |
||||
if (WebDavHelp.initWebDav()) { |
||||
val url = getUrl(book) |
||||
WebDav(url).download()?.let { byteArray -> |
||||
val json = String(byteArray) |
||||
GSON.fromJsonObject<BookProgress>(json)?.let { |
||||
return it |
||||
} |
||||
} |
||||
} |
||||
return null |
||||
} |
||||
|
||||
private fun getUrl(book: Book): String { |
||||
return webDavUrl + book.name + "_" + book.author + ".json" |
||||
} |
||||
} |
Loading…
Reference in new issue