@@ -46,23 +46,23 @@ public FilesystemOperations(IShellPage associatedInstance)
46
46
47
47
#region IFilesystemOperations
48
48
49
- public async Task < IStorageHistory > CreateAsync ( PathWithType source , IProgress < FilesystemErrorCode > errorCode , CancellationToken cancellationToken )
49
+ public async Task < IStorageHistory > CreateAsync ( IStorageItemWithPath source , IProgress < FilesystemErrorCode > errorCode , CancellationToken cancellationToken )
50
50
{
51
51
try
52
52
{
53
53
switch ( source . ItemType )
54
54
{
55
55
case FilesystemItemType . File :
56
56
{
57
- StorageFolder folder = await StorageFolder . GetFolderFromPathAsync ( Path . GetDirectoryName ( source . Path ) ) ;
57
+ StorageFolder folder = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( source . Path ) ) ;
58
58
await folder . CreateFileAsync ( Path . GetFileName ( source . Path ) ) ;
59
59
60
60
break ;
61
61
}
62
62
63
63
case FilesystemItemType . Directory :
64
64
{
65
- StorageFolder folder = await StorageFolder . GetFolderFromPathAsync ( Path . GetDirectoryName ( source . Path ) ) ;
65
+ StorageFolder folder = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( source . Path ) ) ;
66
66
await folder . CreateFolderAsync ( Path . GetFileName ( source . Path ) ) ;
67
67
68
68
break ;
@@ -95,15 +95,14 @@ public async Task<IStorageHistory> CopyAsync(IStorageItem source,
95
95
IProgress < FilesystemErrorCode > errorCode ,
96
96
CancellationToken cancellationToken )
97
97
{
98
- return await CopyAsync ( new PathWithType ( source . Path ,
99
- source . IsOfType ( StorageItemTypes . File ) ? FilesystemItemType . File : FilesystemItemType . Directory ) ,
98
+ return await CopyAsync ( source . FromStorageItem ( ) ,
100
99
destination ,
101
100
progress ,
102
101
errorCode ,
103
102
cancellationToken ) ;
104
103
}
105
104
106
- public async Task < IStorageHistory > CopyAsync ( PathWithType source ,
105
+ public async Task < IStorageHistory > CopyAsync ( IStorageItemWithPath source ,
107
106
string destination ,
108
107
IProgress < float > progress ,
109
108
IProgress < FilesystemErrorCode > errorCode ,
@@ -122,12 +121,12 @@ await DialogDisplayHelper.ShowDialogAsync(
122
121
}
123
122
124
123
IStorageItem copiedItem = null ;
125
- long itemSize = await FilesystemHelpers . GetItemSize ( await source . Path . ToStorageItem ( ) ) ;
124
+ long itemSize = await FilesystemHelpers . GetItemSize ( await source . ToStorageItem ( associatedInstance ) ) ;
126
125
bool reportProgress = false ; // TODO: The default value is false
127
126
128
127
if ( source . ItemType == FilesystemItemType . Directory )
129
128
{
130
- if ( string . IsNullOrWhiteSpace ( source . Path ) ||
129
+ if ( ! string . IsNullOrWhiteSpace ( source . Path ) &&
131
130
Path . GetDirectoryName ( destination ) . IsSubPathOf ( source . Path ) ) // We check if user tried to copy anything above the source.ItemPath
132
131
{
133
132
ImpossibleActionResponseTypes responseType = ImpossibleActionResponseTypes . Abort ;
@@ -169,14 +168,14 @@ await DialogDisplayHelper.ShowDialogAsync(
169
168
progress ? . Report ( ( float ) ( itemSize * 100.0f / itemSize ) ) ;
170
169
}
171
170
172
- FilesystemResult < StorageFolder > fsSourceFolderResult = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( source . Path ) ) ;
173
- FilesystemResult < StorageFolder > fsDestinationFolderResult = await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( destination ) ) ;
171
+ StorageFolder fsSourceFolder = ( StorageFolder ) await source . ToStorageItem ( associatedInstance ) ;
172
+ StorageFolder fsDestinationFolder = ( StorageFolder ) await associatedInstance . FilesystemViewModel . GetFolderFromPathAsync ( Path . GetDirectoryName ( destination ) ) ;
174
173
175
- if ( fsSourceFolderResult && fsDestinationFolderResult )
174
+ if ( fsSourceFolder != null && fsDestinationFolder != null )
176
175
{
177
176
FilesystemResult fsCopyResult = await FilesystemTasks . Wrap ( async ( ) =>
178
177
{
179
- return await FilesystemHelpers . CloneDirectoryAsync ( fsSourceFolderResult . Result , fsDestinationFolderResult . Result , Path . GetFileName ( source . Path ) ) ;
178
+ return await FilesystemHelpers . CloneDirectoryAsync ( fsSourceFolder , fsDestinationFolder , fsSourceFolder . Name ) ;
180
179
} )
181
180
. OnSuccess ( t =>
182
181
{
@@ -201,15 +200,15 @@ await DialogDisplayHelper.ShowDialogAsync(
201
200
202
201
if ( fsResult )
203
202
{
204
- StorageFile file = ( StorageFile ) await source . Path . ToStorageItem ( ) ;
203
+ StorageFile file = ( StorageFile ) await source . ToStorageItem ( associatedInstance ) ;
205
204
206
205
FilesystemResult < StorageFile > fsResultCopy = new FilesystemResult < StorageFile > ( null , FilesystemErrorCode . ERROR_GENERIC ) ;
207
206
208
207
if ( file != null )
209
208
{
210
209
fsResultCopy = await FilesystemTasks . Wrap ( ( ) =>
211
210
{
212
- return file . CopyAsync ( fsResult . Result , Path . GetFileName ( source . Path ) , NameCollisionOption . GenerateUniqueName ) . AsTask ( ) ;
211
+ return file . CopyAsync ( fsResult . Result , Path . GetFileName ( file . Name ) , NameCollisionOption . GenerateUniqueName ) . AsTask ( ) ;
213
212
} ) ;
214
213
}
215
214
@@ -222,7 +221,7 @@ await DialogDisplayHelper.ShowDialogAsync(
222
221
// Try again with CopyFileFromApp
223
222
if ( NativeFileOperationsHelper . CopyFileFromApp ( source . Path , destination , true ) )
224
223
{
225
- copiedItem = await source . Path . ToStorageItem ( ) ; // Dangerous - the provided item may be different than output result!
224
+ copiedItem = await source . ToStorageItem ( associatedInstance ) ; // Dangerous - the provided item may be different than output result!
226
225
}
227
226
else
228
227
{
@@ -253,9 +252,7 @@ await DialogDisplayHelper.ShowDialogAsync(
253
252
254
253
progress ? . Report ( 100.0f ) ;
255
254
256
- var pathWithType = new PathWithType (
257
- copiedItem != null ? ( ! string . IsNullOrWhiteSpace ( copiedItem . Path ) ? copiedItem . Path : destination ) : destination ,
258
- source . ItemType ) ;
255
+ var pathWithType = copiedItem . FromStorageItem ( destination , source . ItemType ) ;
259
256
260
257
return new StorageHistory ( FileOperationType . Copy , source , pathWithType ) ;
261
258
}
@@ -266,15 +263,14 @@ public async Task<IStorageHistory> MoveAsync(IStorageItem source,
266
263
IProgress < FilesystemErrorCode > errorCode ,
267
264
CancellationToken cancellationToken )
268
265
{
269
- return await MoveAsync ( new PathWithType ( source . Path ,
270
- source . IsOfType ( StorageItemTypes . File ) ? FilesystemItemType . File : FilesystemItemType . Directory ) ,
266
+ return await MoveAsync ( source . FromStorageItem ( ) ,
271
267
destination ,
272
268
progress ,
273
269
errorCode ,
274
270
cancellationToken ) ;
275
271
}
276
272
277
- public async Task < IStorageHistory > MoveAsync ( PathWithType source ,
273
+ public async Task < IStorageHistory > MoveAsync ( IStorageItemWithPath source ,
278
274
string destination ,
279
275
IProgress < float > progress ,
280
276
IProgress < FilesystemErrorCode > errorCode ,
@@ -303,15 +299,14 @@ public async Task<IStorageHistory> DeleteAsync(IStorageItem source,
303
299
bool permanently ,
304
300
CancellationToken cancellationToken )
305
301
{
306
- return await DeleteAsync ( new PathWithType ( source . Path ,
307
- source . IsOfType ( StorageItemTypes . File ) ? FilesystemItemType . File : FilesystemItemType . Directory ) ,
302
+ return await DeleteAsync ( source . FromStorageItem ( ) ,
308
303
progress ,
309
304
errorCode ,
310
305
permanently ,
311
306
cancellationToken ) ;
312
307
}
313
308
314
- public async Task < IStorageHistory > DeleteAsync ( PathWithType source ,
309
+ public async Task < IStorageHistory > DeleteAsync ( IStorageItemWithPath source ,
315
310
IProgress < float > progress ,
316
311
IProgress < FilesystemErrorCode > errorCode ,
317
312
bool permanently ,
@@ -392,7 +387,7 @@ await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
392
387
// Get newest file
393
388
ShellFileItem item = nameMatchItems . Where ( ( item ) => item . RecycleDate != null ) . OrderBy ( ( item ) => item . RecycleDate ) . FirstOrDefault ( ) ;
394
389
395
- return new StorageHistory ( FileOperationType . Recycle , source , new PathWithType ( item ? . RecyclePath , source . ItemType ) ) ;
390
+ return new StorageHistory ( FileOperationType . Recycle , source , StorageItemHelpers . FromPathAndType ( item ? . RecyclePath , source . ItemType ) ) ;
396
391
}
397
392
398
393
return new StorageHistory ( FileOperationType . Delete , source , null ) ;
@@ -427,7 +422,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItem source,
427
422
await source . RenameAsync ( newName , collision ) ;
428
423
429
424
errorCode ? . Report ( FilesystemErrorCode . ERROR_SUCCESS ) ;
430
- return new StorageHistory ( FileOperationType . Rename , new PathWithType ( originalSource , itemType ) , new PathWithType ( source . Path , itemType ) ) ;
425
+ return new StorageHistory ( FileOperationType . Rename , StorageItemHelpers . FromPathAndType ( originalSource , itemType ) , source . FromStorageItem ( ) ) ;
431
426
}
432
427
catch ( Exception e )
433
428
{
@@ -439,7 +434,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItem source,
439
434
return null ;
440
435
}
441
436
442
- public async Task < IStorageHistory > RenameAsync ( PathWithType source ,
437
+ public async Task < IStorageHistory > RenameAsync ( IStorageItemWithPath source ,
443
438
string newName ,
444
439
NameCollisionOption collision ,
445
440
IProgress < FilesystemErrorCode > errorCode ,
@@ -457,11 +452,11 @@ public async Task<IStorageHistory> RenameAsync(PathWithType source,
457
452
{
458
453
try
459
454
{
460
- IStorageItem itemToRename = await source . Path . ToStorageItem ( ) ;
455
+ IStorageItem itemToRename = await source . ToStorageItem ( associatedInstance ) ;
461
456
await itemToRename . RenameAsync ( newName , collision ) ;
462
457
463
458
errorCode ? . Report ( FilesystemErrorCode . ERROR_SUCCESS ) ;
464
- return new StorageHistory ( FileOperationType . Rename , source , new PathWithType ( itemToRename . Path , source . ItemType ) ) ;
459
+ return new StorageHistory ( FileOperationType . Rename , source , itemToRename . FromStorageItem ( ) ) ;
465
460
}
466
461
catch ( Exception e )
467
462
{
@@ -473,7 +468,7 @@ public async Task<IStorageHistory> RenameAsync(PathWithType source,
473
468
return null ;
474
469
}
475
470
476
- public async Task < IStorageHistory > RestoreFromTrashAsync ( PathWithType source ,
471
+ public async Task < IStorageHistory > RestoreFromTrashAsync ( IStorageItemWithPath source ,
477
472
string destination ,
478
473
IProgress < float > progress ,
479
474
IProgress < FilesystemErrorCode > errorCode ,
@@ -490,7 +485,7 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(PathWithType source,
490
485
fsResult = sourceFolder . ErrorCode | destinationFolder . ErrorCode ;
491
486
errorCode ? . Report ( fsResult ) ;
492
487
493
- if ( sourceFolder && destinationFolder )
488
+ if ( fsResult )
494
489
{
495
490
fsResult = await FilesystemTasks . Wrap ( ( ) =>
496
491
{
@@ -510,7 +505,7 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(PathWithType source,
510
505
fsResult = sourceFile . ErrorCode | destinationFolder . ErrorCode ;
511
506
errorCode ? . Report ( fsResult ) ;
512
507
513
- if ( sourceFile && destinationFolder )
508
+ if ( fsResult )
514
509
{
515
510
fsResult = await FilesystemTasks . Wrap ( ( ) =>
516
511
{
@@ -519,13 +514,21 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(PathWithType source,
519
514
NameCollisionOption . GenerateUniqueName ) . AsTask ( ) ;
520
515
} ) ;
521
516
}
517
+ else if ( fsResult == FilesystemErrorCode . ERROR_UNAUTHORIZED )
518
+ {
519
+ // Try again with MoveFileFromApp
520
+ fsResult = ( FilesystemResult ) NativeFileOperationsHelper . MoveFileFromApp ( source . Path , destination ) ;
521
+ }
522
522
errorCode ? . Report ( fsResult ) ;
523
523
}
524
524
525
- // Recycle bin also stores a file starting with $I for each item
526
- string iFilePath = Path . Combine ( Path . GetDirectoryName ( source . Path ) , Path . GetFileName ( source . Path ) . Replace ( "$R" , "$I" ) ) ;
527
- await associatedInstance . FilesystemViewModel . GetFileFromPathAsync ( iFilePath )
528
- . OnSuccess ( iFile => iFile . DeleteAsync ( ) . AsTask ( ) ) ;
525
+ if ( fsResult )
526
+ {
527
+ // Recycle bin also stores a file starting with $I for each item
528
+ string iFilePath = Path . Combine ( Path . GetDirectoryName ( source . Path ) , Path . GetFileName ( source . Path ) . Replace ( "$R" , "$I" ) ) ;
529
+ await associatedInstance . FilesystemViewModel . GetFileFromPathAsync ( iFilePath )
530
+ . OnSuccess ( iFile => iFile . DeleteAsync ( ) . AsTask ( ) ) ;
531
+ }
529
532
530
533
errorCode ? . Report ( fsResult ) ;
531
534
if ( fsResult != FilesystemErrorCode . ERROR_SUCCESS )
@@ -544,7 +547,7 @@ await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
544
547
}
545
548
}
546
549
547
- return new StorageHistory ( FileOperationType . Restore , source , new PathWithType ( destination , source . ItemType ) ) ;
550
+ return new StorageHistory ( FileOperationType . Restore , source , StorageItemHelpers . FromPathAndType ( destination , source . ItemType ) ) ;
548
551
}
549
552
550
553
#endregion IFilesystemOperations
0 commit comments