Skip to content

Commit 5ed2952

Browse files
committed
Simplified FilesystemOperations
1 parent af74cdb commit 5ed2952

File tree

6 files changed

+56
-135
lines changed

6 files changed

+56
-135
lines changed

Files/Filesystem/FilesystemOperations/FilesystemOperations.cs

+14-113
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public FilesystemOperations(IShellPage associatedInstance)
6565
var newEntryInfo = await RegistryHelper.GetNewContextMenuEntryForType(Path.GetExtension(source.Path));
6666
if (newEntryInfo == null)
6767
{
68-
BaseStorageFolder folder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(source.Path));
68+
BaseStorageFolder folder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(source.Path));
6969
item = await folder.CreateFileAsync(Path.GetFileName(source.Path));
7070
}
7171
else
@@ -78,7 +78,7 @@ public FilesystemOperations(IShellPage associatedInstance)
7878

7979
case FilesystemItemType.Directory:
8080
{
81-
BaseStorageFolder folder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(source.Path));
81+
BaseStorageFolder folder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(source.Path));
8282
item = await folder.CreateFolderAsync(Path.GetFileName(source.Path));
8383

8484
break;
@@ -145,7 +145,7 @@ await DialogDisplayHelper.ShowDialogAsync(
145145
if (source.ItemType == FilesystemItemType.Directory)
146146
{
147147
if (!string.IsNullOrWhiteSpace(source.Path) &&
148-
Path.GetDirectoryName(destination).IsSubPathOf(source.Path)) // We check if user tried to copy anything above the source.ItemPath
148+
PathNormalization.GetParentDir(destination).IsSubPathOf(source.Path)) // We check if user tried to copy anything above the source.ItemPath
149149
{
150150
var destinationName = destination.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last();
151151
var sourceName = source.Path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last();
@@ -171,11 +171,11 @@ await DialogDisplayHelper.ShowDialogAsync(
171171
}
172172
return null;
173173
}
174-
else if (!FtpHelpers.IsFtpPath(destination) && !FtpHelpers.IsFtpPath(source.Path))
174+
else
175175
{
176176
// CopyFileFromApp only works on file not directories
177177
var fsSourceFolder = await source.ToStorageItemResult(associatedInstance);
178-
var fsDestinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
178+
var fsDestinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
179179
var fsResult = (FilesystemResult)(fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode);
180180

181181
if (fsResult)
@@ -218,38 +218,16 @@ await DialogDisplayHelper.ShowDialogAsync(
218218
return null;
219219
}
220220
}
221-
else if (FtpHelpers.IsFtpPath(destination) && !FtpHelpers.IsFtpPath(source.Path))
222-
{
223-
var fsSourceFolder = await source.ToStorageItemResult(associatedInstance);
224-
var ftpDestFolder = await new StorageFolderWithPath(null, destination).ToStorageItemResult(associatedInstance);
225-
var fsCopyResult = await FilesystemTasks.Wrap(() => CloneDirectoryToFtpAsync((BaseStorageFolder)fsSourceFolder, (FtpStorageFolder)ftpDestFolder.Result, collision.Convert()));
226-
227-
if (fsCopyResult == FileSystemStatusCode.AlreadyExists)
228-
{
229-
errorCode?.Report(FileSystemStatusCode.AlreadyExists);
230-
progress?.Report(100.0f);
231-
return null;
232-
}
233-
234-
errorCode?.Report(fsCopyResult ? FileSystemStatusCode.Success : FileSystemStatusCode.Generic);
235-
progress?.Report(100.0f);
236-
return null;
237-
}
238-
else
239-
{
240-
errorCode?.Report(FileSystemStatusCode.Generic);
241-
return null;
242-
}
243221
}
244-
else if (source.ItemType == FilesystemItemType.File && !string.IsNullOrEmpty(source.Path) && !FtpHelpers.IsFtpPath(destination))
222+
else if (source.ItemType == FilesystemItemType.File)
245223
{
246224
var fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.CopyFileFromApp(source.Path, destination, true));
247225

