Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/src/main/java/io/legado/app/data/dao/BookDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ interface BookDao {
@Query("SELECT * FROM books WHERE (`group` & :group) > 0")
fun getBooksByGroup(group: Long): List<Book>

@Query("SELECT * FROM books where local_modified > cloud_modified")
fun getNeedPush(): List<Book>
@get:Query("SELECT * FROM books where local_modified > cloud_modified")
val needPush: List<Book>

@Query("update books set cloud_modified = :cloudModified, local_modified = :cloudModified where bookUrl = :bookUrl and local_modified <= :cloudModified")
fun markSynced(bookUrl: String, cloudModified: Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.legado.app.data.entities.ReadRecord
import io.legado.app.data.entities.SyncConflict
import io.legado.app.lib.webdav.WebDavFile
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.sha1Encode
import java.io.File

Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/io/legado/app/help/sync/SyncClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.legado.app.lib.webdav.Authorization
import io.legado.app.lib.webdav.WebDav
import io.legado.app.lib.webdav.WebDavFile
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.sha1Encode

/**
Expand Down Expand Up @@ -208,7 +209,7 @@ object SyncClient {

// ============================ 冲突处理 ============================

private fun pullUpdate(
private suspend fun pullUpdate(
file: WebDavFile,
local: Any?,
lastPullAt: Long,
Expand Down Expand Up @@ -331,11 +332,11 @@ object SyncClient {

// ============================ 工具 ============================

private fun listRemoteFiles(url: String): List<WebDavFile> {
private suspend fun listRemoteFiles(url: String): List<WebDavFile> {
return webDav(url).listFiles()
}

private fun listRemoteFiles(type: DataSyncType): List<WebDavFile> {
private suspend fun listRemoteFiles(type: DataSyncType): List<WebDavFile> {
return listRemoteFiles(dirUrl(type))
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/io/legado/app/help/sync/SyncLedger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.legado.app.help.sync

import io.legado.app.help.config.LocalConfig
import io.legado.app.utils.putLong
import io.legado.app.utils.putString
import io.legado.app.utils.remove

/**
* 同步状态记录, 持久化到 LocalConfig 共享参数
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/io/legado/app/help/sync/SyncManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.legado.app.help.sync

import io.legado.app.help.AppWebDav
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.coroutine.onError
import io.legado.app.utils.printOnDebug
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.sync.Mutex
Expand All @@ -27,7 +26,7 @@ object SyncManager {
syncAll()
}.onError {
it.printOnDebug()
it.toastOnUi()
appCtx.toastOnUi(it.localizedMessage)
SyncLedger.lastResult = "同步失败: ${it.localizedMessage}"
}.invokeOnCompletion {
onFinish?.invoke()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object WorkManagerHelper {
cancel(context)
return
}
val interval = SyncConfig.intervalMinutes.coerceAtLeast(MIN_INTERVAL)
val interval = SyncConfig.intervalMinutes.toLong().coerceAtLeast(MIN_INTERVAL)
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/io/legado/app/utils/MD5Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ object MD5Utils {
return SHA1Digester.digestHex(str)
}
}

fun String.sha1Encode(): String = MD5Utils.sha1Encode(this)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use a dedicated SHA-1 digester

When incremental sync calls this extension on a thread that has already used MD5Utils.md5Encode, it returns an MD5 digest instead of SHA-1 because MD5Digester and SHA1Digester share the same ThreadLocal<Digester>. Since the sync client uses this value for WebDAV filenames, different threads/devices can write and look up different names for the same record, causing missed pulls or duplicate cloud entries; keep separate thread-local digesters or instantiate SHA-1 independently here.

Useful? React with 👍 / 👎.