📦 LinkLeong / OZSync

📄 sync-manager.ts · 829 lines
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829import { TFile, TFolder, Vault, Notice, App } from 'obsidian';
import { OZSyncClient } from './ozsync-client';
import { OZSyncSettings, SyncStatus, SyncOperation, OZSyncFile } from './types';
import { format } from 'date-fns';

/**
 * OBSIDIAN VAULT STRUCTURE AND AUTO-SYNC EXPLANATION
 * 
 * 1. WHAT IS AN OBSIDIAN VAULT?
 *    An Obsidian vault is a DIRECTORY (folder) on your computer that contains:
 *    - Markdown files (.md) - your notes and documents
 *    - Attachments (images, PDFs, etc.) in various formats
 *    - Configuration files in the .obsidian subdirectory
 *    - Subdirectories to organize your content
 * 
 * 2. VAULT LOCATION:
 *    The vault directory is located at: /Users/liangjianli/Documents/oasis/
 *    This is the root directory that contains all your Obsidian content.
 * 
 * 3. AUTO-SYNC FUNCTIONALITY:
 *    When auto-sync is enabled, this plugin:
 *    - Monitors ALL files within the vault directory
 *    - Automatically uploads changed files to OZSync cloud storage
 *    - Runs at regular intervals (configurable in settings)
 *    - Syncs to user-selected directory on OZSync
 * 
 * 4. SYNC SCOPE AND BEHAVIOR:
 *    - WHAT GETS SYNCED: All markdown files (.md) and attachments in the vault
 *    - WHERE IT GOES: Files are uploaded to user-selected directory on OZSync
 *    - WHEN IT SYNCS: Automatically at set intervals when auto-sync is enabled
 *    - EXCLUSIONS: System files (.obsidian folder) are automatically excluded
 * 
 * 5. FILE STRUCTURE MAPPING:
 *    Local vault: /Users/liangjianli/Documents/oasis/MyNote.md
 *    Remote path: {syncDirectory}/MyNote.md
 *    
 *    Local vault: /Users/liangjianli/Documents/oasis/Folder/SubNote.md
 *    Remote path: {syncDirectory}/Folder/SubNote.md
 */

export class SyncManager {
	private vault: Vault;
	private client: OZSyncClient;
	private settings: OZSyncSettings;
	private syncStatus: SyncStatus;
	private syncOperations: SyncOperation[] = [];
	private syncInterval: number | null = null;
	private addLog: (log: any) => void;
	private onStatusUpdate?: (status: Partial<SyncStatus>) => void;

	constructor(client: OZSyncClient, settings: OZSyncSettings, addLog: (log: any) => void, onStatusUpdate?: (status: Partial<SyncStatus>) => void) {
		this.client = client;
		this.settings = settings;
		this.addLog = addLog;
		this.onStatusUpdate = onStatusUpdate;
		this.vault = (window as any).app.vault;
		this.syncStatus = {
			isConnected: false,
			syncInProgress: false,
			status: 'idle',
			pendingFiles: 0,
			processedFiles: 0,
			totalFiles: 0,
			syncSpeed: 0,
			bytesTransferred: 0,
			totalBytes: 0,
			errorCount: 0,
			startTime: undefined
		};
	}

	/**
	 * Initialize sync manager
	 */
	async initialize(): Promise<void> {
		try {
			// Don't auto-test connection on plugin startup to avoid unnecessary API requests
			// Connection test will be performed when user actively operates
			this.syncStatus.isConnected = false;
			
			console.log('OZSync: Manager initialized (connection will be tested when needed)');
		} catch (error) {
			console.error('Failed to initialize sync manager:', error);
		}
	}