248226
if (!fsResult)
249227
{
250228
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
251229

252-
FilesystemResult<BaseStorageFolder> destinationResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
230+
FilesystemResult<BaseStorageFolder> destinationResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
253231
var sourceResult = await source.ToStorageItemResult(associatedInstance);
254232
fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode;
255233

@@ -299,67 +277,8 @@ await DialogDisplayHelper.ShowDialogAsync(
299277
return null;
300278
}
301279
}
302-
else if (string.IsNullOrEmpty(source.Path) && !FtpHelpers.IsFtpPath(destination))
303-
{
304-
var fsResult = source.Item is BaseStorageFile file ? await FilesystemTasks.Wrap(async () =>
305-
await file.CopyAsync(
306-
await StorageFileExtensions.DangerousGetFolderFromPathAsync(Path.GetDirectoryName(destination)),
307-
file.Name,
308-
collision)) : new FilesystemResult<BaseStorageFile>(null, FileSystemStatusCode.Generic);
309-
310-
if (!fsResult)
311-
{
312-
errorCode?.Report(fsResult.ErrorCode);
313-
return null;
314-
}
315-
}
316-
else if (FtpHelpers.IsFtpPath(destination))
317-
{
318-
using var ftpClient = new FtpClient();
319-
ftpClient.Host = FtpHelpers.GetFtpHost(destination);
320-
ftpClient.Port = FtpHelpers.GetFtpPort(destination);
321-
ftpClient.Credentials = FtpManager.Credentials.Get(ftpClient.Host, FtpManager.Anonymous);
322-
323-
if (!await ftpClient.EnsureConnectedAsync())
324-
{
325-
errorCode?.Report(FileSystemStatusCode.Generic);
326-
return null;
327-
}
328-
329-
if (source.Item is BaseStorageFile file)
330-
{
331-
void ReportFtpPorgress(object sender, FtpProgress p)
332-
{
333-
progress?.Report((float)p.Progress);
334-
}
335-
336-
using var stream = await file.OpenStreamForReadAsync();
337-
338-
var ftpProgress = new Progress<FtpProgress>();
339-
ftpProgress.ProgressChanged += ReportFtpPorgress;
340-
341-
var result = await ftpClient.UploadAsync(stream, FtpHelpers.GetFtpPath(destination), collision switch
342-
{
343-
NameCollisionOption.ReplaceExisting => FtpRemoteExists.Overwrite,
344-
_ => FtpRemoteExists.Skip,
345-
}, false, ftpProgress, cancellationToken);
346-
347-
ftpProgress.ProgressChanged -= ReportFtpPorgress;
348-
349-
if (result != FtpStatus.Success)
350-
{
351-
errorCode?.Report(FileSystemStatusCode.Generic);
352-
return null;
353-
}
354-
}
355-
}
356-
else
357-
{
358-
errorCode?.Report(FileSystemStatusCode.Generic);
359-
return null;
360-
}
361280

