/// <summary> /// This function allows to synchronize a local folder with an folder exists in the cloud storage /// and vice versa. The local folder and the target folder has to be created before. /// </summary> /// <param name="srcFolder"></param> /// <param name="tgtFolder"></param> /// <param name="flags"></param> /// <returns></returns> public Boolean SynchronizeFolder(DirectoryInfo srcFolder, ICloudDirectoryEntry tgtFolder, SyncFolderFlags flags) { // init ret value Boolean bRet = true; // init helper parameter Boolean bRecursive = ((flags & SyncFolderFlags.Recursive) != 0); // init the differ DirectoryDiff diff = new DirectoryDiff(srcFolder, tgtFolder); // build the diff results List <DirectoryDiffResultItem> res = diff.Compare(bRecursive); // process the diff result foreach (DirectoryDiffResultItem item in res) { switch (item.compareResult) { case ComparisonResult.Identical: { continue; } case ComparisonResult.MissingInLocalFolder: { // check of the upload flag was set if ((flags & SyncFolderFlags.DownloadItems) != SyncFolderFlags.DownloadItems) { continue; } // copy remote to local or create path // 1. get the rel path String relPath; if (item.remoteItem is ICloudDirectoryEntry) { relPath = CloudStorage.GetFullCloudPath(tgtFolder, item.remoteItem, '\\'); } else { relPath = CloudStorage.GetFullCloudPath(tgtFolder, item.remoteItem.Parent, '\\'); } // 2. ensure the directory exists String tgtPath = Path.Combine(srcFolder.FullName, relPath); if (!Directory.Exists(tgtPath)) { Directory.CreateDirectory(tgtPath); } // 3. download file if needed if (!(item.remoteItem is ICloudDirectoryEntry)) { DownloadFile(item.remoteItem.Parent, item.remoteItem.Name, tgtPath); } break; } case ComparisonResult.MissingInRemoteFolder: { // check of the upload flag was set if ((flags & SyncFolderFlags.UploadItems) != SyncFolderFlags.UploadItems) { continue; } // copy local to remote // 1. get the rel path String relPath; if ((item.localItem.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { relPath = item.localItem.FullName.Remove(0, srcFolder.FullName.Length); } else { relPath = Path.GetDirectoryName(item.localItem.FullName).Remove(0, srcFolder.FullName.Length); } // 2. convert delimit relPath = relPath.Replace(Path.DirectorySeparatorChar, '/'); // 3. ensure the directory exists ICloudDirectoryEntry realTarget = null; if (relPath.Length == 0) { realTarget = tgtFolder; } else { if (relPath[0] == '/') { relPath = relPath.Remove(0, 1); } //check if subfolder exists, if it doesn't, create it realTarget = GetFolder(relPath, tgtFolder) ?? CreateFolderEx(relPath, tgtFolder); } // 4. check target if (realTarget == null) { bRet = false; continue; } // 5. upload file if needed if ((item.localItem.Attributes & FileAttributes.Directory) != FileAttributes.Directory) { if (UploadFile(item.localItem.FullName, realTarget) == null) { bRet = false; } } break; } case ComparisonResult.SizeDifferent: { throw new NotImplementedException(); } } } return(bRet); }