	/**
	 * Test connection to OZSync server
	 */
	async testConnection(): Promise<boolean> {
		try {
			console.log('[SyncManager] Testing connection to OZSync server...');
			console.log('[SyncManager] Current sync status before test:', {
				isConnected: this.syncStatus.isConnected,
				clientExists: !!this.client,
				timestamp: new Date().toISOString()
			});
			
			console.log('[SyncManager] Calling client.testConnection()...');
			const connectionResult = await this.client.testConnection();
			console.log('[SyncManager] Client testConnection returned:', connectionResult);
			
			this.syncStatus.isConnected = connectionResult;
			
			console.log('[SyncManager] Connection test result:', {
				isConnected: this.syncStatus.isConnected,
				connectionResult,
				timestamp: new Date().toISOString()
			});
			
			if (this.syncStatus.isConnected) {
				console.log('[SyncManager] Connection successful, ensuring sync directory...');
				// After successful connection, ensure sync directory exists
				await this.ensureSyncDirectory();
				console.log('[SyncManager] Sync directory ensured');
				
				// Note: Auto sync is managed by main plugin, not started here
				console.log('[SyncManager] Connection successful - auto sync will be managed by main plugin');
				
				new Notice('OZSync: Connection test successful');
			} else {
				console.log('[SyncManager] Connection test failed - no connection established');
				new Notice('OZSync: Connection test failed');
			}
			
			// Notify main plugin of connection status update
			console.log('[SyncManager] Notifying main plugin of connection status:', {
				isConnected: this.syncStatus.isConnected,
				hasCallback: !!this.onStatusUpdate,
				callbackFunction: this.onStatusUpdate,
				timestamp: new Date().toISOString()
			});
			
			if (this.onStatusUpdate) {
				console.log('[SyncManager] Calling onStatusUpdate callback...');
				this.onStatusUpdate({ isConnected: this.syncStatus.isConnected });
				console.log('[SyncManager] onStatusUpdate callback called successfully');
			} else {
				console.warn('[SyncManager] No onStatusUpdate callback available!');
			}
			
			return this.syncStatus.isConnected;
		} catch (error) {
			console.error('[SyncManager] Connection test failed with error:', error);
			console.error('[SyncManager] Error details:', {
				errorMessage: error.message,
				errorStack: error.stack,
				timestamp: new Date().toISOString()
			});
			
			this.syncStatus.isConnected = false;
			// Notify main plugin of connection status update
			console.log('[SyncManager] Notifying main plugin of connection failure');
			if (this.onStatusUpdate) {
				this.onStatusUpdate({ isConnected: false });
			} else {
				console.warn('[SyncManager] No onStatusUpdate callback available for error notification!');
			}
			new Notice('OZSync: Connection test failed');
			return false;
		}
	}

	/**
	 * Start automatic synchronization
	 * 
	 * AUTO-SYNC WORKING PRINCIPLE:
	 * 1. Creates a timer that runs at user-defined intervals (default: every 15 minutes)
	 * 2. Each timer tick triggers performSync() which:
	 *    - Scans the entire vault directory for changes
	 *    - Compares local file modification times with remote versions
	 *    - Uploads only files that have been modified since last sync
	 * 3. Respects user exclusion settings (folders/file types to skip)
	 * 4. Runs continuously until manually stopped or plugin disabled
	 */
	startAutoSync(): void {
		if (this.syncInterval) {
			clearInterval(this.syncInterval);
		}
		
		const intervalMs = this.settings.syncInterval * 60 * 1000; // Convert minutes to milliseconds
		
		// Calculate and set next sync time
		this.updateNextSyncTime();
		
		this.syncInterval = window.setInterval(() => {
			this.performSync();
			// Update next sync time after each sync
			this.updateNextSyncTime();
		}, intervalMs);
		
		console.log(`Auto sync started with interval: ${this.settings.syncInterval} minutes`);
		console.log(`Next sync scheduled for: ${this.syncStatus.nextSyncTime?.toLocaleString()}`);
	}

	/**
	 * Stop automatic synchronization
	 */
	stopAutoSync(): void {
		if (this.syncInterval) {
			clearInterval(this.syncInterval);
			this.syncInterval = null;
			// Clear next sync time when auto sync is stopped
			this.syncStatus.nextSyncTime = undefined;
			console.log('Auto sync stopped');
		}
	}

	/**
	 * Update next sync time based on current settings
	 */
	private updateNextSyncTime(): void {
		if (this.settings.autoSyncEnabled && this.syncInterval) {
			const intervalMs = this.settings.syncInterval * 60 * 1000;
			this.syncStatus.nextSyncTime = new Date(Date.now() + intervalMs);
		} else {
			this.syncStatus.nextSyncTime = undefined;
		}
		// Notify main plugin of status update
		this.notifyStatusUpdate({ nextSyncTime: this.syncStatus.nextSyncTime });
	}

