-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
FilesystemHelpers.cs
901 lines (736 loc) · 33.7 KB
/
FilesystemHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.
using Files.Core.Storage;
using Files.Core.Storage.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Vanara.PInvoke;
using Vanara.Windows.Shell;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using FileAttributes = System.IO.FileAttributes;
namespace Files.App.Utils.Storage
{
public sealed class FilesystemHelpers : IFilesystemHelpers
{
private readonly IStorageTrashBinService StorageTrashBinService = Ioc.Default.GetRequiredService<IStorageTrashBinService>();
private readonly static StatusCenterViewModel _statusCenterViewModel = Ioc.Default.GetRequiredService<StatusCenterViewModel>();
private IShellPage associatedInstance;
private readonly IWindowsJumpListService jumpListService;
private ShellFilesystemOperations filesystemOperations;
private ItemManipulationModel? itemManipulationModel => associatedInstance.SlimContentPage?.ItemManipulationModel;
private readonly CancellationToken cancellationToken;
private static char[] RestrictedCharacters
{
get
{
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
return userSettingsService.FoldersSettingsService.AreAlternateStreamsVisible
? ['\\', '/', '*', '?', '"', '<', '>', '|'] // Allow ":" char
: ['\\', '/', ':', '*', '?', '"', '<', '>', '|'];
}
}
private static readonly string[] RestrictedFileNames =
[
"CON", "PRN", "AUX",
"NUL", "COM1", "COM2",
"COM3", "COM4", "COM5",
"COM6", "COM7", "COM8",
"COM9", "LPT1", "LPT2",
"LPT3", "LPT4", "LPT5",
"LPT6", "LPT7", "LPT8", "LPT9"
];
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
public FilesystemHelpers(IShellPage associatedInstance, CancellationToken cancellationToken)
{
this.associatedInstance = associatedInstance;
this.cancellationToken = cancellationToken;
jumpListService = Ioc.Default.GetRequiredService<IWindowsJumpListService>();
filesystemOperations = new ShellFilesystemOperations(this.associatedInstance);
}
public async Task<(ReturnResult, IStorageItem?)> CreateAsync(IStorageItemWithPath source, bool registerHistory)
{
var returnStatus = ReturnResult.InProgress;
var progress = new Progress<StatusCenterItemProgressModel>();
progress.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
if (!IsValidForFilename(source.Name))
{
await DialogDisplayHelper.ShowDialogAsync(
"ErrorDialogThisActionCannotBeDone".GetLocalizedResource(),
"ErrorDialogNameNotAllowed".GetLocalizedResource());
return (ReturnResult.Failed, null);
}
var result = await filesystemOperations.CreateAsync(source, progress, cancellationToken);
if (registerHistory && !string.IsNullOrWhiteSpace(source.Path))
{
App.HistoryWrapper.AddHistory(result.Item1);
}
await Task.Yield();
return (returnStatus, result.Item2);
}
public async Task<ReturnResult> DeleteItemsAsync(IEnumerable<IStorageItemWithPath> source, DeleteConfirmationPolicies showDialog, bool permanently, bool registerHistory)
{
source = await source.ToListAsync();
var returnStatus = ReturnResult.InProgress;
var deleteFromRecycleBin = source.Select(item => item.Path).Any(StorageTrashBinService.IsUnderTrashBin);
var canBeSentToBin = !deleteFromRecycleBin && await StorageTrashBinService.CanGoTrashBin(source.FirstOrDefault()?.Path);
if (showDialog is DeleteConfirmationPolicies.Always ||
showDialog is DeleteConfirmationPolicies.PermanentOnly &&
(permanently || !canBeSentToBin))
{
var incomingItems = new List<BaseFileSystemDialogItemViewModel>();
List<ShellFileItem>? binItems = null;
foreach (var src in source)
{
if (StorageTrashBinService.IsUnderTrashBin(src.Path))
{
binItems ??= await StorageTrashBinService.GetAllRecycleBinFoldersAsync();
// Might still be null because we're deserializing the list from Json
if (!binItems.IsEmpty())
{
// Get original file name
var matchingItem = binItems.FirstOrDefault(x => x.RecyclePath == src.Path);
incomingItems.Add(new FileSystemDialogDefaultItemViewModel() { SourcePath = src.Path, DisplayName = matchingItem?.FileName ?? src.Name });
}
}
else
{
incomingItems.Add(new FileSystemDialogDefaultItemViewModel() { SourcePath = src.Path });
}
}
var dialogViewModel = FileSystemDialogViewModel.GetDialogViewModel(
new() { IsInDeleteMode = true },
(canBeSentToBin ? permanently : true, canBeSentToBin),
FilesystemOperationType.Delete,
incomingItems,
[]);
var dialogService = Ioc.Default.GetRequiredService<IDialogService>();
// Return if the result isn't delete
if (await dialogService.ShowDialogAsync(dialogViewModel) != DialogResult.Primary)
return ReturnResult.Cancelled;
// Delete selected items if the result is Yes
permanently = dialogViewModel.DeletePermanently;
}
else
{
// Delete permanently if recycle bin is not supported
permanently |= !canBeSentToBin;
}
// Add an in-progress card in the StatusCenter
var banner = permanently
? StatusCenterHelper.AddCard_Delete(returnStatus, source)
: StatusCenterHelper.AddCard_Recycle(returnStatus, source);
banner.ProgressEventSource.ProgressChanged += (s, e)
=> returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
var token = banner.CancellationToken;
var sw = new Stopwatch();
sw.Start();
IStorageHistory history = await filesystemOperations.DeleteItemsAsync((IList<IStorageItemWithPath>)source, banner.ProgressEventSource, permanently, token);
banner.Progress.ReportStatus(FileSystemStatusCode.Success);
await Task.Yield();
if (!permanently && registerHistory)
App.HistoryWrapper.AddHistory(history);
// Execute removal tasks concurrently in background
var sourcePaths = source.Select(x => x.Path);
_ = Task.WhenAll(sourcePaths.Select(jumpListService.RemoveFolderAsync));
var itemsCount = banner.TotalItemsCount;
// Remove the in-progress card from the StatusCenter
_statusCenterViewModel.RemoveItem(banner);
sw.Stop();
// Add a complete card in the StatusCenter
_ = permanently
? StatusCenterHelper.AddCard_Delete(token.IsCancellationRequested ? ReturnResult.Cancelled : returnStatus, source, itemsCount)
: StatusCenterHelper.AddCard_Recycle(token.IsCancellationRequested ? ReturnResult.Cancelled : returnStatus, source, itemsCount);
return returnStatus;
}
public Task<ReturnResult> DeleteItemAsync(IStorageItemWithPath source, DeleteConfirmationPolicies showDialog, bool permanently, bool registerHistory)
=> DeleteItemsAsync(source.CreateEnumerable(), showDialog, permanently, registerHistory);
public Task<ReturnResult> DeleteItemsAsync(IEnumerable<IStorageItem> source, DeleteConfirmationPolicies showDialog, bool permanently, bool registerHistory)
=> DeleteItemsAsync(source.Select((item) => item.FromStorageItem()), showDialog, permanently, registerHistory);
public Task<ReturnResult> DeleteItemAsync(IStorageItem source, DeleteConfirmationPolicies showDialog, bool permanently, bool registerHistory)
=> DeleteItemAsync(source.FromStorageItem(), showDialog, permanently, registerHistory);
public Task<ReturnResult> RestoreItemFromTrashAsync(IStorageItem source, string destination, bool registerHistory)
=> RestoreItemFromTrashAsync(source.FromStorageItem(), destination, registerHistory);
public Task<ReturnResult> RestoreItemsFromTrashAsync(IEnumerable<IStorageItem> source, IEnumerable<string> destination, bool registerHistory)
=> RestoreItemsFromTrashAsync(source.Select((item) => item.FromStorageItem()), destination, registerHistory);
public Task<ReturnResult> RestoreItemFromTrashAsync(IStorageItemWithPath source, string destination, bool registerHistory)
=> RestoreItemsFromTrashAsync(source.CreateEnumerable(), destination.CreateEnumerable(), registerHistory);
public async Task<ReturnResult> RestoreItemsFromTrashAsync(IEnumerable<IStorageItemWithPath> source, IEnumerable<string> destination, bool registerHistory)
{
source = await source.ToListAsync();
destination = await destination.ToListAsync();
var returnStatus = ReturnResult.InProgress;
var progress = new Progress<StatusCenterItemProgressModel>();
progress.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
var sw = new Stopwatch();
sw.Start();
IStorageHistory history = await filesystemOperations.RestoreItemsFromTrashAsync((IList<IStorageItemWithPath>)source, (IList<string>)destination, progress, cancellationToken);
await Task.Yield();
if (registerHistory && source.Any((item) => !string.IsNullOrWhiteSpace(item.Path)))
{
App.HistoryWrapper.AddHistory(history);
}
int itemsMoved = history?.Source.Count ?? 0;
sw.Stop();
return returnStatus;
}
public async Task<ReturnResult> PerformOperationTypeAsync(
DataPackageOperation operation,
DataPackageView packageView,
string destination,
bool showDialog,
bool registerHistory,
bool isTargetExecutable = false,
bool isTargetScriptFile = false)
{
try
{
if (destination is null)
{
return default;
}
if (destination.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal))
{
return await RecycleItemsFromClipboard(packageView, destination, UserSettingsService.FoldersSettingsService.DeleteConfirmationPolicy, registerHistory);
}
else if (operation.HasFlag(DataPackageOperation.Move))
{
return await MoveItemsFromClipboard(packageView, destination, showDialog, registerHistory);
}
else if (operation.HasFlag(DataPackageOperation.Copy))
{
return await CopyItemsFromClipboard(packageView, destination, showDialog, registerHistory);
}
else if (operation.HasFlag(DataPackageOperation.Link))
{
// Open with piggybacks off of the link operation, since there isn't one for it
if (isTargetExecutable || isTargetScriptFile)
{
var items = await GetDraggedStorageItems(packageView);
await NavigationHelpers.OpenItemsWithExecutableAsync(associatedInstance, items, destination);
return ReturnResult.Success;
}
else
{
return await CreateShortcutFromClipboard(packageView, destination, showDialog, registerHistory);
}
}
else if (operation.HasFlag(DataPackageOperation.None))
{
return await CopyItemsFromClipboard(packageView, destination, showDialog, registerHistory);
}
else
{
return default;
}
}
finally
{
packageView.ReportOperationCompleted(packageView.RequestedOperation);
}
}
public Task<ReturnResult> CopyItemsAsync(IEnumerable<IStorageItem> source, IEnumerable<string> destination, bool showDialog, bool registerHistory)
=> CopyItemsAsync(source.Select((item) => item.FromStorageItem()), destination, showDialog, registerHistory);
public Task<ReturnResult> CopyItemAsync(IStorageItem source, string destination, bool showDialog, bool registerHistory)
=> CopyItemAsync(source.FromStorageItem(), destination, showDialog, registerHistory);
public async Task<ReturnResult> CopyItemsAsync(IEnumerable<IStorageItemWithPath> source, IEnumerable<string> destination, bool showDialog, bool registerHistory)
{
source = await source.ToListAsync();
destination = await destination.ToListAsync();
var returnStatus = ReturnResult.InProgress;
var banner = StatusCenterHelper.AddCard_Copy(
returnStatus,
source,
destination);
banner.ProgressEventSource.ProgressChanged += (s, e)
=> returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
var token = banner.CancellationToken;
var (collisions, cancelOperation, itemsResult) = await GetCollision(FilesystemOperationType.Copy, source, destination, showDialog);
if (cancelOperation)
{
_statusCenterViewModel.RemoveItem(banner);
return ReturnResult.Cancelled;
}
itemManipulationModel?.ClearSelection();
IStorageHistory history = await filesystemOperations.CopyItemsAsync((IList<IStorageItemWithPath>)source, (IList<string>)destination, collisions, banner.ProgressEventSource, token);
banner.Progress.ReportStatus(FileSystemStatusCode.Success);
if (registerHistory && history is not null && source.Any((item) => !string.IsNullOrWhiteSpace(item.Path)))
{
foreach (var item in history.Source.Zip(history.Destination, (k, v) => new { Key = k, Value = v }).ToDictionary(k => k.Key, v => v.Value))
{
foreach (var item2 in itemsResult)
{
if (!string.IsNullOrEmpty(item2.CustomName) && item2.SourcePath == item.Key.Path && Path.GetFileName(item2.SourcePath) != item2.CustomName)
{
var renameHistory = await filesystemOperations.RenameAsync(item.Value, item2.CustomName, NameCollisionOption.FailIfExists, banner.ProgressEventSource, token);
history.Destination[history.Source.IndexOf(item.Key)] = renameHistory.Destination[0];
}
}
}
App.HistoryWrapper.AddHistory(history);
}
await Task.Yield();
var itemsCount = banner.TotalItemsCount;
_statusCenterViewModel.RemoveItem(banner);
StatusCenterHelper.AddCard_Copy(
token.IsCancellationRequested ? ReturnResult.Cancelled : returnStatus,
source,
destination,
itemsCount);
return returnStatus;
}
public Task<ReturnResult> CopyItemAsync(IStorageItemWithPath source, string destination, bool showDialog, bool registerHistory)
=> CopyItemsAsync(source.CreateEnumerable(), destination.CreateEnumerable(), showDialog, registerHistory);
public async Task<ReturnResult> CopyItemsFromClipboard(DataPackageView packageView, string destination, bool showDialog, bool registerHistory)
{
var source = await GetDraggedStorageItems(packageView);
if (!source.IsEmpty())
{
ReturnResult returnStatus = ReturnResult.InProgress;
var destinations = new List<string>();
List<ShellFileItem>? binItems = null;
foreach (var item in source)
{
if (StorageTrashBinService.IsUnderTrashBin(item.Path))
{
binItems ??= await StorageTrashBinService.GetAllRecycleBinFoldersAsync();
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
{
var matchingItem = binItems.FirstOrDefault(x => x.RecyclePath == item.Path); // Get original file name
destinations.Add(PathNormalization.Combine(destination, matchingItem?.FileName ?? item.Name));
}
}
else
{
destinations.Add(PathNormalization.Combine(destination, item.Name));
}
}
returnStatus = await CopyItemsAsync(source, destinations, showDialog, registerHistory);
return returnStatus;
}
if (packageView.Contains(StandardDataFormats.Bitmap))
{
try
{
var imgSource = await packageView.GetBitmapAsync();
using var imageStream = await imgSource.OpenReadAsync();
var folder = await StorageFileExtensions.DangerousGetFolderFromPathAsync(destination);
// Set the name of the file to be the current time and date
var file = await folder.CreateFileAsync($"{DateTime.Now:MM-dd-yy-HHmmss}.png", CreationCollisionOption.GenerateUniqueName);
SoftwareBitmap softwareBitmap;
// Create the decoder from the stream
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);
// Get the SoftwareBitmap representation of the file
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
await BitmapHelper.SaveSoftwareBitmapToFileAsync(softwareBitmap, file, BitmapEncoder.PngEncoderId);
return ReturnResult.Success;
}
catch (Exception)
{
return ReturnResult.UnknownException;
}
}
// Happens if you copy some text and then you Ctrl+V in Files
return ReturnResult.BadArgumentException;
}
public Task<ReturnResult> MoveItemsAsync(IEnumerable<IStorageItem> source, IEnumerable<string> destination, bool showDialog, bool registerHistory)
=> MoveItemsAsync(source.Select((item) => item.FromStorageItem()), destination, showDialog, registerHistory);
public Task<ReturnResult> MoveItemAsync(IStorageItem source, string destination, bool showDialog, bool registerHistory)
=> MoveItemAsync(source.FromStorageItem(), destination, showDialog, registerHistory);
public async Task<ReturnResult> MoveItemsAsync(IEnumerable<IStorageItemWithPath> source, IEnumerable<string> destination, bool showDialog, bool registerHistory)
{
source = await source.ToListAsync();
destination = await destination.ToListAsync();
var returnStatus = ReturnResult.InProgress;
var banner = StatusCenterHelper.AddCard_Move(
returnStatus,
source,
destination);
banner.ProgressEventSource.ProgressChanged += (s, e)
=> returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
var token = banner.CancellationToken;
var (collisions, cancelOperation, itemsResult) = await GetCollision(FilesystemOperationType.Move, source, destination, showDialog);
if (cancelOperation)
{
_statusCenterViewModel.RemoveItem(banner);
return ReturnResult.Cancelled;
}
var sw = new Stopwatch();
sw.Start();
itemManipulationModel?.ClearSelection();
IStorageHistory history = await filesystemOperations.MoveItemsAsync((IList<IStorageItemWithPath>)source, (IList<string>)destination, collisions, banner.ProgressEventSource, token);
banner.Progress.ReportStatus(FileSystemStatusCode.Success);
await Task.Yield();
if (registerHistory && history is not null && source.Any((item) => !string.IsNullOrWhiteSpace(item.Path)))
{
foreach (var item in history.Source.Zip(history.Destination, (k, v) => new { Key = k, Value = v }).ToDictionary(k => k.Key, v => v.Value))
{
foreach (var item2 in itemsResult)
{
if (!string.IsNullOrEmpty(item2.CustomName) && item2.SourcePath == item.Key.Path)
{
var renameHistory = await filesystemOperations.RenameAsync(item.Value, item2.CustomName, NameCollisionOption.FailIfExists, banner.ProgressEventSource, token);
history.Destination[history.Source.IndexOf(item.Key)] = renameHistory.Destination[0];
}
}
}
App.HistoryWrapper.AddHistory(history);
}
// Execute removal tasks concurrently in background
var sourcePaths = source.Select(x => x.Path);
_ = Task.WhenAll(sourcePaths.Select(jumpListService.RemoveFolderAsync));
var itemsCount = banner.TotalItemsCount;
_statusCenterViewModel.RemoveItem(banner);
sw.Stop();
StatusCenterHelper.AddCard_Move(
token.IsCancellationRequested ? ReturnResult.Cancelled : returnStatus,
source,
destination,
itemsCount);
return returnStatus;
}
public Task<ReturnResult> MoveItemAsync(IStorageItemWithPath source, string destination, bool showDialog, bool registerHistory)
=> MoveItemsAsync(source.CreateEnumerable(), destination.CreateEnumerable(), showDialog, registerHistory);
public async Task<ReturnResult> MoveItemsFromClipboard(DataPackageView packageView, string destination, bool showDialog, bool registerHistory)
{
if (!HasDraggedStorageItems(packageView))
{
// Happens if you copy some text and then you Ctrl+V in Files
return ReturnResult.BadArgumentException;
}
var source = await GetDraggedStorageItems(packageView);
ReturnResult returnStatus = ReturnResult.InProgress;
var destinations = new List<string>();
List<ShellFileItem>? binItems = null;
foreach (var item in source)
{
if (StorageTrashBinService.IsUnderTrashBin(item.Path))
{
binItems ??= await StorageTrashBinService.GetAllRecycleBinFoldersAsync();
if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json
{
var matchingItem = binItems.FirstOrDefault(x => x.RecyclePath == item.Path); // Get original file name
destinations.Add(PathNormalization.Combine(destination, matchingItem?.FileName ?? item.Name));
}
}
else
{
destinations.Add(PathNormalization.Combine(destination, item.Name));
}
}
returnStatus = await MoveItemsAsync(source, destinations, showDialog, registerHistory);
return returnStatus;
}
public Task<ReturnResult> RenameAsync(IStorageItem source, string newName, NameCollisionOption collision, bool registerHistory, bool showExtensionDialog = true)
=> RenameAsync(source.FromStorageItem(), newName, collision, registerHistory, showExtensionDialog);
public async Task<ReturnResult> RenameAsync(IStorageItemWithPath source, string newName, NameCollisionOption collision, bool registerHistory, bool showExtensionDialog = true)
{
var returnStatus = ReturnResult.InProgress;
var progress = new Progress<StatusCenterItemProgressModel>();
progress.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
if (!IsValidForFilename(newName))
{
await DialogDisplayHelper.ShowDialogAsync(
"ErrorDialogThisActionCannotBeDone".GetLocalizedResource(),
"ErrorDialogNameNotAllowed".GetLocalizedResource());
return ReturnResult.Failed;
}
IStorageHistory? history = null;
switch (source.ItemType)
{
case FilesystemItemType.Directory:
history = await filesystemOperations.RenameAsync(source, newName, collision, progress, cancellationToken);
break;
// Prompt user when extension has changed, not when file name has changed
case FilesystemItemType.File:
if
(
showExtensionDialog &&
Path.GetExtension(source.Path) != Path.GetExtension(newName) &&
UserSettingsService.FoldersSettingsService.ShowFileExtensionWarning
)
{
var yesSelected = await DialogDisplayHelper.ShowDialogAsync("Rename".GetLocalizedResource(), "RenameFileDialog/Text".GetLocalizedResource(), "Yes".GetLocalizedResource(), "No".GetLocalizedResource());
if (yesSelected)
{
history = await filesystemOperations.RenameAsync(source, newName, collision, progress, cancellationToken);
break;
}
break;
}
history = await filesystemOperations.RenameAsync(source, newName, collision, progress, cancellationToken);
break;
default:
history = await filesystemOperations.RenameAsync(source, newName, collision, progress, cancellationToken);
break;
}
if (registerHistory && !string.IsNullOrWhiteSpace(source.Path))
{
App.HistoryWrapper.AddHistory(history);
}
await jumpListService.RemoveFolderAsync(source.Path); // Remove items from jump list
await Task.Yield();
return returnStatus;
}
public async Task<ReturnResult> CreateShortcutFromClipboard(DataPackageView packageView, string destination, bool showDialog, bool registerHistory)
{
if (!HasDraggedStorageItems(packageView))
{
// Happens if you copy some text and then you Ctrl+V in Files
return ReturnResult.BadArgumentException;
}
var source = await GetDraggedStorageItems(packageView);
var returnStatus = ReturnResult.InProgress;
var progress = new Progress<StatusCenterItemProgressModel>();
progress.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
source = source.Where(x => !string.IsNullOrEmpty(x.Path));
var dest = source.Select(x => Path.Combine(destination, FilesystemHelpers.GetShortcutNamingPreference(x.Name)));
source = await source.ToListAsync();
dest = await dest.ToListAsync();
var history = await filesystemOperations.CreateShortcutItemsAsync((IList<IStorageItemWithPath>)source, (IList<string>)dest, progress, cancellationToken);
if (registerHistory)
{
App.HistoryWrapper.AddHistory(history);
}
await Task.Yield();
return returnStatus;
}
public async Task<ReturnResult> RecycleItemsFromClipboard(DataPackageView packageView, string destination, DeleteConfirmationPolicies showDialog, bool registerHistory)
{
if (!HasDraggedStorageItems(packageView))
{
// Happens if you copy some text and then you Ctrl+V in Files
return ReturnResult.BadArgumentException;
}
var source = await GetDraggedStorageItems(packageView);
ReturnResult returnStatus = ReturnResult.InProgress;
source = source.Where(x => !StorageTrashBinService.IsUnderTrashBin(x.Path)); // Can't recycle items already in recyclebin
returnStatus = await DeleteItemsAsync(source, showDialog, false, registerHistory);
return returnStatus;
}
public static bool IsValidForFilename(string name)
=> !string.IsNullOrWhiteSpace(name) && !ContainsRestrictedCharacters(name) && !ContainsRestrictedFileName(name);
private static async Task<(List<FileNameConflictResolveOptionType> collisions, bool cancelOperation, IEnumerable<IFileSystemDialogConflictItemViewModel>)> GetCollision(FilesystemOperationType operationType, IEnumerable<IStorageItemWithPath> source, IEnumerable<string> destination, bool forceDialog)
{
var incomingItems = new List<BaseFileSystemDialogItemViewModel>();
var conflictingItems = new List<BaseFileSystemDialogItemViewModel>();
var collisions = new Dictionary<string, FileNameConflictResolveOptionType>();
foreach (var item in source.Zip(destination, (src, dest, index) => new { src, dest, index }))
{
var itemPathOrName = string.IsNullOrEmpty(item.src.Path) ? item.src.Item.Name : item.src.Path;
incomingItems.Add(new FileSystemDialogConflictItemViewModel() { ConflictResolveOption = FileNameConflictResolveOptionType.None, SourcePath = itemPathOrName, DestinationPath = item.dest, DestinationDisplayName = Path.GetFileName(item.dest) });
var path = incomingItems.ElementAt(item.index).SourcePath;
if (path is not null && collisions.ContainsKey(path))
{
// Something strange happened, log
App.Logger.LogWarning($"Duplicate key when resolving conflicts: {incomingItems.ElementAt(item.index).SourcePath}, {item.src.Name}\n" +
$"Source: {string.Join(", ", source.Select(x => string.IsNullOrEmpty(x.Path) ? x.Item.Name : x.Path))}");
}
collisions.AddIfNotPresent(incomingItems.ElementAt(item.index).SourcePath, FileNameConflictResolveOptionType.GenerateNewName);
// Assume GenerateNewName when source and destination are the same
if (string.IsNullOrEmpty(item.src.Path) || item.src.Path != item.dest)
{
// Same item names in both directories
if (StorageHelpers.Exists(item.dest) ||
(FtpHelpers.IsFtpPath(item.dest) &&
await Ioc.Default.GetRequiredService<IFtpStorageService>().TryGetFileAsync(item.dest) is not null))
{
(incomingItems[item.index] as FileSystemDialogConflictItemViewModel)!.ConflictResolveOption = FileNameConflictResolveOptionType.GenerateNewName;
conflictingItems.Add(incomingItems.ElementAt(item.index));
}
}
}
IEnumerable<IFileSystemDialogConflictItemViewModel>? itemsResult = null;
var mustResolveConflicts = !conflictingItems.IsEmpty();
if (mustResolveConflicts || forceDialog)
{
var dialogService = Ioc.Default.GetRequiredService<IDialogService>();
var dialogViewModel = FileSystemDialogViewModel.GetDialogViewModel(
new() { ConflictsExist = mustResolveConflicts },
(false, false),
operationType,
incomingItems.Except(conflictingItems).ToList(), // TODO: Could be optimized
conflictingItems);
var result = await dialogService.ShowDialogAsync(dialogViewModel);
itemsResult = dialogViewModel.GetItemsResult();
if (mustResolveConflicts) // If there were conflicts, result buttons are different
{
if (result != DialogResult.Primary) // Operation was cancelled
{
return ([], true, itemsResult);
}
}
collisions.Clear();
foreach (var item in itemsResult)
{
collisions.AddIfNotPresent(item.SourcePath, item.ConflictResolveOption);
}
}
// Since collisions are scrambled, we need to sort them PATH--PATH
var newCollisions = new List<FileNameConflictResolveOptionType>();
foreach (var src in source)
{
var itemPathOrName = string.IsNullOrEmpty(src.Path) ? src.Item.Name : src.Path;
var match = collisions.SingleOrDefault(x => x.Key == itemPathOrName);
var fileNameConflictResolveOptionType = (match.Key is not null) ? match.Value : FileNameConflictResolveOptionType.Skip;
newCollisions.Add(fileNameConflictResolveOptionType);
}
return (newCollisions, false, itemsResult ?? new List<IFileSystemDialogConflictItemViewModel>());
}
public static bool HasDraggedStorageItems(DataPackageView packageView)
{
return packageView is not null && (packageView.Contains(StandardDataFormats.StorageItems) || packageView.Contains("FileDrop"));
}
public static async Task<IEnumerable<IStorageItemWithPath>> GetDraggedStorageItems(DataPackageView packageView)
{
var itemsList = new List<IStorageItemWithPath>();
var hasVirtualItems = false;
if (packageView.Contains(StandardDataFormats.StorageItems))
{
try
{
var source = await packageView.GetStorageItemsAsync();
itemsList.AddRange(source.Select(x => x.FromStorageItem()));
}
catch (Exception ex) when ((uint)ex.HResult == 0x80040064 || (uint)ex.HResult == 0x8004006A)
{
hasVirtualItems = true;
}
catch (Exception ex)
{
App.Logger.LogWarning(ex, ex.Message);
return itemsList;
}
}
// workaround for pasting folders from remote desktop (#12318)
try
{
if (hasVirtualItems && packageView.Contains("FileContents"))
{
var descriptor = NativeClipboard.CurrentDataObject.GetData<Shell32.FILEGROUPDESCRIPTOR>("FileGroupDescriptorW");
for (var ii = 0; ii < descriptor.cItems; ii++)
{
if (descriptor.fgd[ii].dwFileAttributes.HasFlag(FileFlagsAndAttributes.FILE_ATTRIBUTE_DIRECTORY))
itemsList.Add(new VirtualStorageFolder(descriptor.fgd[ii].cFileName).FromStorageItem());
else if (NativeClipboard.CurrentDataObject.GetData("FileContents", DVASPECT.DVASPECT_CONTENT, ii) is IStream stream)
{
var streamContent = new ComStreamWrapper(stream);
itemsList.Add(new VirtualStorageFile(streamContent, descriptor.fgd[ii].cFileName).FromStorageItem());
}
}
}
}
catch (Exception ex)
{
App.Logger.LogWarning(ex, ex.Message);
}
// workaround for GetStorageItemsAsync() bug that only yields 16 items at most
// https://learn.microsoft.com/windows/win32/shell/clipboard#cf_hdrop
if (packageView.Contains("FileDrop"))
{
var fileDropData = await SafetyExtensions.IgnoreExceptions(
() => packageView.GetDataAsync("FileDrop").AsTask());
if (fileDropData is IRandomAccessStream stream)
{
stream.Seek(0);
byte[]? dropBytes = null;
int bytesRead = 0;
try
{
dropBytes = new byte[stream.Size];
bytesRead = await stream.AsStreamForRead().ReadAsync(dropBytes);
}
catch (COMException)
{
}
if (bytesRead > 0)
{
IntPtr dropStructPointer = Marshal.AllocHGlobal(dropBytes!.Length);
try
{
Marshal.Copy(dropBytes, 0, dropStructPointer, dropBytes.Length);
HDROP dropStructHandle = new(dropStructPointer);
var itemPaths = new List<string>();
uint filesCount = Shell32.DragQueryFile(dropStructHandle, 0xffffffff, null, 0);
for (uint i = 0; i < filesCount; i++)
{
uint charsNeeded = Shell32.DragQueryFile(dropStructHandle, i, null, 0);
uint bufferSpaceRequired = charsNeeded + 1; // include space for terminating null character
string buffer = new('\0', (int)bufferSpaceRequired);
uint charsCopied = Shell32.DragQueryFile(dropStructHandle, i, buffer, bufferSpaceRequired);
if (charsCopied > 0)
{
string path = buffer[..(int)charsCopied];
itemPaths.Add(Path.GetFullPath(path));
}
}
foreach (var path in itemPaths)
{
var isDirectory = Win32Helper.HasFileAttribute(path, FileAttributes.Directory);
itemsList.Add(StorageHelpers.FromPathAndType(path, isDirectory ? FilesystemItemType.Directory : FilesystemItemType.File));
}
}
finally
{
Marshal.FreeHGlobal(dropStructPointer);
}
}
}
}
itemsList = itemsList.DistinctBy(x => string.IsNullOrEmpty(x.Path) ? x.Item.Name : x.Path).ToList();
return itemsList;
}
public static string FilterRestrictedCharacters(string input)
{
int invalidCharIndex;
while ((invalidCharIndex = input.IndexOfAny(RestrictedCharacters)) >= 0)
{
input = input.Remove(invalidCharIndex, 1);
}
return input;
}
public static bool ContainsRestrictedCharacters(string input)
{
return input.IndexOfAny(RestrictedCharacters) >= 0;
}
public static bool ContainsRestrictedFileName(string input)
{
foreach (string name in RestrictedFileNames)
{
if (input.StartsWith(name, StringComparison.OrdinalIgnoreCase) && (input.Length == name.Length || input[name.Length] == '.'))
return true;
}
return false;
}
/// <summary>
/// Gets the shortcut naming template from File Explorer
/// </summary>
public static string GetShortcutNamingPreference(string itemName)
{
var keyName = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\NamingTemplates";
var value = Registry.GetValue(keyName, "ShortcutNameTemplate", null);
if (value is null)
return string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), itemName) + ".lnk";
else
{
// Trim the quotes and the "%s" from the string
value = value?.ToString()?.TrimStart(['"', '%', 's']);
value = value?.ToString()?.TrimEnd(['"']);
return itemName + value;
}
}
public void Dispose()
{
filesystemOperations?.Dispose();
// SUPPRESS: Cannot convert null literal to non-nullable reference type.
#pragma warning disable CS8625
associatedInstance = null;
filesystemOperations = null;
}
}
}