Skip to content
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

Codebase quality: Use helper method for checking .lnk and .url extensions #10772

Merged
merged 4 commits into from
Dec 19, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
IEnumerable<ShellFileItem> nameMatchItems, items = await recycleBinHelpers.EnumerateRecycleBin();

// Get name matching files
if (Path.GetExtension(source.Path) == ".lnk" || Path.GetExtension(source.Path) == ".url") // We need to check if it is a shortcut file
if (FileExtensionHelpers.IsShortcutOrUrlFile(source.Path)) // We need to check if it is a shortcut file
{
nameMatchItems = items.Where((item) => item.FilePath == Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileNameWithoutExtension(source.Path)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ CancellationToken cancellationToken
{
if (file is ShortcutStorageFile linkFile)
{
var isUrl = linkFile.Name.EndsWith(".url", StringComparison.OrdinalIgnoreCase);
var isUrl = FileExtensionHelpers.IsWebLinkFile(linkFile.Name);
return new ShortcutItem(file.FolderRelativeId)
{
PrimaryItemAttribute = StorageItemTypes.File,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ CancellationToken cancellationToken
IsSymLink = true
};
}
else if (findData.cFileName.EndsWith(".lnk", StringComparison.Ordinal) || findData.cFileName.EndsWith(".url", StringComparison.Ordinal))
else if (FileExtensionHelpers.IsShortcutOrUrlFile(findData.cFileName))
{
var isUrl = findData.cFileName.EndsWith(".url", StringComparison.OrdinalIgnoreCase);
var isUrl = FileExtensionHelpers.IsWebLinkFile(findData.cFileName);
var shInfo = await ParseLinkAsync(itemPath);
if (shInfo is null)
{
Expand Down Expand Up @@ -393,12 +393,12 @@ private async static Task<ShellLinkItem> ParseLinkAsync(string linkPath)
{
try
{
if (linkPath.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase))
if (FileExtensionHelpers.IsShortcutFile(linkPath))
{
using var link = new ShellLink(linkPath, LinkResolution.NoUIWithMsgPump, default, TimeSpan.FromMilliseconds(100));
return ShellFolderExtensions.GetShellLinkItem(link);
}
else if (linkPath.EndsWith(".url", StringComparison.OrdinalIgnoreCase))
else if (FileExtensionHelpers.IsWebLinkFile(linkPath))
{
var linkUrl = await Win32API.StartSTATask(() =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Filesystem/StorageItems/NativeStorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class NativeStorageFile : BaseStorageFile
public override string FileType => IO.Path.GetExtension(Name);
public override string FolderRelativeId => $"0\\{Name}";

public bool IsShortcut => FileType.Equals(".lnk", StringComparison.OrdinalIgnoreCase) || FileType.Equals(".url", StringComparison.OrdinalIgnoreCase);
public bool IsShortcut => FileExtensionHelpers.IsShortcutOrUrlFile(FileType);
public bool IsAlternateStream => System.Text.RegularExpressions.Regex.IsMatch(Path, @"\w:\w");

public override string DisplayType
Expand Down Expand Up @@ -164,7 +164,7 @@ private static bool CheckAccess(string path)

private static bool IsNativePath(string path)
{
var isShortcut = path.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".url", StringComparison.OrdinalIgnoreCase);
var isShortcut = FileExtensionHelpers.IsShortcutOrUrlFile(path);
var isAlternateStream = System.Text.RegularExpressions.Regex.IsMatch(path, @"\w:\w");
return isShortcut || isAlternateStream;
}
Expand Down
27 changes: 18 additions & 9 deletions src/Files.App/Helpers/FileExtensionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static bool IsImageFile(string? fileExtensionToCheck)
public static bool IsPowerShellFile(string fileExtensionToCheck)
{
return !string.IsNullOrEmpty(fileExtensionToCheck) &&
fileExtensionToCheck.Equals(".ps1", StringComparison.OrdinalIgnoreCase);
fileExtensionToCheck.Equals(".ps1", StringComparison.OrdinalIgnoreCase);
}

/// <summary>
Expand Down Expand Up @@ -65,7 +65,7 @@ public static bool IsBrowsableZipFile(string? filePath, out string? ext)
public static bool IsInfFile(string? fileExtensionToCheck)
{
return !string.IsNullOrWhiteSpace(fileExtensionToCheck) &&
fileExtensionToCheck.Equals(".inf", StringComparison.OrdinalIgnoreCase);
fileExtensionToCheck.Equals(".inf", StringComparison.OrdinalIgnoreCase);
}

/// <summary>
Expand All @@ -87,16 +87,25 @@ public static bool IsFontFile(string? fileExtensionToCheck)
}

/// <summary>
/// Check if the file extension is a shortcut file.
/// Check if the file path is a shortcut file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is a shortcute file;
/// <param name="filePathToCheck">The file path to check.</param>
/// <returns><c>true</c> if the filePathToCheck is a shortcut file;
/// otherwise <c>false</c>.</returns>
/// <remarks>Font file type is .lnkf</remarks>
public static bool IsShortcutFile(string? fileExtensionToCheck)
/// <remarks>Shortcut file type is .lnk</remarks>
public static bool IsShortcutFile(string? filePathToCheck)
{
return !string.IsNullOrWhiteSpace(fileExtensionToCheck) &&
fileExtensionToCheck.Equals(".lnk", StringComparison.OrdinalIgnoreCase);
return !string.IsNullOrWhiteSpace(filePathToCheck) &&
filePathToCheck.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase);
}

public static bool IsWebLinkFile(string? filePathToCheck)
{
return !string.IsNullOrWhiteSpace(filePathToCheck) &&
filePathToCheck.EndsWith(".url", StringComparison.OrdinalIgnoreCase);
}

public static bool IsShortcutOrUrlFile(string? filePathToCheck)
=> IsShortcutFile(filePathToCheck) || IsWebLinkFile(filePathToCheck);
}
}
8 changes: 4 additions & 4 deletions src/Files.App/Helpers/FileOperationsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,12 @@ public static void TryCancelOperation(string operationId)
{
try
{
if (linkPath.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase))
if (FileExtensionHelpers.IsShortcutFile(linkPath))
{
using var link = new ShellLink(linkPath, LinkResolution.NoUIWithMsgPump, default, TimeSpan.FromMilliseconds(100));
return Task.FromResult((string.Empty, ShellFolderExtensions.GetShellLinkItem(link)));
}
else if (linkPath.EndsWith(".url", StringComparison.OrdinalIgnoreCase))
else if (FileExtensionHelpers.IsWebLinkFile(linkPath))
{
return Win32API.StartSTATask(() =>
{
Expand All @@ -579,14 +579,14 @@ public static Task<bool> CreateOrUpdateLinkAsync(string linkSavePath, string tar
{
try
{
if (linkSavePath.EndsWith(".lnk", StringComparison.Ordinal))
if (FileExtensionHelpers.IsShortcutFile(linkSavePath))
{
using var newLink = new ShellLink(targetPath, arguments, workingDirectory);
newLink.RunAsAdministrator = runAsAdmin;
newLink.SaveAs(linkSavePath); // Overwrite if exists
return Task.FromResult(true);
}
else if (linkSavePath.EndsWith(".url", StringComparison.Ordinal))
else if (FileExtensionHelpers.IsWebLinkFile(linkSavePath))
{
return Win32API.StartSTATask(() =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Helpers/FileThumbnailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static async Task<byte[]> LoadIconFromStorageItemAsync(IStorageItem item,

public static async Task<byte[]> LoadIconFromPathAsync(string filePath, uint thumbnailSize, ThumbnailMode thumbnailMode, bool isFolder = false)
{
if (!filePath.EndsWith(".lnk", StringComparison.Ordinal) && !filePath.EndsWith(".url", StringComparison.Ordinal))
if (!FileExtensionHelpers.IsShortcutOrUrlFile(filePath))
{
var item = await StorageHelpers.ToStorageItem<IStorageItem>(filePath);
if (item is not null)
Expand Down
13 changes: 7 additions & 6 deletions src/Files.App/Helpers/NavigationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static async Task<bool> OpenPath(string path, IShellPage associatedInstan
bool isHiddenItem = NativeFileOperationsHelper.HasFileAttribute(path, System.IO.FileAttributes.Hidden);
bool isDirectory = NativeFileOperationsHelper.HasFileAttribute(path, System.IO.FileAttributes.Directory);
bool isReparsePoint = NativeFileOperationsHelper.HasFileAttribute(path, System.IO.FileAttributes.ReparsePoint);
bool isShortcut = path.EndsWith(".lnk", StringComparison.Ordinal) || path.EndsWith(".url", StringComparison.Ordinal);
bool isShortcut = FileExtensionHelpers.IsShortcutOrUrlFile(path);
FilesystemResult opened = (FilesystemResult)false;

var shortcutInfo = new ShellLinkItem();
Expand Down Expand Up @@ -248,9 +248,9 @@ private static async Task<FilesystemResult> OpenDirectory(string path, IShellPag

var opened = (FilesystemResult)false;
bool isHiddenItem = NativeFileOperationsHelper.HasFileAttribute(path, System.IO.FileAttributes.Hidden);
bool IsShortcut = path.EndsWith(".lnk", StringComparison.Ordinal) || path.EndsWith(".url", StringComparison.Ordinal); // Determine
bool isShortcut = FileExtensionHelpers.IsShortcutOrUrlFile(path);

if (IsShortcut)
if (isShortcut)
{
if (string.IsNullOrEmpty(shortcutInfo.TargetPath))
{
Expand Down Expand Up @@ -339,16 +339,17 @@ private static async Task<FilesystemResult> OpenFile(string path, IShellPage ass
{
var opened = (FilesystemResult)false;
bool isHiddenItem = NativeFileOperationsHelper.HasFileAttribute(path, System.IO.FileAttributes.Hidden);
bool IsShortcut = path.EndsWith(".lnk", StringComparison.Ordinal) || path.EndsWith(".url", StringComparison.Ordinal) || !string.IsNullOrEmpty(shortcutInfo.TargetPath);
if (IsShortcut)
bool isShortcut = FileExtensionHelpers.IsShortcutOrUrlFile(path) || !string.IsNullOrEmpty(shortcutInfo.TargetPath);

if (isShortcut)
{
if (string.IsNullOrEmpty(shortcutInfo.TargetPath))
{
await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args);
}
else
{
if (!path.EndsWith(".url", StringComparison.Ordinal))
if (!FileExtensionHelpers.IsWebLinkFile(path))
{
StorageFileWithPath childFile = await associatedInstance.FilesystemViewModel.GetFileWithPathFromPathAsync(shortcutInfo.TargetPath);
if (childFile is not null)
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Helpers/StorageHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static async Task<TRequested> ToStorageItem<TRequested>(string path) wher
FilesystemResult<BaseStorageFile> file = null;
FilesystemResult<BaseStorageFolder> folder = null;

if (path.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".url", StringComparison.OrdinalIgnoreCase))
if (FileExtensionHelpers.IsShortcutOrUrlFile(path))
{
// TODO: In the future, when IStorageItemWithPath will inherit from IStorageItem,
// we could implement this code here for getting .lnk files
Expand Down
7 changes: 4 additions & 3 deletions src/Files.App/Shell/Win32Shell.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Files.Shared;
using Files.App.Helpers;
using Files.Shared;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -97,14 +98,14 @@ public static (bool HasRecycleBin, long NumItems, long BinSize) QueryRecycleBin(

try
{
if (filePath.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase))
if (FileExtensionHelpers.IsShortcutFile(filePath))
{
using var link = new ShellLink(filePath, LinkResolution.NoUIWithMsgPump, default, TimeSpan.FromMilliseconds(100));
targetPath = link.TargetPath;
return ShellFolderExtensions.GetShellLinkItem(link);
}

if (filePath.EndsWith(".url", StringComparison.OrdinalIgnoreCase))
if (FileExtensionHelpers.IsWebLinkFile(filePath))
{
targetPath = await Win32API.StartSTATask(() =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/ViewModels/Properties/FileProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override void GetBaseProperties()
ViewModel.ShortcutItemWorkingDirVisibility = Item.IsLinkItem || shortcutItem.IsSymLink ? false : true;
ViewModel.ShortcutItemArguments = shortcutItem.Arguments;
ViewModel.ShortcutItemArgumentsVisibility = Item.IsLinkItem || shortcutItem.IsSymLink ? false : true;
ViewModel.IsSelectedItemShortcut = ".lnk".Equals(Item.FileExtension, StringComparison.OrdinalIgnoreCase);
ViewModel.IsSelectedItemShortcut = FileExtensionHelpers.IsShortcutFile(Item.FileExtension);
ViewModel.ShortcutItemOpenLinkCommand = new RelayCommand(async () =>
{
if (Item.IsLinkItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ private async Task Drop(DragEventArgs e)

IStorageItem item = await StorageHelpers.ToStorageItem<IStorageItem>(itemPath);

if (item is not null || (itemPath.EndsWith(".lnk", StringComparison.Ordinal) || itemPath.EndsWith(".url", StringComparison.Ordinal)))
if (item is not null || FileExtensionHelpers.IsShortcutOrUrlFile(itemPath))
{
if (await AddItemFromPath(itemPath,
itemPath.EndsWith(".lnk", StringComparison.Ordinal) || itemPath.EndsWith(".url", StringComparison.Ordinal) ? FilesystemItemType.File : (item.IsOfType(StorageItemTypes.Folder) ? FilesystemItemType.Directory : FilesystemItemType.File)))
FileExtensionHelpers.IsShortcutOrUrlFile(itemPath) ? FilesystemItemType.File : (item.IsOfType(StorageItemTypes.Folder) ? FilesystemItemType.Directory : FilesystemItemType.File)))
{
itemsAdded = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public string Name
fileName = System.IO.Path.GetFileName(this.Path);
}

if (fileName.EndsWith(".lnk", StringComparison.Ordinal) || fileName.EndsWith(".url", StringComparison.Ordinal))
if (FileExtensionHelpers.IsShortcutOrUrlFile(fileName))
{
fileName = fileName.Remove(fileName.Length - 4);
}
Expand Down