public void Sync() { _fileScheduler.RunAsync = RunAsync; var sw = Stopwatch.StartNew(); LoadDirectoryMetadata(); _logger.WriteLine("Syncing folder {0}", LocalDirectory.Name); var files = LocalDirectory.GetFiles("*.*", SearchOption.TopDirectoryOnly); var dirs = LocalDirectory.GetDirectories("*.*", SearchOption.TopDirectoryOnly); var missingLocalFiles = _cloudFileNames.Where(i => !_localFileNames.Contains(i)).ToList(); foreach (var file in files) { _fileScheduler.Execute(new Task(() => { SyncFile(file); })); } foreach (var missingLocalFile in missingLocalFiles) { _fileScheduler.Execute(new Task(() => { SyncFromCloud(missingLocalFile); })); } _fileScheduler.WaitAll(); sw.Stop(); _logger.WriteLine("Finish syncing {0} in {1}", LocalDirectory.Name, sw.Elapsed); var missingLocalDirectories = _cloudDirectories.Where(i => !dirs.Select(j => j.Name).Contains(i.Name)); foreach (var dir in dirs) { _directoryScheduler.Execute(new Task(() => { var dirSync = new DirectorySync(CloudDirectory.GetDirectoryReference(dir.Name), dir, _logger); dirSync.Sync(); })); } foreach (var cloudDir in missingLocalDirectories) { _directoryScheduler.Execute(new Task(() => { var dirInfo = new DirectoryInfo(Path.Combine(LocalDirectory.FullName, cloudDir.Name)); dirInfo.Create(); var dirSync = new DirectorySync(cloudDir, dirInfo, _logger); dirSync.Sync(); })); } }
private void LoadDirectoryMetadata() { _logger.WriteLine("Loading directory metadata {0}", CloudDirectory.Name); var items = CloudDirectory.ListFilesAndDirectories(); foreach (var i in items) { if (i is CloudFile) { var file = (CloudFile)i; file.FetchAttributes(); _cloudFiles.Add(file); } else { _cloudDirectories.Add((CloudFileDirectory)i); } } _cloudFileNames = _cloudFiles.Where(i => i.Metadata.ContainsKey(Constants.LocalFileName)).Select(i => i.Metadata[Constants.LocalFileName].ToLowerInvariant()).ToList(); _localFileNames = LocalDirectory.GetFiles("*.*", SearchOption.TopDirectoryOnly) .Select(i => i.FullName.ToLowerInvariant()) .ToList(); }
private void LocalClean() { LocalDirectory.GetDirectories("*.*", SearchOption.TopDirectoryOnly).ToList().ForEach(i => i.Delete(true)); LocalDirectory.GetFiles("*.*", SearchOption.TopDirectoryOnly).ToList().ForEach(i => i.Delete()); }