pull/1738/head
kunfei 3 years ago
parent e740202869
commit eada533161
  1. 2
      app/src/main/java/io/legado/app/App.kt
  2. 20
      app/src/main/java/io/legado/app/help/storage/AppWebDav.kt
  3. 6
      app/src/main/java/io/legado/app/help/storage/BackupConfig.kt
  4. 13
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@ -43,6 +43,8 @@ class App : MultiDexApplication() {
registerActivityLifecycleCallbacks(LifecycleHelp)
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig)
Coroutine.async {
//WebDav初始化
AppWebDav.initConfig()
//初始化封面
BookCover.toString()
//清除过期数据

@ -28,6 +28,8 @@ object AppWebDav {
private val bookProgressUrl = "${rootWebDavUrl}bookProgress/"
private val zipFilePath = "${appCtx.externalFiles.absolutePath}${File.separator}backup.zip"
var isOk = false
private val rootWebDavUrl: String
get() {
val configUrl = appCtx.getPrefString(PreferKey.webDavUrl)?.trim()
@ -49,23 +51,21 @@ object AppWebDav {
}
@Throws(Exception::class)
suspend fun initWebDav(): Boolean {
suspend fun initConfig() {
isOk = false
val account = appCtx.getPrefString(PreferKey.webDavAccount)
val password = appCtx.getPrefString(PreferKey.webDavPassword)
if (!account.isNullOrBlank() && !password.isNullOrBlank()) {
HttpAuth.auth = HttpAuth.Auth(account, password)
WebDav(rootWebDavUrl).makeAsDir()
WebDav(bookProgressUrl).makeAsDir()
return true
isOk = WebDav(rootWebDavUrl).makeAsDir() && WebDav(bookProgressUrl).makeAsDir()
}
return false
}
@Throws(Exception::class)
private suspend fun getWebDavFileNames(): ArrayList<String> {
val url = rootWebDavUrl
val names = arrayListOf<String>()
if (initWebDav()) {
if (isOk) {
var files = WebDav(url).listFiles()
files = files.reversed()
files.forEach {
@ -120,7 +120,7 @@ object AppWebDav {
suspend fun backUpWebDav(path: String) {
try {
if (initWebDav() && NetworkUtils.isAvailable()) {
if (isOk && NetworkUtils.isAvailable()) {
val paths = arrayListOf(*Backup.backupFileNames)
for (i in 0 until paths.size) {
paths[i] = path + File.separator + paths[i]
@ -138,7 +138,7 @@ object AppWebDav {
suspend fun exportWebDav(byteArray: ByteArray, fileName: String) {
try {
if (initWebDav() && NetworkUtils.isAvailable()) {
if (isOk && NetworkUtils.isAvailable()) {
// 默认导出到legado文件夹下exports目录
val exportsWebDavUrl = rootWebDavUrl + EncoderUtils.escape("exports") + "/"
// 在legado文件夹创建exports目录,如果不存在的话
@ -161,7 +161,7 @@ object AppWebDav {
val bookProgress = BookProgress(book)
val json = GSON.toJson(bookProgress)
val url = getProgressUrl(book)
if (initWebDav()) {
if (isOk) {
WebDav(url).upload(json.toByteArray(), "application/json")
}
}
@ -175,7 +175,7 @@ object AppWebDav {
* 获取书籍进度
*/
suspend fun getBookProgress(book: Book): BookProgress? {
if (initWebDav() && NetworkUtils.isAvailable()) {
if (isOk && NetworkUtils.isAvailable()) {
val url = getProgressUrl(book)
WebDav(url).download()?.let { byteArray ->
val json = String(byteArray)

@ -22,7 +22,11 @@ object BackupConfig {
PreferKey.themeMode,
PreferKey.bookshelfLayout,
PreferKey.showRss,
PreferKey.threadCount
PreferKey.threadCount,
PreferKey.webDavUrl,
PreferKey.webDavDir,
PreferKey.webDavAccount,
PreferKey.webDavPassword
)
//配置忽略标题

@ -60,8 +60,11 @@ import io.legado.app.ui.replace.ReplaceRuleActivity
import io.legado.app.ui.replace.edit.ReplaceEditActivity
import io.legado.app.ui.widget.dialog.TextDialog
import io.legado.app.utils.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
class ReadBookActivity : BaseReadBookActivity(),
View.OnTouchListener,
@ -245,13 +248,7 @@ class ReadBookActivity : BaseReadBookActivity(),
}
}
}
launch {
menu.findItem(R.id.menu_get_progress)?.isVisible =
withContext(IO) {
runCatching { AppWebDav.initWebDav() }
.getOrElse { false }
}
}
menu.findItem(R.id.menu_get_progress)?.isVisible = AppWebDav.isOk
}
}

Loading…
Cancel
Save