|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using Windows.ApplicationModel.DataTransfer; |
| 4 | +using Windows.Storage; |
| 5 | + |
| 6 | +namespace Files.App.Helpers |
| 7 | +{ |
| 8 | + public static class TransferHelpers |
| 9 | + { |
| 10 | + public static async Task ExecuteTransferAsync(IReadOnlyList<IStorable> itemsToTransfer, ShellViewModel shellViewModel, StatusCenterViewModel statusViewModel, DataPackageOperation type = DataPackageOperation.Copy) |
| 11 | + { |
| 12 | + ConcurrentBag<IStorageItem> items = []; |
| 13 | + var itemsCount = itemsToTransfer.Count; |
| 14 | + var statusCenterItem = itemsCount > 50 ? StatusCenterHelper.AddCard_Prepare() : null; |
| 15 | + var dataPackage = new DataPackage() { RequestedOperation = type }; |
| 16 | + |
| 17 | + try |
| 18 | + { |
| 19 | + // Update the status to in-progress |
| 20 | + if (statusCenterItem is not null) |
| 21 | + { |
| 22 | + statusCenterItem.Progress.EnumerationCompleted = true; |
| 23 | + statusCenterItem.Progress.ItemsCount = items.Count; |
| 24 | + statusCenterItem.Progress.ReportStatus(FileSystemStatusCode.InProgress); |
| 25 | + } |
| 26 | + |
| 27 | + await itemsToTransfer.ParallelForEachAsync(async storable => |
| 28 | + { |
| 29 | + // Update the status to increase processed count by one |
| 30 | + if (statusCenterItem is not null) |
| 31 | + { |
| 32 | + statusCenterItem.Progress.AddProcessedItemsCount(1); |
| 33 | + statusCenterItem.Progress.Report(); |
| 34 | + } |
| 35 | + |
| 36 | + var result = storable switch |
| 37 | + { |
| 38 | + IFile => await shellViewModel.GetFileFromPathAsync(storable.Id).OnSuccess(x => items.Add(x)), |
| 39 | + IFolder => await shellViewModel.GetFolderFromPathAsync(storable.Id).OnSuccess(x => items.Add(x)), |
| 40 | + }; |
| 41 | + |
| 42 | + if (!result) |
| 43 | + throw new SystemIO.IOException($"Failed to process {storable.Id} in cutting/copying to the clipboard.", (int)result.ErrorCode); |
| 44 | + }, 10, statusCenterItem?.CancellationToken ?? CancellationToken.None); |
| 45 | + |
| 46 | + var standardObjectsOnly = items.All(x => x is StorageFile or StorageFolder or SystemStorageFile or SystemStorageFolder); |
| 47 | + if (standardObjectsOnly) |
| 48 | + items = new(await items.ToStandardStorageItemsAsync()); |
| 49 | + |
| 50 | + if (items.IsEmpty) |
| 51 | + return; |
| 52 | + |
| 53 | + dataPackage.Properties.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName; |
| 54 | + dataPackage.SetStorageItems(items, false); |
| 55 | + |
| 56 | + Clipboard.SetContent(dataPackage); |
| 57 | + } |
| 58 | + catch (Exception ex) |
| 59 | + { |
| 60 | + if (ex is not SystemIO.IOException) |
| 61 | + App.Logger.LogWarning(ex, "Failed to process cutting/copying due to an unknown error."); |
| 62 | + |
| 63 | + if ((FileSystemStatusCode)ex.HResult is FileSystemStatusCode.Unauthorized) |
| 64 | + { |
| 65 | + var filePaths = itemsToTransfer.Select(x => x.Id).ToArray(); |
| 66 | + await FileOperationsHelpers.SetClipboard(filePaths, type); |
| 67 | + } |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + if (statusCenterItem is not null) |
| 72 | + statusViewModel.RemoveItem(statusCenterItem); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static async Task ExecuteTransferAsync(IContentPageContext context, StatusCenterViewModel statusViewModel, DataPackageOperation type = DataPackageOperation.Copy) |
| 77 | + { |
| 78 | + if (context.ShellPage?.SlimContentPage is null || |
| 79 | + context.ShellPage.SlimContentPage.IsItemSelected is false) |
| 80 | + return; |
| 81 | + |
| 82 | + // Reset cut mode |
| 83 | + context.ShellPage.SlimContentPage.ItemManipulationModel.RefreshItemsOpacity(); |
| 84 | + |
| 85 | + ConcurrentBag<IStorageItem> items = []; |
| 86 | + var itemsCount = context.SelectedItems.Count; |
| 87 | + var statusCenterItem = itemsCount > 50 ? StatusCenterHelper.AddCard_Prepare() : null; |
| 88 | + var dataPackage = new DataPackage() { RequestedOperation = type }; |
| 89 | + |
| 90 | + try |
| 91 | + { |
| 92 | + // Update the status to in-progress |
| 93 | + if (statusCenterItem is not null) |
| 94 | + { |
| 95 | + statusCenterItem.Progress.EnumerationCompleted = true; |
| 96 | + statusCenterItem.Progress.ItemsCount = items.Count; |
| 97 | + statusCenterItem.Progress.ReportStatus(FileSystemStatusCode.InProgress); |
| 98 | + } |
| 99 | + |
| 100 | + await context.SelectedItems.ToList().ParallelForEachAsync(async listedItem => |
| 101 | + { |
| 102 | + // Update the status to increase processed count by one |
| 103 | + if (statusCenterItem is not null) |
| 104 | + { |
| 105 | + statusCenterItem.Progress.AddProcessedItemsCount(1); |
| 106 | + statusCenterItem.Progress.Report(); |
| 107 | + } |
| 108 | + |
| 109 | + if (listedItem is FtpItem ftpItem) |
| 110 | + { |
| 111 | + // Don't dim selected items here since FTP doesn't support cut |
| 112 | + if (ftpItem.PrimaryItemAttribute is StorageItemTypes.File or StorageItemTypes.Folder) |
| 113 | + items.Add(await ftpItem.ToStorageItem()); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + if (type is DataPackageOperation.Move) |
| 118 | + { |
| 119 | + // Dim opacities accordingly |
| 120 | + await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => |
| 121 | + { |
| 122 | + listedItem.Opacity = Constants.UI.DimItemOpacity; |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + var result = listedItem.PrimaryItemAttribute == StorageItemTypes.File || listedItem is ZipItem |
| 127 | + ? await context.ShellPage.ShellViewModel.GetFileFromPathAsync(listedItem.ItemPath).OnSuccess(t => items.Add(t)) |
| 128 | + : await context.ShellPage.ShellViewModel.GetFolderFromPathAsync(listedItem.ItemPath).OnSuccess(t => items.Add(t)); |
| 129 | + |
| 130 | + if (!result) |
| 131 | + throw new SystemIO.IOException($"Failed to process {listedItem.ItemPath} in cutting/copying to the clipboard.", (int)result.ErrorCode); |
| 132 | + } |
| 133 | + }, 10, statusCenterItem?.CancellationToken ?? CancellationToken.None); |
| 134 | + |
| 135 | + var standardObjectsOnly = items.All(x => x is StorageFile or StorageFolder or SystemStorageFile or SystemStorageFolder); |
| 136 | + if (standardObjectsOnly) |
| 137 | + items = new(await items.ToStandardStorageItemsAsync()); |
| 138 | + |
| 139 | + if (items.IsEmpty) |
| 140 | + return; |
| 141 | + |
| 142 | + dataPackage.Properties.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName; |
| 143 | + dataPackage.SetStorageItems(items, false); |
| 144 | + |
| 145 | + Clipboard.SetContent(dataPackage); |
| 146 | + } |
| 147 | + catch (Exception ex) |
| 148 | + { |
| 149 | + if (ex is not SystemIO.IOException) |
| 150 | + App.Logger.LogWarning(ex, "Failed to process cutting/copying due to an unknown error."); |
| 151 | + |
| 152 | + if ((FileSystemStatusCode)ex.HResult is FileSystemStatusCode.Unauthorized) |
| 153 | + { |
| 154 | + var filePaths = context.SelectedItems.Select(x => x.ItemPath).ToArray(); |
| 155 | + await FileOperationsHelpers.SetClipboard(filePaths, type); |
| 156 | + |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + // Reset cut mode |
| 161 | + context.ShellPage.SlimContentPage.ItemManipulationModel.RefreshItemsOpacity(); |
| 162 | + } |
| 163 | + finally |
| 164 | + { |
| 165 | + if (statusCenterItem is not null) |
| 166 | + statusViewModel.RemoveItem(statusCenterItem); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments