Skip to content

Fixed invalid items from being added to bundles #7119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Files/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ protected async void FileList_DragItemsStarting(object sender, DragItemsStarting

if (result.ErrorCode == FileSystemStatusCode.Unauthorized)
{
var itemList = e.Items.OfType<ListedItem>().Select(x => StorageItemHelpers.FromPathAndType(
var itemList = e.Items.OfType<ListedItem>().Select(x => StorageHelpers.FromPathAndType(
x.ItemPath, x.PrimaryItemAttribute == StorageItemTypes.File ? FilesystemItemType.File : FilesystemItemType.Directory));
e.Data.Properties["FileDrop"] = itemList.ToList();
return;
Expand Down
4 changes: 2 additions & 2 deletions Files/Files.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@
<Compile Include="Filesystem\StorageHistory\StorageHistoryWrapper.cs" />
<Compile Include="Helpers\AcrylicTheme.cs" />
<Compile Include="Helpers\Convert\ErrorCodeConverter.cs" />
<Compile Include="Helpers\StorageItemHelpers.cs" />
<Compile Include="Helpers\StorageHelpers.cs" />
<Compile Include="Helpers\DialogDisplayHelper.cs" />
<Compile Include="Helpers\DispatcherHelper.cs" />
<Compile Include="ViewModels\Properties\FilePropertySection.cs" />
Expand Down Expand Up @@ -1549,4 +1549,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
16 changes: 8 additions & 8 deletions Files/Filesystem/FilesystemOperations/FilesystemOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public FilesystemOperations(IShellPage associatedInstance)
if (fsAdminResult)
{
fsResult = fsAdminResult;
item = StorageItemHelpers.FromPathAndType(shellOpRes.Items.SingleOrDefault()?.Destination, FilesystemItemType.File);
item = StorageHelpers.FromPathAndType(shellOpRes.Items.SingleOrDefault()?.Destination, FilesystemItemType.File);
}
}
}
Expand All @@ -108,7 +108,7 @@ public FilesystemOperations(IShellPage associatedInstance)
if (fsAdminResult && shellOpRes.Items.Count == 1)
{
fsResult = fsAdminResult;
item = StorageItemHelpers.FromPathAndType(shellOpRes.Items.Single().Destination, FilesystemItemType.File);
item = StorageHelpers.FromPathAndType(shellOpRes.Items.Single().Destination, FilesystemItemType.File);
}
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public FilesystemOperations(IShellPage associatedInstance)
if (fsAdminResult && shellOpRes.Items.Count == 1)
{
fsResult = fsAdminResult;
item = StorageItemHelpers.FromPathAndType(shellOpRes.Items.Single().Destination, FilesystemItemType.Directory);
item = StorageHelpers.FromPathAndType(shellOpRes.Items.Single().Destination, FilesystemItemType.Directory);
}
}
break;
Expand Down Expand Up @@ -665,7 +665,7 @@ await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
// Get newest file
ShellFileItem item = nameMatchItems.Where((item) => item.RecycleDate != null).OrderBy((item) => item.RecycleDate).FirstOrDefault();

return new StorageHistory(FileOperationType.Recycle, source, StorageItemHelpers.FromPathAndType(item?.RecyclePath, source.ItemType));
return new StorageHistory(FileOperationType.Recycle, source, StorageHelpers.FromPathAndType(item?.RecyclePath, source.ItemType));
}

return new StorageHistory(FileOperationType.Delete, source, null);
Expand All @@ -683,7 +683,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItem source,
IProgress<FileSystemStatusCode> errorCode,
CancellationToken cancellationToken)
{
return await RenameAsync(StorageItemHelpers.FromStorageItem(source), newName, collision, errorCode, cancellationToken);
return await RenameAsync(StorageHelpers.FromStorageItem(source), newName, collision, errorCode, cancellationToken);
}

public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source,
Expand Down Expand Up @@ -728,7 +728,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source,
if (NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination))
{
errorCode?.Report(FileSystemStatusCode.Success);
return new StorageHistory(FileOperationType.Rename, source, StorageItemHelpers.FromPathAndType(destination, source.ItemType));
return new StorageHistory(FileOperationType.Rename, source, StorageHelpers.FromPathAndType(destination, source.ItemType));
}
else
{
Expand All @@ -743,7 +743,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source,
if (fsResult)
{
errorCode?.Report(FileSystemStatusCode.Success);
return new StorageHistory(FileOperationType.Rename, source, StorageItemHelpers.FromPathAndType(destination, source.ItemType));
return new StorageHistory(FileOperationType.Rename, source, StorageHelpers.FromPathAndType(destination, source.ItemType));
}
}
}
Expand Down Expand Up @@ -873,7 +873,7 @@ await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
}
}

return new StorageHistory(FileOperationType.Restore, source, StorageItemHelpers.FromPathAndType(destination, source.ItemType));
return new StorageHistory(FileOperationType.Restore, source, StorageHelpers.FromPathAndType(destination, source.ItemType));
}

