Skip to content

Commit

Permalink
Improved the code and speed of ToStorageItem<TOut>() (#4346)
Browse files Browse the repository at this point in the history
  • Loading branch information
d2dyno1 authored Mar 30, 2021
1 parent 7f96c49 commit 2f14f59
Showing 1 changed file with 62 additions and 12 deletions.
74 changes: 62 additions & 12 deletions Files/Helpers/StorageItemHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Files.Enums;
using Files.Filesystem;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Storage;

Expand All @@ -20,35 +22,83 @@ public static async Task<TOut> ToStorageItem<TOut>(string path, IShellPage assoc
FilesystemResult<StorageFile> file = null;
FilesystemResult<StorageFolder> folder = null;

if (associatedInstance == null)
if (path.ToLower().EndsWith(".lnk") || path.ToLower().EndsWith(".url"))
{
file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path));
// TODO: In the future, when IStorageItemWithPath will inherit from IStorageItem,
// we could implement this code here for getting .lnk files
// for now, we can't

return default(TOut);

if (!file)
if (false) // Prevent unnecessary exceptions
{
folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path));
Debugger.Break();
throw new ArgumentException("Function ToStorageItem<TOut>() does not support converting from .lnk and .url files");
}
}
else
{
file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path);

if (!file)
if (typeof(IStorageFile).IsAssignableFrom(typeof(TOut)))
{
await GetFile();
}
else if (typeof(IStorageFolder).IsAssignableFrom(typeof(TOut)))
{
await GetFolder();
}
else if (typeof(IStorageItem).IsAssignableFrom(typeof(TOut)))
{
if (System.IO.Path.HasExtension(path)) // Probably a file
{
folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path);
await GetFile();
}
else // Possibly a folder
{
await GetFolder();

if (!folder)
{
// It wasn't a folder, so check file then because it wasn't checked
await GetFile();
}
}
}

if (file)
if (file != null && file)
{
return (TOut)(IStorageItem)file.Result;
}
else if (folder)
else if (folder != null && folder)
{
return (TOut)(IStorageItem)folder.Result;
}

return default(TOut);

// Extensions

async Task GetFile()
{
if (associatedInstance == null)
{
file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path));
}
else
{
file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path);
}
}

async Task GetFolder()
{
if (associatedInstance == null)
{
folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path));
}
else
{
folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path);
}
}
}

public static async Task<FilesystemResult<IStorageItem>> ToStorageItemResult(this IStorageItemWithPath item, IShellPage associatedInstance = null)
Expand Down Expand Up @@ -114,4 +164,4 @@ public static FilesystemResult<T> ToType<T, V>(FilesystemResult<V> result) where
return new FilesystemResult<T>(result.Result as T, result.ErrorCode);
}
}
}
}

0 comments on commit 2f14f59

Please sign in to comment.