	/**
	 * Notify main plugin of status update
	 */
	private notifyStatusUpdate(updates: Partial<SyncStatus>): void {
		if (this.onStatusUpdate) {
			this.onStatusUpdate(updates);
		}
	}

	/**
	 * Perform synchronization
	 */
	async performSync(): Promise<void> {
		if (this.syncStatus.syncInProgress) {
			console.log('Sync already in progress, skipping');
			return;
		}

		this.syncStatus.syncInProgress = true;
		this.syncStatus.status = 'syncing';
		this.syncStatus.startTime = new Date();
		this.syncStatus.errorCount = 0;
		this.syncStatus.processedFiles = 0;
		this.syncStatus.bytesTransferred = 0;
		this.syncStatus.totalBytes = 0;

		try {
			console.log('Starting bidirectional sync operation');
			
			// Get local files that need to be synced
			const localFiles = await this.getFilesToSync();
			
			// Get all remote files
			const remoteFiles = await this.client.getAllFilesRecursive(this.settings.syncDirectory);
			
			// Create sync operations based on file comparison
			const syncOperations = await this.compareFiles(localFiles, remoteFiles);
			
			this.syncStatus.totalFiles = syncOperations.length;
			this.syncStatus.pendingFiles = syncOperations.length;

			if (syncOperations.length === 0) {
				console.log('No files need to be synced');
				this.syncStatus.status = 'idle';
				this.syncStatus.syncInProgress = false;
				this.syncStatus.lastSyncTime = new Date();
				return;
			}

			console.log(`Found ${syncOperations.length} sync operations to perform`);

			// Execute sync operations
			for (const operation of syncOperations) {
				try {
					if (operation.type === 'upload' && operation.file) {
					await this.syncFile(operation.file);
				} else if (operation.type === 'download' && operation.remotePath && operation.localPath) {
					await this.downloadFile(operation.remotePath, operation.localPath);
				}
					this.syncStatus.processedFiles++;
					this.syncStatus.pendingFiles--;
				} catch (error) {
					this.syncStatus.errorCount++;
					console.error(`Failed to ${operation.type} file: ${operation.file?.path || operation.remotePath}`, error);
				}
			}

			this.syncStatus.status = 'idle';
			this.syncStatus.lastSyncTime = new Date();
			console.log(`Bidirectional sync completed. Processed: ${this.syncStatus.processedFiles}, Errors: ${this.syncStatus.errorCount}`);

		} catch (error) {
			this.syncStatus.status = 'error';
			this.syncStatus.errorCount++;
			console.error('Sync operation failed', error);
		} finally {
			this.syncStatus.syncInProgress = false;
		}
	}

	/**
	 * Get files that need to be synchronized
	 * 
	 * SYNC SCOPE DETERMINATION:
	 * 1. Gets ALL files in the vault (markdown and attachments)
	 * 2. Applies system exclusion filters:
	 *    - Skips files in .obsidian folder (system configurations)
	 *    - Skips files in .trash folder (deleted files)
	 * 3. Checks modification times:
	 *    - Compares local file mtime with remote file timestamp
	 *    - Only includes files that are newer locally than remotely
	 * 4. Returns final list of files that need uploading
	 */
	private async getFilesToSync(): Promise<TFile[]> {
		const allFiles = this.vault.getFiles();
		const filesToSync: TFile[] = [];

		for (const file of allFiles) {
			// Check if file should be excluded (only system folders)
			if (this.shouldExcludeFile(file)) {
				continue;
			}

			// Check if file needs sync (modified since last sync)
			if (await this.needsSync(file)) {
				filesToSync.push(file);
			}
		}

		return filesToSync;
	}

