-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncManager.kt
More file actions
422 lines (370 loc) · 16.4 KB
/
Copy pathSyncManager.kt
File metadata and controls
422 lines (370 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package io.github.smiling_pixel.sync
import io.github.smiling_pixel.client.CloudDriveClient
import io.github.smiling_pixel.database.DiaryRepository
import io.github.smiling_pixel.model.DiaryEntry
import io.github.smiling_pixel.preference.SettingsRepository
import io.github.smiling_pixel.preference.getSettingsRepository
import io.github.smiling_pixel.util.generateSyncId
import kotlinx.coroutines.flow.first
import kotlinx.datetime.LocalDate
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import kotlin.time.Clock
import kotlin.time.ExperimentalTime
import kotlin.time.Instant as KotlinTimeInstant
/**
* Result counters returned by [performCloudSync].
*/
data class SyncResult(
val uploaded: Int,
val downloaded: Int,
val unchanged: Int,
val warnings: List<String> = emptyList()
)
/**
* Synchronizes local diary entries with cloud files.
*
* Matching is keyed by [DiaryEntry.syncId], a stable cross-device identifier.
* Local database IDs are intentionally not used for cloud matching.
*/
@OptIn(ExperimentalTime::class)
suspend fun performCloudSync(
client: CloudDriveClient,
repo: DiaryRepository,
localEntries: List<DiaryEntry>,
settings: SettingsRepository = getSettingsRepository()
): SyncResult {
val isEnabled = settings.isCloudSyncEnabled.first()
if (!isEnabled) {
throw Exception("Cloud Sync is disabled. Please enable it in Settings.")
}
if (!client.isAuthorized()) {
throw Exception("Not connected to Google Drive. Please connect in Settings.")
}
val syncPath = settings.cloudSyncPath.first()
val parentId = getOrCreateFolderByPath(client, syncPath)
val remoteFiles = client.listFiles(parentId).filter {
it.name.startsWith(SYNC_ENTRY_FILE_PREFIX) || it.name.startsWith(SYNC_TOMBSTONE_FILE_PREFIX)
}
val remoteFileMap = mutableMapOf<String, Pair<io.github.smiling_pixel.client.DriveFile, Long>>()
val remoteFileVersions = mutableMapOf<String, MutableList<Pair<io.github.smiling_pixel.client.DriveFile, Long>>>()
val remoteTombstoneMap = mutableMapOf<String, Pair<io.github.smiling_pixel.client.DriveFile, Long>>()
for (f in remoteFiles) {
val parsedEntry = parseRemoteEntryFileName(f.name)
if (parsedEntry != null) {
val (syncId, timestamp) = parsedEntry
remoteFileVersions.getOrPut(syncId) { mutableListOf() }.add(Pair(f, timestamp))
val existing = remoteFileMap[syncId]
if (existing == null || existing.second < timestamp) {
remoteFileMap[syncId] = Pair(f, timestamp)
}
continue
}
val parsedTombstone = parseRemoteTombstoneFileName(f.name)
if (parsedTombstone != null) {
val (syncId, deletedAtEpochMillis) = parsedTombstone
val existing = remoteTombstoneMap[syncId]
if (existing == null || existing.second < deletedAtEpochMillis) {
remoteTombstoneMap[syncId] = Pair(f, deletedAtEpochMillis)
}
}
}
var uploaded = 0
var downloaded = 0
var unchanged = 0
val warnings = mutableListOf<String>()
val localEntriesBySyncId = localEntries.associateBy { it.syncId }.toMutableMap()
val localTombstones = loadLocalDeletionTombstones(settings).toMutableMap()
// Apply remote tombstones before reconciling entries to avoid resurrecting deleted notes.
for ((syncId, tombstoneData) in remoteTombstoneMap) {
val remoteDeletedAt = tombstoneData.second
val localEntry = localEntriesBySyncId[syncId]
if (localEntry != null && localEntry.updatedAt.toEpochMilliseconds() <= remoteDeletedAt) {
repo.delete(localEntry, recordSyncTombstone = false)
localEntriesBySyncId.remove(syncId)
downloaded++
}
val localDeletedAt = localTombstones[syncId]
if (localDeletedAt != null && localDeletedAt <= remoteDeletedAt) {
localTombstones.remove(syncId)
}
val remoteEntries = remoteFileVersions[syncId].orEmpty()
for ((driveFile, remoteUpdatedAt) in remoteEntries) {
if (remoteUpdatedAt <= remoteDeletedAt) {
runCatching { client.deleteFile(driveFile.id) }
}
}
remoteFileVersions.remove(syncId)
remoteFileMap.remove(syncId)
}
val emptyEntryLocal = DiaryEntry(id = 0, syncId = generateSyncId(), title = "", content = "")
for (local in localEntriesBySyncId.values) {
val remote = remoteFileMap[local.syncId]
val localTime = local.updatedAt.toEpochMilliseconds()
if (remote != null) {
val (driveFile, remoteTime) = remote
if (localTime > remoteTime) {
val name = buildSyncEntryFileName(local.syncId, localTime)
client.createFile(name, encodeEntryForSync(local), SYNC_ENTRY_MIME_TYPE, parentId)
// Delete old files only after successful upload to avoid losing the only remote copy.
val oldVersions = remoteFileVersions[local.syncId].orEmpty()
for ((oldDriveFile, oldRemoteTime) in oldVersions) {
if (oldRemoteTime <= remoteTime) {
runCatching { client.deleteFile(oldDriveFile.id) }
}
}
uploaded++
} else if (remoteTime > localTime) {
val remoteContent = client.downloadFile(driveFile.id)
val parsed = decodeEntryForSync(remoteContent, local)
if (parsed != null) {
repo.update(parsed)
downloaded++
} else {
val legacyName = quarantineLegacyFile(client, driveFile, remoteContent, parentId)
warnings += "Quarantined unrecognized remote file \"${driveFile.name}\" as \"$legacyName\""
}
} else {
unchanged++
}
remoteFileMap.remove(local.syncId)
} else {
val name = buildSyncEntryFileName(local.syncId, localTime)
client.createFile(name, encodeEntryForSync(local), SYNC_ENTRY_MIME_TYPE, parentId)
uploaded++
}
}
for ((syncId, deletedAtEpochMillis) in localTombstones.toMap()) {
val remoteEntries = remoteFileVersions[syncId].orEmpty()
val newestRemoteEntryTimestamp = remoteEntries.maxOfOrNull { it.second }
if (newestRemoteEntryTimestamp != null && newestRemoteEntryTimestamp > deletedAtEpochMillis) {
localTombstones.remove(syncId)
continue
}
for ((driveFile, remoteTimestamp) in remoteEntries) {
if (remoteTimestamp <= deletedAtEpochMillis) {
runCatching { client.deleteFile(driveFile.id) }
}
}
remoteFileVersions.remove(syncId)
remoteFileMap.remove(syncId)
val remoteTombstone = remoteTombstoneMap[syncId]
if (remoteTombstone == null || remoteTombstone.second < deletedAtEpochMillis) {
val name = buildRemoteTombstoneFileName(syncId, deletedAtEpochMillis)
val created = client.createFile(
name,
encodeTombstoneForSync(syncId, deletedAtEpochMillis),
SYNC_ENTRY_MIME_TYPE,
parentId
)
if (remoteTombstone != null) {
runCatching { client.deleteFile(remoteTombstone.first.id) }
}
remoteTombstoneMap[syncId] = Pair(created, deletedAtEpochMillis)
uploaded++
}
localTombstones.remove(syncId)
}
// For remote files that don't exist locally, download them
for ((_, remoteData) in remoteFileMap) {
val (driveFile, _) = remoteData
val remoteContent = client.downloadFile(driveFile.id)
val parsed = decodeEntryForSync(remoteContent, emptyEntryLocal)
if (parsed != null) {
repo.insert(parsed)
downloaded++
} else {
val legacyName = quarantineLegacyFile(client, driveFile, remoteContent, parentId)
warnings += "Quarantined unrecognized remote file \"${driveFile.name}\" as \"$legacyName\""
}
}
saveLocalDeletionTombstones(settings, localTombstones)
// TODO: Optimise the synchronization process in the future (e.g., batch operations, or more efficient incremental sync).
return SyncResult(uploaded, downloaded, unchanged, warnings.toList())
}
@OptIn(ExperimentalTime::class)
suspend fun recordLocalDeletionTombstone(
syncId: String,
deletedAtEpochMillis: Long = Clock.System.now().toEpochMilliseconds(),
settings: SettingsRepository = getSettingsRepository()
) {
if (!looksLikeUuid(syncId)) return
val tombstones = loadLocalDeletionTombstones(settings).toMutableMap()
val existing = tombstones[syncId]
if (existing == null || existing < deletedAtEpochMillis) {
tombstones[syncId] = deletedAtEpochMillis
saveLocalDeletionTombstones(settings, tombstones)
}
}
private suspend fun getOrCreateFolderByPath(client: CloudDriveClient, path: String): String? {
val folders = path.split("/").filter { it.isNotBlank() }
if (folders.isEmpty()) return null
var currentParentId: String? = null
for (folderName in folders) {
val files = client.listFiles(currentParentId)
var folder = files.firstOrNull { it.name == folderName && it.isFolder }
if (folder == null) {
folder = client.createFolder(folderName, currentParentId)
}
currentParentId = folder.id
}
return currentParentId
}
/**
* Builds the cloud-sync file name used for a diary entry payload.
*
* @param syncId Stable cross-device entry identifier.
* @param timestampMillis Entry update timestamp in epoch milliseconds.
* @return The sync-compatible entry file name.
*/
internal fun buildSyncEntryFileName(syncId: String, timestampMillis: Long): String {
return "${SYNC_ENTRY_FILE_PREFIX}${syncId}_${timestampMillis}${SYNC_ENTRY_FILE_EXTENSION}"
}
private fun buildRemoteTombstoneFileName(syncId: String, timestampMillis: Long): String {
return "${SYNC_TOMBSTONE_FILE_PREFIX}${syncId}_${timestampMillis}${SYNC_ENTRY_FILE_EXTENSION}"
}
private fun parseRemoteEntryFileName(fileName: String): Pair<String, Long>? {
if (!fileName.startsWith(SYNC_ENTRY_FILE_PREFIX) || !fileName.endsWith(SYNC_ENTRY_FILE_EXTENSION)) return null
val core = fileName.removePrefix(SYNC_ENTRY_FILE_PREFIX).removeSuffix(SYNC_ENTRY_FILE_EXTENSION)
val separatorIndex = core.lastIndexOf('_')
if (separatorIndex <= 0 || separatorIndex == core.lastIndex) return null
val syncId = core.substring(0, separatorIndex)
val timestamp = core.substring(separatorIndex + 1).toLongOrNull() ?: return null
// Legacy numeric-ID files are intentionally ignored in this migration phase.
if (!looksLikeUuid(syncId)) return null
return syncId to timestamp
}
private fun parseRemoteTombstoneFileName(fileName: String): Pair<String, Long>? {
if (!fileName.startsWith(SYNC_TOMBSTONE_FILE_PREFIX) || !fileName.endsWith(SYNC_ENTRY_FILE_EXTENSION)) return null
val core = fileName.removePrefix(SYNC_TOMBSTONE_FILE_PREFIX).removeSuffix(SYNC_ENTRY_FILE_EXTENSION)
val separatorIndex = core.lastIndexOf('_')
if (separatorIndex <= 0 || separatorIndex == core.lastIndex) return null
val syncId = core.substring(0, separatorIndex)
val timestamp = core.substring(separatorIndex + 1).toLongOrNull() ?: return null
if (!looksLikeUuid(syncId)) return null
return syncId to timestamp
}
private fun looksLikeUuid(value: String): Boolean {
val pattern = Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
return pattern.matches(value)
}
private const val SYNC_ENTRY_FILE_EXTENSION = ".txt"
private const val SYNC_ENTRY_FILE_PREFIX = "markday_entry_"
private const val SYNC_TOMBSTONE_FILE_PREFIX = "markday_tombstone_"
private const val SYNC_LEGACY_FILE_PREFIX = "markday_legacy_"
private const val SYNC_ENTRY_MIME_TYPE = "text/plain"
private val syncPayloadJson = Json {
ignoreUnknownKeys = true
}
@Serializable
private data class SyncDeletionTombstonePayload(
val syncId: String,
val deletedAtEpochMillis: Long,
)
@Serializable
private data class SyncEntryPayload(
val syncId: String,
val title: String,
val createdAtEpochMillis: Long,
val updatedAtEpochMillis: Long,
val entryDateIso: String,
val weatherCondition: String? = null,
val minTemperature: Double? = null,
val maxTemperature: Double? = null,
val content: String,
)
@OptIn(ExperimentalTime::class)
internal fun encodeEntryForSync(entry: DiaryEntry): ByteArray {
val payload = SyncEntryPayload(
syncId = entry.syncId,
title = entry.title,
createdAtEpochMillis = entry.createdAt.toEpochMilliseconds(),
updatedAtEpochMillis = entry.updatedAt.toEpochMilliseconds(),
entryDateIso = entry.entryDate.toString(),
weatherCondition = entry.weatherCondition,
minTemperature = entry.minTemperature,
maxTemperature = entry.maxTemperature,
content = entry.content
)
return syncPayloadJson.encodeToString(SyncEntryPayload.serializer(), payload).encodeToByteArray()
}
@OptIn(ExperimentalTime::class)
internal fun decodeEntryForSync(bytes: ByteArray, original: DiaryEntry): DiaryEntry? {
try {
val text = bytes.decodeToString()
val payload = syncPayloadJson.decodeFromString(SyncEntryPayload.serializer(), text)
val syncId = payload.syncId
if (!looksLikeUuid(syncId)) return null
val title = payload.title
val createdAt = KotlinTimeInstant.fromEpochMilliseconds(payload.createdAtEpochMillis)
val updatedAt = KotlinTimeInstant.fromEpochMilliseconds(payload.updatedAtEpochMillis)
val entryDate = LocalDate.parse(payload.entryDateIso)
val weatherCondition = payload.weatherCondition
val minTemperature = payload.minTemperature
val maxTemperature = payload.maxTemperature
val content = payload.content
return original.copy(
syncId = syncId,
title = title,
createdAt = createdAt,
updatedAt = updatedAt,
entryDate = entryDate,
weatherCondition = weatherCondition,
minTemperature = minTemperature,
maxTemperature = maxTemperature,
content = content
)
} catch (e: Exception) {
return null
}
}
private suspend fun loadLocalDeletionTombstones(settings: SettingsRepository): Map<String, Long> {
val raw = settings.cloudSyncDeletionTombstonesJson.first() ?: return emptyMap()
return try {
val payloads = syncPayloadJson.decodeFromString(
ListSerializer(SyncDeletionTombstonePayload.serializer()),
raw
)
payloads
.filter { looksLikeUuid(it.syncId) }
.associate { it.syncId to it.deletedAtEpochMillis }
} catch (_: Exception) {
emptyMap()
}
}
private suspend fun saveLocalDeletionTombstones(settings: SettingsRepository, tombstones: Map<String, Long>) {
if (tombstones.isEmpty()) {
settings.setCloudSyncDeletionTombstonesJson(null)
return
}
val payloads = tombstones
.entries
.sortedBy { it.key }
.map { SyncDeletionTombstonePayload(syncId = it.key, deletedAtEpochMillis = it.value) }
val json = syncPayloadJson.encodeToString(ListSerializer(SyncDeletionTombstonePayload.serializer()), payloads)
settings.setCloudSyncDeletionTombstonesJson(json)
}
private fun encodeTombstoneForSync(syncId: String, deletedAtEpochMillis: Long): ByteArray {
val payload = SyncDeletionTombstonePayload(syncId = syncId, deletedAtEpochMillis = deletedAtEpochMillis)
return syncPayloadJson.encodeToString(SyncDeletionTombstonePayload.serializer(), payload).encodeToByteArray()
}
/**
* Renames a remote file that cannot be decoded (e.g., legacy format) to a quarantine prefix
* so it is not re-downloaded on every subsequent sync.
*
* @return the new quarantine file name (even if the operations fail).
*/
private suspend fun quarantineLegacyFile(
client: CloudDriveClient,
driveFile: io.github.smiling_pixel.client.DriveFile,
content: ByteArray,
parentId: String?
): String {
val legacyName = driveFile.name.replaceFirst(SYNC_ENTRY_FILE_PREFIX, SYNC_LEGACY_FILE_PREFIX)
runCatching {
client.createFile(legacyName, content, SYNC_ENTRY_MIME_TYPE, parentId)
client.deleteFile(driveFile.id)
}
return legacyName
}