362-
if (Path.GetDirectoryName(destination) == associatedInstance.FilesystemViewModel.WorkingDirectory.TrimPath())
281+
if (PathNormalization.GetParentDir(destination) == associatedInstance.FilesystemViewModel.WorkingDirectory.TrimPath())
363282
{
364283
await Windows.ApplicationModel.Core.CoreApplication.MainView.DispatcherQueue.EnqueueAsync(async () =>
365284
{
@@ -444,7 +363,7 @@ await DialogDisplayHelper.ShowDialogAsync(
444363
if (source.ItemType == FilesystemItemType.Directory)
445364
{
446365
if (!string.IsNullOrWhiteSpace(source.Path) &&
447-
Path.GetDirectoryName(destination).IsSubPathOf(source.Path)) // We check if user tried to move anything above the source.ItemPath
366+
PathNormalization.GetParentDir(destination).IsSubPathOf(source.Path)) // We check if user tried to move anything above the source.ItemPath
448367
{
449368
var destinationName = destination.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last();
450369
var sourceName = source.Path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).Last();
@@ -479,7 +398,7 @@ await DialogDisplayHelper.ShowDialogAsync(
479398
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
480399

481400
var fsSourceFolder = await source.ToStorageItemResult(associatedInstance);
482-
var fsDestinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
401+
var fsDestinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
483402
fsResult = fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode;
484403

485404
if (fsResult)
@@ -528,7 +447,7 @@ await DialogDisplayHelper.ShowDialogAsync(
528447
{
529448
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
530449

531-
FilesystemResult<BaseStorageFolder> destinationResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
450+
FilesystemResult<BaseStorageFolder> destinationResult = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
532451
var sourceResult = await source.ToStorageItemResult(associatedInstance);
533452
fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode;
534453

@@ -566,7 +485,7 @@ await DialogDisplayHelper.ShowDialogAsync(
566485
errorCode?.Report(fsResult.ErrorCode);
567486
}
568487

569-
if (Path.GetDirectoryName(destination) == associatedInstance.FilesystemViewModel.WorkingDirectory.TrimPath())
488+
if (PathNormalization.GetParentDir(destination) == associatedInstance.FilesystemViewModel.WorkingDirectory.TrimPath())
570489
{
571490
await Windows.ApplicationModel.Core.CoreApplication.MainView.DispatcherQueue.EnqueueAsync(async () =>
572491
{
@@ -844,7 +763,7 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
844763
if (source.ItemType == FilesystemItemType.Directory)
845764
{
846765
FilesystemResult<BaseStorageFolder> sourceFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(source.Path);
847-
FilesystemResult<BaseStorageFolder> destinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
766+
FilesystemResult<BaseStorageFolder> destinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
848767

849768
fsResult = sourceFolder.ErrorCode | destinationFolder.ErrorCode;
850769
errorCode?.Report(fsResult);
@@ -860,7 +779,7 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
860779
else
861780
{
862781
FilesystemResult<BaseStorageFile> sourceFile = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(source.Path);
863-
FilesystemResult<BaseStorageFolder> destinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
782+
FilesystemResult<BaseStorageFolder> destinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination));
864783

865784
fsResult = sourceFile.ErrorCode | destinationFolder.ErrorCode;
866785
errorCode?.Report(fsResult);
@@ -935,24 +854,6 @@ private async static Task<BaseStorageFolder> CloneDirectoryAsync(BaseStorageFold
935854
return createdRoot;
936855
}
937856

938-
private async static Task CloneDirectoryToFtpAsync(BaseStorageFolder sourceFolder, FtpStorageFolder destinationFolder, CreationCollisionOption collision = CreationCollisionOption.FailIfExists)
939-
{
940-
var result = await FilesystemTasks.Wrap(async () => await destinationFolder.CreateFolderAsync(sourceFolder.Name, collision));
941-
942-
if (result)
943-
{
944-
foreach (BaseStorageFile fileInSourceDir in await sourceFolder.GetFilesAsync())
945-
{
946-
await destinationFolder.UploadFileAsync(fileInSourceDir, fileInSourceDir.Name, NameCollisionOption.FailIfExists);
947-
}
948-
949-
foreach (BaseStorageFolder folderinSourceDir in await sourceFolder.GetFoldersAsync())
950-
{
951-
await CloneDirectoryToFtpAsync(folderinSourceDir, destinationFolder.CloneWithPath($"{destinationFolder.Path}/{sourceFolder.Name}"));
952-
}
953-
}
954-
}
955-
956857
private static async Task<BaseStorageFolder> MoveDirectoryAsync(BaseStorageFolder sourceFolder, BaseStorageFolder destinationDirectory, string sourceRootName, CreationCollisionOption collision = CreationCollisionOption.FailIfExists, bool deleteSource = false)
957858
{
958859
BaseStorageFolder createdRoot = await destinationDirectory.CreateFolderAsync(sourceRootName, collision);

Files/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Files.Enums;
55
using Files.Extensions;
66
using Files.Filesystem.FilesystemHistory;
7-
using Files.Filesystem.StorageItems;
87
using Files.Helpers;
98
using Files.Interacts;
109
using Files.ViewModels;
@@ -618,11 +617,11 @@ public async Task<ReturnResult> CopyItemsFromClipboard(DataPackageView packageVi
618617
{
619618
binItems ??= await recycleBinHelpers.EnumerateRecycleBin();
620619
var matchingItem = binItems.FirstOrDefault(x => x.RecyclePath == item.Path); // Get original file name
621-
destinations.Add(Path.Combine(destination, matchingItem?.FileName ?? item.Name));
620+
destinations.Add(PathNormalization.Combine(destination, matchingItem?.FileName ?? item.Name));
622621
}
623622
else
624623
{
625-
destinations.Add(Path.Combine(destination, item.Name));
624+
destinations.Add(PathNormalization.Combine(destination, item.Name));
626625
}
627626
}
628627

@@ -846,11 +845,11 @@ public async Task<ReturnResult> MoveItemsFromClipboard(DataPackageView packageVi
846845
{
847846
binItems ??= await recycleBinHelpers.EnumerateRecycleBin();
848847
var matchingItem = binItems.FirstOrDefault(x => x.RecyclePath == item.Path); // Get original file name
849-
destinations.Add(Path.Combine(destination, matchingItem?.FileName ?? item.Name));
848+
destinations.Add(PathNormalization.Combine(destination, matchingItem?.FileName ?? item.Name));
850849
}
851850
else
852851
{
853-
destinations.Add(Path.Combine(destination, item.Name));
852+
destinations.Add(PathNormalization.Combine(destination, item.Name));
854853
}
855854
}
856855

Files/Filesystem/ListedItem.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public FtpItem(FtpListItem item, string folder, string dateReturnFormat = null)
396396
ItemDateModifiedReal = item.RawModified < DateTime.FromFileTimeUtc(0) ? DateTimeOffset.MinValue : item.RawModified;
397397
ItemName = item.Name;
398398
FileExtension = Path.GetExtension(item.Name);
399-
ItemPath = Path.Combine(folder, item.Name).Replace("\\", "/");
399+
ItemPath = PathNormalization.Combine(folder, item.Name);
400400
PrimaryItemAttribute = isFile ? StorageItemTypes.File : StorageItemTypes.Folder;
401401
ItemPropertiesInitialized = false;
402402

Files/Filesystem/StorageFileHelpers/StorageFileExtensions.cs

+14-3
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ public static bool AreItemsAlreadyInFolder(this IEnumerable<IStorageItem> storag
285285

286286
public static BaseStorageFolder AsBaseStorageFolder(this IStorageItem item)
287287
{
288-
if (item.IsOfType(StorageItemTypes.Folder))
288+
if (item == null)
289+
{
290+
return null;
291+
}
292+
else if (item.IsOfType(StorageItemTypes.Folder))
289293
{
290294
if (item is StorageFolder folder)
291295
{
@@ -301,7 +305,11 @@ public static BaseStorageFolder AsBaseStorageFolder(this IStorageItem item)
301305

302306
public static BaseStorageFile AsBaseStorageFile(this IStorageItem item)
303307
{
304-
if (item.IsOfType(StorageItemTypes.File))
308+
if (item == null)
309+
{
310+
return null;
311+
}
312+
else if (item.IsOfType(StorageItemTypes.File))
305313
{
306314
if (item is StorageFile file)
307315
{
@@ -322,7 +330,10 @@ public static async Task<List<IStorageItem>> ToStandardStorageItemsAsync(this IE
322330
{
323331
try
324332
{
325-
if (item.IsOfType(StorageItemTypes.File))
333+
if (item == null)
334+
{
335+
}
336+
else if (item.IsOfType(StorageItemTypes.File))
326337
{
327338
newItems.Add(await item.AsBaseStorageFile().ToStorageFileAsync());
328339
}

0 commit comments

Comments
 (0)