private void CopyAmazonFolder(FolderTreeViewModel item, bool isCutting = false) { CopyAmazonStructure structure = new CopyAmazonStructure() { SourceParentPrefix = item.ParentFolder != null ? item.ParentFolder.Prefix : string.Empty, SourcePrefix = item.Prefix, Cutting = isCutting, FolderPrefix = item.Prefix }; Clipboard.SetData("amazonClipboardData", JsonConvert.SerializeObject(structure)); }
private void CopyAmazonFile(FolderTreeViewModel source, List <AmazonS3FileObject> items, bool isCutting = false) { List <string> itemKeys = new List <string>(); foreach (var f in items) { itemKeys.Add(f.FileObject.Key); } CopyAmazonStructure structure = new CopyAmazonStructure() { SourceParentPrefix = source.ParentFolder != null ? source.ParentFolder.Prefix : string.Empty, SourcePrefix = source.Prefix, Cutting = isCutting, OriginalKeysToCopy = itemKeys }; Clipboard.SetData("amazonClipboardData", JsonConvert.SerializeObject(structure)); }
private List <SourceDestinationData> CompilePasteOperations(CopyAmazonStructure data, FolderTreeViewModel item) { string newPrefix = item.Prefix; List <SourceDestinationData> operationData = new List <SourceDestinationData>(); List <string> keysInFolder = new List <string>(); if (data.FolderPrefix != null) { FolderTreeViewModel node = RemoteFolders[0]; GetAllItemsUnderPrefix(node, data.FolderPrefix + "/", keysInFolder); foreach (string fullKey in keysInFolder) { string relativePath = fullKey.Substring(data.FolderPrefix.LastIndexOf('/') + 1); operationData.Add(new SourceDestinationData() { Source = fullKey, Destination = newPrefix + "/" + relativePath }); } } else { foreach (var key in data.OriginalKeysToCopy) { string filename = key; if (key.Contains("/")) { filename = key.Substring(key.LastIndexOf('/') + 1); } string newObjectKey = newPrefix + "/" + filename; operationData.Add(new SourceDestinationData() { Source = key, Destination = newObjectKey }); } } return(operationData); }
private void PasteAmazonObject(FolderTreeViewModel item) { if (Clipboard.ContainsData("amazonClipboardData")) { CopyAmazonStructure data = JsonConvert.DeserializeObject <CopyAmazonStructure>(Clipboard.GetData("amazonClipboardData").ToString()); if (data.FolderPrefix != null && data.Cutting == true) { if ((item.Prefix + "/").IndexOf(data.SourcePrefix + "/") == 0) { MessageBox.Show(Window.GetWindow(this), "Target is same as source", "Error"); return; } if (string.IsNullOrEmpty(data.SourceParentPrefix) == false && data.SourceParentPrefix.Equals(item.Prefix)) { MessageBox.Show(Window.GetWindow(this), "Target is same as source", "Error"); return; } if (item.Prefix.Equals(data.SourcePrefix)) { MessageBox.Show(Window.GetWindow(this), "Target is same as source", "Error"); return; } } if (MessageBox.Show(Window.GetWindow(this), "Do you really want to paste files here?", "Paste?", MessageBoxButton.YesNo) != MessageBoxResult.Yes) { return; } FolderTreeViewModel sourceToRefresh = null; if (data.FolderPrefix != null) { sourceToRefresh = FindNodeByPrefix(RemoteFolders[0], data.SourceParentPrefix); } else { sourceToRefresh = FindNodeByPrefix(RemoteFolders[0], data.SourcePrefix); } List <SourceDestinationData> operationData = CompilePasteOperations(data, item); BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (s, e) => { int currentCount = 0; ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = 2; System.Threading.Tasks.Parallel.ForEach(operationData, options, operation => { currentCount++; int percentDone = (int)Math.Round(((double)(currentCount) / (double)operationData.Count) * 100, 0); worker.ReportProgress(percentDone); _amazon.CopyFile(operation.Source, operation.Destination); if (data.Cutting) { _amazon.DeleteFile(operation.Source); } }); if (data.Cutting && data.FolderPrefix != null) { _amazon.DeleteFolder(data.FolderPrefix); } }; worker.RunWorkerCompleted += (s, e) => { ctrlLoading.AdornerShowsProgress = false; this.IsWorking = false; if (e.Error == null) { if (data.Cutting == true) { Clipboard.Clear(); EnqueueGetRequest(sourceToRefresh, false); EnqueueGetRequest(item, true); ProcessNextGetRequest(); } else { GetBucketContents(item); } } else { MessageBox.Show(Window.GetWindow(this), "Error occured: " + e.Error.Message, "Error"); } }; worker.WorkerReportsProgress = true; worker.ProgressChanged += (s, e) => { ctrlLoading.AdornerProgress = e.ProgressPercentage; }; worker.RunWorkerAsync(); ctrlLoading.AdornerShowsProgress = true; this.IsWorking = true; } }