From 563ca7e8e82254592a5f22eefb56a31fce45ae22 Mon Sep 17 00:00:00 2001 From: d2dyno006 <53011783+d2dyno006@users.noreply.github.com> Date: Sun, 28 Mar 2021 22:32:59 +0200 Subject: [PATCH 1/6] Improve the code for ToStorageItem() --- Files/Helpers/StorageItemHelpers.cs | 45 ++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/Files/Helpers/StorageItemHelpers.cs b/Files/Helpers/StorageItemHelpers.cs index cabd0177c849..276413a2525d 100644 --- a/Files/Helpers/StorageItemHelpers.cs +++ b/Files/Helpers/StorageItemHelpers.cs @@ -20,30 +20,55 @@ public static async Task ToStorageItem(string path, IShellPage assoc FilesystemResult file = null; FilesystemResult folder = null; - if (associatedInstance == null) + if (typeof(IStorageFile).IsAssignableFrom(typeof(TOut))) { - file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path)); - - if (!file) + if (associatedInstance == null) + { + file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path)); + } + else + { + file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path); + } + } + else if (typeof(IStorageFolder).IsAssignableFrom(typeof(TOut))) + { + if (associatedInstance == null) { folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path)); } + else + { + folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path); + } } - else + else if (typeof(IStorageItem).IsAssignableFrom(typeof(TOut))) { - file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path); + if (associatedInstance == null) + { + file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path)); - if (!file) + if (!file) + { + folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path)); + } + } + else { - folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path); + file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path); + + if (!file) + { + folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path); + } } } - if (file) + if (file != null && file) { return (TOut)(IStorageItem)file.Result; } - else if (folder) + else if (folder != null && folder) { return (TOut)(IStorageItem)folder.Result; } From 2763013bd68e5bbf705103170ff44c595561edce Mon Sep 17 00:00:00 2001 From: d2dyno006 <53011783+d2dyno006@users.noreply.github.com> Date: Sun, 28 Mar 2021 23:28:39 +0200 Subject: [PATCH 2/6] Further improvements --- Files/Helpers/StorageItemHelpers.cs | 78 +++++++++++++++++++---------- 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/Files/Helpers/StorageItemHelpers.cs b/Files/Helpers/StorageItemHelpers.cs index 276413a2525d..64cbb06daed1 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,46 +22,44 @@ public static async Task ToStorageItem(string path, IShellPage assoc FilesystemResult file = null; FilesystemResult folder = null; - if (typeof(IStorageFile).IsAssignableFrom(typeof(TOut))) + if (path.EndsWith(".lnk") || path.EndsWith(".url")) { - if (associatedInstance == null) + // 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 + + if (false) // Prevent unnecessary exceptions { - file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path)); - } - else - { - file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path); + Debugger.Break(); + throw new ArgumentException("Function ToStorageItem() does not support converting from .lnk and .url files"); } } + + if (typeof(IStorageFile).IsAssignableFrom(typeof(TOut))) + { + await GetFile(); + } else if (typeof(IStorageFolder).IsAssignableFrom(typeof(TOut))) { - if (associatedInstance == null) - { - folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path)); - } - else - { - folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path); - } + await GetFolder(); } else if (typeof(IStorageItem).IsAssignableFrom(typeof(TOut))) { - if (associatedInstance == null) - { - file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path)); + bool fileChecked = false; - if (!file) - { - folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path)); - } + if (System.IO.Path.HasExtension(path)) // Probably a file + { + await GetFile(); + fileChecked = true; } - else + else // Possibly a folder { - file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path); + await GetFolder(); - if (!file) + if (!folder && !fileChecked && !file) { - folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path); + // It wasn't a folder, so check file then because it wasn't checked + await GetFile(); } } } @@ -74,6 +74,32 @@ public static async Task ToStorageItem(string path, IShellPage assoc } 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) From 3a8ef95b0b6b3ca5f4ae613ee0c7647f74c404f2 Mon Sep 17 00:00:00 2001 From: d2dyno1 <53011783+d2dyno1@users.noreply.github.com> Date: Sun, 28 Mar 2021 23:35:01 +0200 Subject: [PATCH 3/6] Update StorageItemHelpers.cs --- Files/Helpers/StorageItemHelpers.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Files/Helpers/StorageItemHelpers.cs b/Files/Helpers/StorageItemHelpers.cs index 64cbb06daed1..7413e4c76492 100644 --- a/Files/Helpers/StorageItemHelpers.cs +++ b/Files/Helpers/StorageItemHelpers.cs @@ -27,6 +27,8 @@ public static async Task ToStorageItem(string path, IShellPage assoc // 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 (false) // Prevent unnecessary exceptions { @@ -165,4 +167,4 @@ public static FilesystemResult ToType(FilesystemResult result) where return new FilesystemResult(result.Result as T, result.ErrorCode); } } -} \ No newline at end of file +} From cde911464506bdc9f0131e37a8de8d7503e07e3d Mon Sep 17 00:00:00 2001 From: d2dyno1 <53011783+d2dyno1@users.noreply.github.com> Date: Sun, 28 Mar 2021 23:47:51 +0200 Subject: [PATCH 4/6] Update StorageItemHelpers.cs --- Files/Helpers/StorageItemHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Files/Helpers/StorageItemHelpers.cs b/Files/Helpers/StorageItemHelpers.cs index 7413e4c76492..4b3fd62b7915 100644 --- a/Files/Helpers/StorageItemHelpers.cs +++ b/Files/Helpers/StorageItemHelpers.cs @@ -58,7 +58,7 @@ public static async Task ToStorageItem(string path, IShellPage assoc { await GetFolder(); - if (!folder && !fileChecked && !file) + if (!folder && !fileChecked) { // It wasn't a folder, so check file then because it wasn't checked await GetFile(); From 75212204f8b035771a9f7106b3c89b992daa1fac Mon Sep 17 00:00:00 2001 From: d2dyno1 <53011783+d2dyno1@users.noreply.github.com> Date: Sun, 28 Mar 2021 23:52:12 +0200 Subject: [PATCH 5/6] Remove unnecessary logic --- Files/Helpers/StorageItemHelpers.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Files/Helpers/StorageItemHelpers.cs b/Files/Helpers/StorageItemHelpers.cs index 4b3fd62b7915..06fb025bcdc4 100644 --- a/Files/Helpers/StorageItemHelpers.cs +++ b/Files/Helpers/StorageItemHelpers.cs @@ -47,18 +47,15 @@ public static async Task ToStorageItem(string path, IShellPage assoc } else if (typeof(IStorageItem).IsAssignableFrom(typeof(TOut))) { - bool fileChecked = false; - if (System.IO.Path.HasExtension(path)) // Probably a file { await GetFile(); - fileChecked = true; } else // Possibly a folder { await GetFolder(); - if (!folder && !fileChecked) + if (!folder) { // It wasn't a folder, so check file then because it wasn't checked await GetFile(); From 8b456137c7f41c6a46ccf071117230b55fd75010 Mon Sep 17 00:00:00 2001 From: d2dyno006 <53011783+d2dyno006@users.noreply.github.com> Date: Tue, 30 Mar 2021 16:30:13 +0200 Subject: [PATCH 6/6] Added ToLower() --- Files/Helpers/StorageItemHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Files/Helpers/StorageItemHelpers.cs b/Files/Helpers/StorageItemHelpers.cs index 06fb025bcdc4..33bf4db2900d 100644 --- a/Files/Helpers/StorageItemHelpers.cs +++ b/Files/Helpers/StorageItemHelpers.cs @@ -22,7 +22,7 @@ public static async Task ToStorageItem(string path, IShellPage assoc FilesystemResult file = null; FilesystemResult folder = null; - if (path.EndsWith(".lnk") || path.EndsWith(".url")) + if (path.ToLower().EndsWith(".lnk") || path.ToLower().EndsWith(".url")) { // TODO: In the future, when IStorageItemWithPath will inherit from IStorageItem, // we could implement this code here for getting .lnk files