Example #1
0
        private async Task startUpSyncFolderAsync()
        {
            List <FolderFileState> serverCurrentList = await _serverManager.GetServerFolderStatusAsync();

            List <FolderFileState> localCurrentList = _folderSyncState.ReadFolder();

            //Sync Operations
            List <FolderFileState> toUpload     = getDifferences(localCurrentList, _folderSyncState.FolderState.LocalFiles().ToList(), true);
            List <FolderFileState> toDownload   = getDifferences(serverCurrentList, _folderSyncState.FolderState.RemoteFiles().ToList(), true);
            List <FolderFileState> toDelete     = getDifferences(_folderSyncState.FolderState.RemoteFiles().ToList(), serverCurrentList, false); //Local delete based on server info
            List <FolderFileState> toCallDelete = getDifferences(_folderSyncState.FolderState.LocalFiles().ToList(), localCurrentList, false);   //Call delete based on local info

            //Analyze Conflicts in Sync Operations

            //Same File ToUpload And ToDownload - NewerVersionRemoteAndLocalVersionChanged
            List <FolderFileState> conflicted = SameFilenameDiffHashInToUploadAndToDownload(toUpload, toDownload);

            toUpload.RemoveAll(x => conflicted.Any(y => x.FileName() == y.FileName()));
            toDownload.RemoveAll(x => conflicted.Any(y => x.FileName() == y.FileName()));
            //toDelete.RemoveAll(x => conflicted.Any(y => x.FileName() == y.FileName())); // Files shouldn't be here
            //toCallDelete.RemoveAll(x => conflicted.Any(y => x.FileName() == y.FileName())); // Files shouldn't be here

            //SameFileToUploadAndToDelete - RemoteFileDeletedAndLocalIsNewer
            conflicted = SameFileToUploadAndToDelete(toUpload, toDelete);
            toUpload.RemoveAll(x => conflicted.Any(y => x.FileName() == y.FileName()));
            toDelete.RemoveAll(x => conflicted.Any(y => x.FileName() == y.FileName()));

            //SameFileToDownloadAndToCallDelete - NewerVersionRemoteAndLocalDeleted
            conflicted = SameFileToDownloadAndToCallDelete(toDownload, toCallDelete);
            toDownload.RemoveAll(x => conflicted.Any(y => x.FileName() == y.FileName()));
            toCallDelete.RemoveAll(x => conflicted.Any(y => x.FileName() == y.FileName()));

            //Execute Sync Operations
            foreach (var f in toDelete)
            {
                await operationDeleteFileAsync(f.FileName(), f.Hash());
            }

            foreach (var f in toDownload)
            {
                await operationDownloadFileAsync(f.FileName(), f.Hash());
            }

            foreach (var f in toUpload)
            {
                await operationUploadFileAsync(f.FileName());
            }

            foreach (var f in toCallDelete)
            {
                await operationCallDeleteFileAsync(f.FileName());
            }
        }