Skip to content

Commit 2f14f59

Browse files
authored
Improved the code and speed of ToStorageItem<TOut>() (#4346)
1 parent 7f96c49 commit 2f14f59

File tree

1 file changed

+62
-12
lines changed

1 file changed

+62
-12
lines changed

Files/Helpers/StorageItemHelpers.cs

+62-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Files.Enums;
22
using Files.Filesystem;
3+
using System;
4+
using System.Diagnostics;
35
using System.Threading.Tasks;
46
using Windows.Storage;
57

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

23-
if (associatedInstance == null)
25+
if (path.ToLower().EndsWith(".lnk") || path.ToLower().EndsWith(".url"))
2426
{
25-
file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path));
27+
// TODO: In the future, when IStorageItemWithPath will inherit from IStorageItem,
28+
// we could implement this code here for getting .lnk files
29+
// for now, we can't
30+
31+
return default(TOut);
2632

27-
if (!file)
33+
if (false) // Prevent unnecessary exceptions
2834
{
29-
folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path));
35+
Debugger.Break();
36+
throw new ArgumentException("Function ToStorageItem<TOut>() does not support converting from .lnk and .url files");
3037
}
3138
}
32-
else
33-
{
34-
file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path);
3539

36-
if (!file)
40+
if (typeof(IStorageFile).IsAssignableFrom(typeof(TOut)))
41+
{
42+
await GetFile();
43+
}
44+
else if (typeof(IStorageFolder).IsAssignableFrom(typeof(TOut)))
45+
{
46+
await GetFolder();
47+
}
48+
else if (typeof(IStorageItem).IsAssignableFrom(typeof(TOut)))
49+
{
50+
if (System.IO.Path.HasExtension(path)) // Probably a file
3751
{
38-
folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path);
52+
await GetFile();
53+
}
54+
else // Possibly a folder
55+
{
56+
await GetFolder();
57+
58+
if (!folder)
59+
{
60+
// It wasn't a folder, so check file then because it wasn't checked
61+
await GetFile();
62+
}
3963
}
4064
}
4165

42-
if (file)
66+
if (file != null && file)
4367
{
4468
return (TOut)(IStorageItem)file.Result;
4569
}
46-
else if (folder)
70+
else if (folder != null && folder)
4771
{
4872
return (TOut)(IStorageItem)folder.Result;
4973
}
5074

5175
return default(TOut);
76+
77+
// Extensions
78+
79+
async Task GetFile()
80+
{
81+
if (associatedInstance == null)
82+
{
83+
file = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFileFromPathAsync(path));
84+
}
85+
else
86+
{
87+
file = await associatedInstance?.FilesystemViewModel?.GetFileFromPathAsync(path);
88+
}
89+
}
90+
91+
async Task GetFolder()
92+
{
93+
if (associatedInstance == null)
94+
{
95+
folder = await FilesystemTasks.Wrap(() => StorageFileExtensions.DangerousGetFolderFromPathAsync(path));
96+
}
97+
else
98+
{
99+
folder = await associatedInstance?.FilesystemViewModel?.GetFolderFromPathAsync(path);
100+
}
101+
}
52102
}
53103

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

0 commit comments

Comments
 (0)