#endregion IFilesystemOperations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ public async Task<ReturnResult> RecycleItemsFromClipboard(DataPackageView packag
}
collisions.AddIfNotPresent(incomingItems.ElementAt(i).SourcePath, FileNameConflictResolveOptionType.GenerateNewName);

if (destination.Count() > 0 && StorageItemHelpers.Exists(destination.ElementAt(i))) // Same item names in both directories
if (destination.Count() > 0 && StorageHelpers.Exists(destination.ElementAt(i))) // Same item names in both directories
{
conflictingItems.Add(incomingItems.ElementAt(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public async Task<IStorageHistory> CopyItemsAsync(IEnumerable<IStorageItemWithPa
if (copiedSources.Any())
{
return new StorageHistory(FileOperationType.Copy, copiedSources.Select(x => sourceRename.Single(s => s.Path == x.Source)),
copiedSources.Select(item => StorageItemHelpers.FromPathAndType(item.Destination, sourceRename.Single(s => s.Path == item.Source).ItemType)));
copiedSources.Select(item => StorageHelpers.FromPathAndType(item.Destination, sourceRename.Single(s => s.Path == item.Source).ItemType)));
}
return null; // Cannot undo overwrite operation
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public async Task<IStorageHistory> CreateShortcutItemsAsync(IEnumerable<IStorage
if (success)
{
createdSources.Add(items.ElementAt(i).src);
createdDestination.Add(StorageItemHelpers.FromPathAndType(items.ElementAt(i).dest, FilesystemItemType.File));
createdDestination.Add(StorageHelpers.FromPathAndType(items.ElementAt(i).dest, FilesystemItemType.File));
}
progress?.Report(i / (float)source.Count() * 100.0f);
}
Expand Down Expand Up @@ -275,7 +275,7 @@ public async Task<IStorageHistory> DeleteItemsAsync(IEnumerable<IStorageItemWith
if (recycledSources.Any())
{
return new StorageHistory(FileOperationType.Recycle, recycledSources.Select(x => source.Single(s => s.Path == x.Source)),
recycledSources.Select(item => StorageItemHelpers.FromPathAndType(item.Destination, source.Single(s => s.Path == item.Source).ItemType)));
recycledSources.Select(item => StorageHelpers.FromPathAndType(item.Destination, source.Single(s => s.Path == item.Source).ItemType)));
}
return new StorageHistory(FileOperationType.Delete, source, null);
}
Expand Down Expand Up @@ -385,7 +385,7 @@ public async Task<IStorageHistory> MoveItemsAsync(IEnumerable<IStorageItemWithPa
if (movedSources.Any())
{
return new StorageHistory(FileOperationType.Move, movedSources.Select(x => sourceRename.Single(s => s.Path == x.Source)),
movedSources.Select(item => StorageItemHelpers.FromPathAndType(item.Destination, sourceRename.Single(s => s.Path == item.Source).ItemType)));
movedSources.Select(item => StorageHelpers.FromPathAndType(item.Destination, sourceRename.Single(s => s.Path == item.Source).ItemType)));
}
return null; // Cannot undo overwrite operation
}
Expand All @@ -403,7 +403,7 @@ public async Task<IStorageHistory> MoveItemsAsync(IEnumerable<IStorageItemWithPa

public async Task<IStorageHistory> RenameAsync(IStorageItem source, string newName, NameCollisionOption collision, IProgress<FileSystemStatusCode> errorCode, CancellationToken cancellationToken)
{
return await RenameAsync(StorageItemHelpers.FromStorageItem(source), newName, collision, errorCode, cancellationToken);
return await RenameAsync(StorageHelpers.FromStorageItem(source), newName, collision, errorCode, cancellationToken);
}

public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source, string newName, NameCollisionOption collision, IProgress<FileSystemStatusCode> errorCode, CancellationToken cancellationToken)
Expand Down Expand Up @@ -439,7 +439,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source, stri
if (renamedSources.Any())
{
return new StorageHistory(FileOperationType.Rename, source,
StorageItemHelpers.FromPathAndType(renamedSources.Single().Destination, source.ItemType));
StorageHelpers.FromPathAndType(renamedSources.Single().Destination, source.ItemType));
}
return null; // Cannot undo overwrite operation
}
Expand Down Expand Up @@ -496,11 +496,11 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
if (movedSources.Any())
{
// Recycle bin also stores a file starting with $I for each item
await DeleteItemsAsync(movedSources.Select(src => StorageItemHelpers.FromPathAndType(
await DeleteItemsAsync(movedSources.Select(src => StorageHelpers.FromPathAndType(
Path.Combine(Path.GetDirectoryName(src.Source), Path.GetFileName(src.Source).Replace("$R", "$I", StringComparison.Ordinal)),
new[] { source }.Single(s => s.Path == src.Source).ItemType)), null, null, true, cancellationToken);
return new StorageHistory(FileOperationType.Restore, source,
StorageItemHelpers.FromPathAndType(movedSources.Single().Destination, source.ItemType));
StorageHelpers.FromPathAndType(movedSources.Single().Destination, source.ItemType));
}
return null; // Cannot undo overwrite operation
}
Expand Down
2 changes: 1 addition & 1 deletion Files/Filesystem/StorageFileHelpers/FilesystemResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public FilesystemResult(FileSystemStatusCode errorCode)
public static implicit operator FilesystemResult(FileSystemStatusCode res) => new FilesystemResult(res);

public static implicit operator bool(FilesystemResult res) =>
res.ErrorCode == FileSystemStatusCode.Success;
res != null && res.ErrorCode == FileSystemStatusCode.Success;

public static explicit operator FilesystemResult(bool res) =>
new FilesystemResult(res ? FileSystemStatusCode.Success : FileSystemStatusCode.Generic);
Expand Down
2 changes: 1 addition & 1 deletion Files/Helpers/FileThumbnailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static async Task<byte[]> LoadIconFromPathAsync(string filePath, uint thu
{
if (!filePath.EndsWith(".lnk", StringComparison.Ordinal) && !filePath.EndsWith(".url", StringComparison.Ordinal))
{
var item = await StorageItemHelpers.ToStorageItem<IStorageItem>(filePath);
var item = await StorageHelpers.ToStorageItem<IStorageItem>(filePath);
if (item != null)
{
var iconData = await LoadIconFromStorageItemAsync(item, thumbnailSize, thumbnailMode);
Expand Down
2 changes: 1 addition & 1 deletion Files/Helpers/NavigationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public static async Task<bool> OpenPath(string path, IShellPage associatedInstan
}
else
{
itemType = await StorageItemHelpers.GetTypeFromPath(path);
itemType = await StorageHelpers.GetTypeFromPath(path);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Files.Filesystem;
using Files.Filesystem.StorageItems;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
Expand All @@ -11,58 +13,95 @@ namespace Files.Helpers
/// <summary>
/// <see cref="IStorageItem"/> related Helpers
/// </summary>
public static class StorageItemHelpers
public static class StorageHelpers
{
public static async Task<IStorageItem> ToStorageItem(this IStorageItemWithPath item, IShellPage associatedInstance = null)
{
return (await item.ToStorageItemResult(associatedInstance)).Result;
}

public static async Task<TOut> ToStorageItem<TOut>(string path, IShellPage associatedInstance = null) where TOut : IStorageItem
public static async Task<TRequested> ToStorageItem<TRequested>(string path, IShellPage associatedInstance = null) where TRequested : IStorageItem
{
FilesystemResult<BaseStorageFile> file = null;
FilesystemResult<BaseStorageFolder> folder = null;

if (path.ToLowerInvariant().EndsWith(".lnk", StringComparison.Ordinal) || path.ToLowerInvariant().EndsWith(".url", StringComparison.Ordinal))
if (path.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".url", StringComparison.OrdinalIgnoreCase))
{
// 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;
}
else if (typeof(IStorageFile).IsAssignableFrom(typeof(TOut)))
{
await GetFile();
}
else if (typeof(IStorageFolder).IsAssignableFrom(typeof(TOut)))

// Fast get attributes
bool exists = NativeFileOperationsHelper.GetFileAttributesExFromApp(path, NativeFileOperationsHelper.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, out NativeFileOperationsHelper.WIN32_FILE_ATTRIBUTE_DATA itemAttributes);
if (exists) // Exists on local storage
{
await GetFolder();
// Directory
if (itemAttributes.dwFileAttributes.HasFlag(System.IO.FileAttributes.Directory))
{
if (typeof(IStorageFile).IsAssignableFrom(typeof(TRequested))) // Wanted file
{
// NotAFile
return default;
}
else // Just get the directory
{
await GetFolder();
}
}
else // File
{
if (typeof(IStorageFolder).IsAssignableFrom(typeof(TRequested))) // Wanted directory
{
// NotAFile
return default;
}
else // Just get the file
{
await GetFile();
}
}
}
else if (typeof(IStorageItem).IsAssignableFrom(typeof(TOut)))
else // Does not exist or is not present on local storage
{
if (System.IO.Path.HasExtension(path)) // Probably a file
Debug.WriteLine($"Path does not exist. Trying to find storage item manually (HRESULT: {Marshal.GetLastWin32Error()})");

if (typeof(IStorageFile).IsAssignableFrom(typeof(TRequested)))
{
await GetFile();
}
else // Possibly a folder
else if (typeof(IStorageFolder).IsAssignableFrom(typeof(TRequested)))
{
await GetFolder();

if (!folder)
}
else if (typeof(IStorageItem).IsAssignableFrom(typeof(TRequested)))
{
if (System.IO.Path.HasExtension(path)) // Possibly a file
{
// It wasn't a folder, so check file then because it wasn't checked
await GetFile();
}

if (!file || file.Result == null) // Possibly a folder
{
await GetFolder();

if (file == null && (!folder || folder.Result == null))
{
// Try file because it wasn't checked
await GetFile();
}
}
}
}

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

return default;
Expand Down
Loading