	/**
	 * Check if a file should be excluded from sync
	 * 
	 * EXCLUSION LOGIC:
	 * Only system folders are automatically excluded:
	 * - .obsidian/ (plugin configurations, not user content)
	 * - .trash/ (deleted files)
	 * This ensures all user content gets synced to OZSync
	 */
	private shouldExcludeFile(file: TFile): boolean {
		// Only exclude system folders
		const systemFolders = ['.obsidian/', '.trash/'];
		
		for (const systemFolder of systemFolders) {
			if (file.path.startsWith(systemFolder)) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Check if a file needs synchronization
	 */
	private async needsSync(file: TFile): Promise<boolean> {
		try {
			const remotePath = this.getRemotePath(file.path);
			const remoteStats = await this.client.getFileStatsV2([remotePath]);
			const remoteFile = remoteStats && remoteStats.length > 0 ? remoteStats[0] : null;
			
			if (!remoteFile) {
				// File doesn't exist remotely, needs sync
				return true;
			}
			
			// Compare modification times
			// Note: remoteFile structure may vary, adjust based on actual API response
			const remoteModTime = remoteFile?.lastModified ? new Date(remoteFile.lastModified).getTime() : 0;
			return file.stat.mtime > remoteModTime;
		} catch (error: any) {
			const errorDetails = {
				methodName: 'needsSync',
				filePath: file.path,
				remotePath: this.getRemotePath(file.path),
				localMtime: file.stat.mtime,
				message: error.message,
				status: error.response?.status,
				responseData: error.response?.data,
				stack: error.stack
			};
			
			console.error('[Sync Manager] Failed to check if file needs sync:', {
				timestamp: new Date().toISOString(),
				...errorDetails
			});
			
			this.addLog({
				type: 'error',
				message: 'Failed to check file sync status',
				details: errorDetails,
				timestamp: new Date().toISOString()
			});
			
			// If we can't check, assume it needs sync
			return true;
		}
	}

	/**
	 * Sync a single file
	 */
	private async syncFile(file: TFile): Promise<void> {
		try {
			const operation: SyncOperation = {
				id: this.generateOperationId(),
				type: 'upload',
				filePath: file.path,
				status: 'in-progress',
				progress: 0,
				timestamp: new Date()
			};
			
			this.syncOperations.push(operation);
			
			console.log('[Sync Manager] Starting file sync:', {
				filePath: file.path,
				fileSize: file.stat.size,
				fileExtension: file.extension,
				operationId: operation.id
			});
			
			// Read file content
			let content: string | Buffer;
			if (file.extension === 'md') {
				content = await this.vault.read(file);
			} else {
				const arrayBuffer = await this.vault.readBinary(file);
				content = Buffer.from(arrayBuffer);
			}
			
			// Get target directory path (without filename)
			const remotePath = this.getRemotePath(file.path);
			const targetDir = remotePath.substring(0, remotePath.lastIndexOf('/'));
			
			console.log('[Sync Manager] File sync paths:', {
				localPath: file.path,
				remotePath,
				targetDir,
				contentSize: content instanceof Buffer ? content.length : content.length
			});
			
			// Ensure target directory exists
			if (targetDir && !(await this.client.fileExistsV2(targetDir))) {
				console.log('[Sync Manager] Creating target directory:', targetDir);
				await this.client.createDirectory(targetDir);
			}
			
			// Upload to OZSync using new API
			const success = await this.client.uploadFileV2(targetDir, file.name, content);
			
			operation.status = success ? 'completed' : 'failed';
			operation.progress = 100;
			
			if (success) {
				this.syncStatus.bytesTransferred += file.stat.size;
				console.log('[Sync Manager] File sync completed:', {
					filePath: file.path,
					bytesTransferred: file.stat.size,
					totalBytesTransferred: this.syncStatus.bytesTransferred
				});
			} else {
				operation.error = 'Upload failed';
				this.syncStatus.errorCount++;
				console.error('[Sync Manager] File sync failed:', {
					filePath: file.path,
					reason: 'Upload returned false'
				});
			}
		} catch (error: any) {
			const errorDetails = {
				methodName: 'syncFile',
				filePath: file.path,
				fileSize: file.stat.size,
				fileExtension: file.extension,
				remotePath: this.getRemotePath(file.path),
				message: error.message,
				status: error.response?.status,
				responseData: error.response?.data,
				stack: error.stack
			};
			
			console.error('[Sync Manager] Failed to sync file:', {
				timestamp: new Date().toISOString(),
				...errorDetails
			});
			
			this.addLog({
				type: 'error',
				message: `Failed to sync file: ${file.path}`,
				details: errorDetails,
				timestamp: new Date().toISOString()
			});
			
			this.syncStatus.errorCount++;
		}
	}

	/**
	 * Compare local and remote files to determine sync operations
	 */
	private async compareFiles(localFiles: TFile[], remoteFiles: OZSyncFile[]): Promise<SyncOperation[]> {
		const operations: SyncOperation[] = [];
		
		// Create maps for easier lookup
		const localFileMap = new Map<string, TFile>();
		const remoteFileMap = new Map<string, OZSyncFile>();
		
		// Map local files by their remote path
		for (const file of localFiles) {
			const remotePath = this.getRemotePath(file.path);
			localFileMap.set(remotePath, file);
		}
		
		// Map remote files by their path
		for (const file of remoteFiles) {
			remoteFileMap.set(file.path, file);
		}
		
		// Check local files against remote files
		for (const [remotePath, localFile] of localFileMap) {
			const remoteFile = remoteFileMap.get(remotePath);
			
			if (!remoteFile) {
				// Local file doesn't exist on remote - upload
				operations.push({
					id: `upload-${localFile.path}`,
					type: 'upload',
					filePath: localFile.path,
					status: 'pending',
					progress: 0,
					timestamp: new Date(),
					file: localFile
				});
			} else {
				// Both files exist - apply conflict resolution strategy
				const localModified = localFile.stat.mtime;
				const remoteModified = remoteFile.lastModified;
				
				// Apply conflict resolution strategy
				if (this.settings.conflictResolution === 'local') {
					// 以本地为准 - 总是上传本地文件
					if (localModified !== remoteModified) {
						operations.push({
							id: `upload-${localFile.path}`,
							type: 'upload',
							filePath: localFile.path,
							status: 'pending',
							progress: 0,
							timestamp: new Date(),
							file: localFile
						});
					}
				} else if (this.settings.conflictResolution === 'remote') {
					// 以服务器为准 - 总是下载远程文件
					if (localModified !== remoteModified) {
						operations.push({
							id: `download-${remotePath}`,
							type: 'download',
							filePath: localFile.path,
							status: 'pending',
							progress: 0,
							timestamp: new Date(),
							remotePath: remotePath,
							localPath: localFile.path
						});
					}
				} else {
					// 默认行为:基于时间戳比较
					if (localModified > remoteModified) {
						// Local file is newer - upload
						operations.push({
							id: `upload-${localFile.path}`,
							type: 'upload',
							filePath: localFile.path,
							status: 'pending',
							progress: 0,
							timestamp: new Date(),
							file: localFile
						});
					} else if (remoteModified > localModified) {
						// Remote file is newer - download
						operations.push({
							id: `download-${remotePath}`,
							type: 'download',
							filePath: localFile.path,
							status: 'pending',
							progress: 0,
							timestamp: new Date(),
							remotePath: remotePath,
							localPath: localFile.path
						});
					}
				}
				// If modification times are equal, no sync needed
			}
		}
		
		// Check for remote files that don't exist locally
		for (const [remotePath, remoteFile] of remoteFileMap) {
			if (!localFileMap.has(remotePath)) {
				// Remote file doesn't exist locally - download
				const localPath = this.getLocalPath(remotePath);
				operations.push({
					id: `download-${remotePath}`,
					type: 'download',
					filePath: localPath,
					status: 'pending',
					progress: 0,
					timestamp: new Date(),
					remotePath: remotePath,
					localPath: localPath
				});
			}
		}
		
		return operations;
	}
	
	/**
	 * Download a file from remote to local
	 */
	private async downloadFile(remotePath: string, localPath: string): Promise<void> {
		try {
			console.log(`Downloading file: ${remotePath} -> ${localPath}`);
			
			// Download file content from OZSync
			const content = await this.client.downloadFile(remotePath);
			
			if (content === null) {
				throw new Error('Failed to download file content');
			}
			
			// Ensure parent directory exists
			const parentDir = localPath.substring(0, localPath.lastIndexOf('/'));
			if (parentDir && !(await this.vault.adapter.exists(parentDir))) {
				await this.vault.adapter.mkdir(parentDir);
			}
			
			// Write file to vault
			if (await this.vault.adapter.exists(localPath)) {
				// File exists, modify it
				await this.vault.adapter.write(localPath, content);
			} else {
				// File doesn't exist, create it
				await this.vault.create(localPath, content);
			}
			
			console.log(`File downloaded successfully: ${localPath}`);
			
		} catch (error) {
			console.error(`Failed to download file: ${remotePath}`, error);
			throw error;
		}
	}
	
	/**
	 * Convert remote path to local path
	 */
	private getLocalPath(remotePath: string): string {
		// Remove the sync directory prefix from remote path
		const syncDir = this.settings.syncDirectory;
		if (remotePath.startsWith(syncDir)) {
			return remotePath.substring(syncDir.length + 1); // +1 for the trailing slash
		}
		return remotePath;
	}
	
	/**
	 * Update sync speed calculation
	 */
	private updateSyncSpeed(): void {
		if (!this.syncStatus.startTime) return;
		
		const elapsedMs = Date.now() - this.syncStatus.startTime.getTime();
		const elapsedSeconds = elapsedMs / 1000;
		
		if (elapsedSeconds > 0) {
			this.syncStatus.syncSpeed = this.syncStatus.bytesTransferred / elapsedSeconds;
		}
	}



	/**
	 * Get remote path for a local file
	 * Uses the configured sync directory directly without modification
	 */
	private getRemotePath(localPath: string): string {
		// Use the sync directory as configured by the user
		const syncDir = this.settings.syncDirectory || '/media/OZSync-HD/Obsidian';
		
		// Remove leading slash from localPath if present
		const cleanLocalPath = localPath.startsWith('/') ? localPath.substring(1) : localPath;
		
		// Combine paths with proper separator, avoiding double slashes
		const remotePath = syncDir.endsWith('/') ? 
			`${syncDir}${cleanLocalPath}` : 
			`${syncDir}/${cleanLocalPath}`;
		
		console.log('[Sync Manager] Path mapping:', {
			localPath,
			cleanLocalPath,
			syncDir,
			remotePath
		});
		
		return remotePath;
	}

	/**
	 * Ensure sync directory exists
	 */
	private async ensureSyncDirectory(): Promise<void> {
		if (!this.settings.syncDirectory) {
			console.warn('[Sync Manager] No sync directory configured');
			return;
		}
		
		try {
			// Use the sync directory as configured by the user
			const syncDir = this.settings.syncDirectory;
			
			console.log('[Sync Manager] Checking sync directory:', {
				syncDirectory: syncDir
			});
			
			if (!(await this.client.fileExistsV2(syncDir))) {
				console.log('[Sync Manager] Creating sync directory:', syncDir);
				await this.client.createDirectory(syncDir);
				console.log('[Sync Manager] Sync directory created successfully');
			} else {
				console.log('[Sync Manager] Sync directory already exists');
			}
		} catch (error: any) {
			const errorDetails = {
				methodName: 'ensureSyncDirectory',
				syncDirectory: this.settings.syncDirectory,
				message: error.message,
				status: error.response?.status,
				responseData: error.response?.data,
				stack: error.stack
			};
			
			console.error('[Sync Manager] Failed to ensure sync directory:', {
				timestamp: new Date().toISOString(),
				...errorDetails
			});
			
			this.addLog({
				type: 'error',
				message: 'Failed to ensure sync directory exists',
				details: errorDetails,
				timestamp: new Date().toISOString()
			});
			
			throw error;
		}
	}









	/**
	 * Generate operation ID
	 */
	private generateOperationId(): string {
		return Date.now().toString(36) + Math.random().toString(36).substr(2);
	}



	/**
	 * Format file size
	 */
	private formatFileSize(bytes: number): string {
		if (bytes === 0) return '0 Bytes';
		const k = 1024;
		const sizes = ['Bytes', 'KB', 'MB', 'GB'];
		const i = Math.floor(Math.log(bytes) / Math.log(k));
		return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
	}

	/**
	 * Get sync status
	 */
	getSyncStatus(): SyncStatus {
		return { ...this.syncStatus };
	}

	/**
	 * Get sync operations
	 */
	getSyncOperations(): SyncOperation[] {
		return [...this.syncOperations];
	}



	/**
	 * Update settings
	 */
	updateSettings(settings: OZSyncSettings): void {
		this.settings = settings;
		
		// Restart auto sync if settings changed
		if (settings.autoSyncEnabled) {
			this.startAutoSync();
		} else {
			this.stopAutoSync();
		}
	}

	/**
	 * Cleanup
	 */
	destroy(): void {
		this.stopAutoSync();
	}
}