public async Task <IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath source, string destination, IProgress <float> progress, IProgress <FileSystemStatusCode> errorCode, CancellationToken cancellationToken) { var connection = await AppServiceConnectionHelper.Instance; if (connection == null || string.IsNullOrWhiteSpace(source.Path) || source.Path.StartsWith(@"\\?\") || string.IsNullOrWhiteSpace(destination) || destination.StartsWith(@"\\?\")) { // Fallback to builtin file operations return(await filesystemOperations.RestoreFromTrashAsync(source, destination, progress, errorCode, cancellationToken)); } var operationID = Guid.NewGuid().ToString(); using var _ = cancellationToken.Register(CancelOperation, operationID, false); EventHandler <Dictionary <string, object> > handler = (s, e) => OnProgressUpdated(s, e, progress); connection.RequestReceived += handler; var(status, response) = await connection.SendMessageForResponseAsync(new ValueSet() { { "Arguments", "FileOperation" }, { "fileop", "MoveItem" }, { "operationID", operationID }, { "filepath", source.Path }, { "destpath", destination }, { "overwrite", false } }); var result = (FilesystemResult)(status == AppServiceResponseStatus.Success && response.Get("Success", false)); if (connection != null) { connection.RequestReceived -= handler; } if (result) { progress?.Report(100.0f); var movedItems = JsonConvert.DeserializeObject <IEnumerable <string> >(response["MovedItems"] as string); errorCode?.Report(FileSystemStatusCode.Success); if (movedItems != null && movedItems.Count() == 1) { // Recycle bin also stores a file starting with $I for each item await DeleteAsync(StorageItemHelpers.FromPathAndType( Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I")), source.ItemType), null, null, true, cancellationToken); return(new StorageHistory(FileOperationType.Restore, source, StorageItemHelpers.FromPathAndType(movedItems.Single(), source.ItemType))); } return(null); // Cannot undo overwrite operation } else { // Retry failed operations return(await filesystemOperations.RestoreFromTrashAsync(source, destination, progress, errorCode, cancellationToken)); } }
public async Task <IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath source, string destination, IProgress <float> progress, IProgress <FileSystemStatusCode> errorCode, CancellationToken cancellationToken) { var connection = await AppServiceConnectionHelper.Instance; if (connection == null || string.IsNullOrWhiteSpace(source.Path) || source.Path.StartsWith(@"\\?\", StringComparison.Ordinal) || string.IsNullOrWhiteSpace(destination) || destination.StartsWith(@"\\?\", StringComparison.Ordinal) || FtpHelpers.IsFtpPath(destination)) { // Fallback to builtin file operations return(await filesystemOperations.RestoreFromTrashAsync(source, destination, progress, errorCode, cancellationToken)); } var operationID = Guid.NewGuid().ToString(); using var r = cancellationToken.Register(CancelOperation, operationID, false); EventHandler <Dictionary <string, object> > handler = (s, e) => OnProgressUpdated(s, e, operationID, progress); connection.RequestReceived += handler; var moveResult = new ShellOperationResult(); var(status, response) = await connection.SendMessageForResponseAsync(new ValueSet() { { "Arguments", "FileOperation" }, { "fileop", "MoveItem" }, { "operationID", operationID }, { "filepath", source.Path }, { "destpath", destination }, { "overwrite", false }, { "HWND", NativeWinApiHelper.CoreWindowHandle.ToInt64() } }); var result = (FilesystemResult)(status == AppServiceResponseStatus.Success && response.Get("Success", false)); var shellOpResult = JsonConvert.DeserializeObject <ShellOperationResult>(response.Get("Result", "")); moveResult.Items.AddRange(shellOpResult?.Items ?? Enumerable.Empty <ShellOperationItemResult>()); if (connection != null) { connection.RequestReceived -= handler; } result &= (FilesystemResult)moveResult.Items.All(x => x.Succeeded); if (result) { progress?.Report(100.0f); errorCode?.Report(FileSystemStatusCode.Success); var movedSources = moveResult.Items.Where(x => new[] { source }.Select(s => s.Path).Contains(x.Source)).Where(x => x.Succeeded && x.Destination != null && x.Source != x.Destination); if (movedSources.Any()) { // Recycle bin also stores a file starting with $I for each item await DeleteItemsAsync(movedSources.Select(src => StorageHelpers.FromPathAndType( Path.Combine(Path.GetDirectoryName(src.Source), Path.GetFileName(src.Source).Replace("$R", "$I", StringComparison.Ordinal)), new[] { source }.Single(s => s.Path == src.Source).ItemType)), null, null, true, cancellationToken); return(new StorageHistory(FileOperationType.Restore, source, StorageHelpers.FromPathAndType(movedSources.Single().Destination, source.ItemType))); } return(null); // Cannot undo overwrite operation } else { // Retry failed operations return(await filesystemOperations.RestoreFromTrashAsync(source, destination, progress, errorCode, cancellationToken)); } }