diff --git a/Files/Helpers/StorageItemHelpers.cs b/Files/Helpers/StorageItemHelpers.cs index cabd0177c849..33bf4db2900d 100644 --- a/Files/Helpers/StorageItemHelpers.cs +++ b/Files/Helpers/StorageItemHelpers.cs @@ -1,5 +1,7 @@ using Files.Enums; using Files.Filesystem; +using System; +using System.Diagnostics; using System.Threading.Tasks; using Windows.Storage; @@ -20,35 +22,83 @@ public static async Task ToStorageItem(string path, IShellPage assoc FilesystemResult file = null; FilesystemResult 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() 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> ToStorageItemResult(this IStorageItemWithPath item, IShellPage associatedInstance = null) @@ -114,4 +164,4 @@ public static FilesystemResult ToType(FilesystemResult result) where return new FilesystemResult(result.Result as T, result.ErrorCode); } } -} \ No newline at end of file +}