/// <summary> /// Executes the sort contained in a preview tree. /// </summary> /// <param name="previewTree"></param> /// <param name="worker_ReplaySorter"></param> /// <param name="replaysThrowingExceptions"></param> /// <returns></returns> public DirectoryFileTree ExecuteSortAsync(DirectoryFileTree previewTree, BackgroundWorker worker_ReplaySorter, List <string> replaysThrowingExceptions) { var resultingTree = new DirectoryFileTree(previewTree.Root.Name); var previewToResultingTreeNodesMapping = new Dictionary <DirectoryFileTreeNode, DirectoryFileTreeNode>(); previewToResultingTreeNodesMapping.Add(previewTree.Root, resultingTree.Root); var nodeQueue = new Queue <DirectoryFileTreeNode>(); nodeQueue.Enqueue(previewTree.Root); var previewTreeNodeDirectories = new Dictionary <DirectoryFileTreeNode, string>(); previewTreeNodeDirectories.Add(previewTree.Root, previewTree.Root.Name + @"\"); var count = previewTree.Count; var currentCount = 0; while (nodeQueue.Count != 0) { var previewNode = nodeQueue.Dequeue(); if (previewNode == null) { continue; } currentCount++; worker_ReplaySorter.ReportProgress(Convert.ToInt32((double)currentCount / count * 100), $"Writing preview to disk... {(previewNode.IsDirectory ? $"Creating directory: {previewNode.Name}" : $"Creating replay {previewNode.Name}")}"); if (previewNode.IsDirectory) { foreach (var previewChild in previewNode.Children.ToList()) { nodeQueue.Enqueue(previewChild); if (previewChild.IsDirectory) { var dirName = FileHandler.CreateDirectory(previewTreeNodeDirectories[previewNode] + previewChild.Name + @"\"); if (!previewTreeNodeDirectories.ContainsKey(previewChild)) { previewTreeNodeDirectories.Add(previewChild, dirName); } previewToResultingTreeNodesMapping.Add(previewChild, resultingTree.AddToNode(previewToResultingTreeNodesMapping[previewNode], FileHandler.ExtractDirectoriesFromPath(dirName, resultingTree.Root.Name).Last())); } else { var fileReplay = FileReplay.Create(previewChild.Value.Content, previewChild.Value.OriginalFilePath, previewChild.Value.Hash); var filePath = previewTreeNodeDirectories[previewNode] + previewChild.Name; fileReplay.AddAfterCurrent(filePath); ReplayHandler.CopyReplay(fileReplay, true); previewToResultingTreeNodesMapping.Add(previewChild, resultingTree.AddToNode(previewToResultingTreeNodesMapping[previewNode], fileReplay)); } } } }
//TODO extract to general BuildTree function inside the DirectoryFileTree ?? Because i'm reusing it when inspecting a backup... private void AddOrModify(DirectoryFileTree tree, Dictionary <string, DirectoryFileTreeNode> directories, string directoryPath, string previousDirectoryPath, string directory, File <IReplay> replay) { if (directories == null || string.IsNullOrWhiteSpace(directoryPath)) { return; } if (directories.ContainsKey(directoryPath)) { if (replay != null) { var fileReplay = FileReplay.Create(replay.Content, replay.OriginalFilePath, replay.Hash); fileReplay.AddAfterCurrent(replay.FilePath); fileReplay.Forward(); tree.AddToNode(directories[directoryPath], fileReplay); } } else { if (replay != null) { var fileReplay = FileReplay.Create(replay.Content, replay.OriginalFilePath, replay.Hash); fileReplay.AddAfterCurrent(replay.FilePath); fileReplay.Forward(); directories.Add( directoryPath, tree.AddToNode( directories[previousDirectoryPath], directory, new List <FileReplay>() { fileReplay }.AsEnumerable() ) ); } else { directories.Add( directoryPath, tree.AddToNode( directories[previousDirectoryPath], directory) ); } } }