Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
149 changes: 149 additions & 0 deletions QuickLook.Common/Helpers/PathHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright © 2017-2026 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;

namespace QuickLook.Common.Helpers;

public static class PathHelper
{
private static readonly char[] TrimmableExtensionChars = ['"', ' ', '\t', '\r', '\n'];

public static string GetExtension(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;

var normalized = Normalize(path);

// Path.GetExtension validates the whole path through Path.CheckInvalidPathChars
// and can throw for shell-provided paths. Parse UNC paths directly so they
// always reach the preview pipeline.
if (IsUncPath(normalized))
return GetExtensionFallback(normalized);

try
{
return Path.GetExtension(normalized);
}
catch (ArgumentException)
{
return GetExtensionFallback(normalized);
}
}

public static string GetFileName(string path)
{
if (string.IsNullOrEmpty(path))
return path;

var normalized = Normalize(path);

try
{
return Path.GetFileName(normalized);
}
catch (ArgumentException)
{
var separator = normalized.LastIndexOfAny([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]);
return separator < 0 ? normalized : normalized.Substring(separator + 1);
}
}

public static string GetPathRoot(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;

var normalized = Normalize(path);

try
{
return Path.GetPathRoot(normalized);
}
catch (ArgumentException)
{
if (normalized.StartsWith(@"\\", StringComparison.Ordinal))
{
var parts = normalized.Substring(2).Split([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar], 3);
return parts.Length switch
{
0 => string.Empty,
1 => @"\\" + parts[0],
_ => @"\\" + parts[0] + @"\" + parts[1],
};
}

if (normalized.Length >= 2 && normalized[1] == Path.VolumeSeparatorChar)
return normalized.Substring(0, 2) + Path.DirectorySeparatorChar;

return string.Empty;
}
}

public static bool IsShortcut(string path)
{
return GetExtension(path).Equals(".lnk", StringComparison.OrdinalIgnoreCase);
}

public static bool IsUncPath(string path)
{
if (string.IsNullOrEmpty(path))
return false;

var normalized = Normalize(path);
return normalized.Length >= 2 &&
normalized[0] == Path.DirectorySeparatorChar &&
normalized[1] == Path.DirectorySeparatorChar;
}

private static string Normalize(string path)
{
path = path.Trim();

if (path.Length >= 2 && path[0] == '"' && path[path.Length - 1] == '"')
path = path.Substring(1, path.Length - 2).Trim();

return path;
}

private static string GetExtensionFallback(string path)
{
var end = path.Length - 1;

while (end >= 0 && char.IsWhiteSpace(path[end]))
end--;

for (var i = end; i >= 0; i--)
{
var c = path[i];

if (c == '.')
return i == end
? string.Empty
: path.Substring(i, end - i + 1).TrimEnd(TrimmableExtensionChars);

if (c == Path.DirectorySeparatorChar ||
c == Path.AltDirectorySeparatorChar ||
c == Path.VolumeSeparatorChar)
break;
}

return string.Empty;
}
}
96 changes: 96 additions & 0 deletions QuickLook.Tests/PathHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright © 2017-2026 QL-Win Contributors
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickLook.Common.Helpers;

namespace QuickLook.Tests;

[TestClass]
public sealed class PathHelperTests
{
[TestMethod]
[DataRow(@"\\server\share\file.png", ".png")]
[DataRow(@"\\server.domain\share\folder\image.JPG", ".JPG")]
[DataRow(@"\\10.1.2.3\share\file.pdf", ".pdf")]
[DataRow(@"\\wsl$\Ubuntu\home\user\file.png", ".png")]
[DataRow(@"\\wsl.localhost\Ubuntu\home\user\file.png", ".png")]
[DataRow(@"\\server\share\folder", "")]
[DataRow(@"\\wsl$\Ubuntu\home\user", "")]
[DataRow(@"C:\local\folder\file.png", ".png")]
[DataRow(@"Z:\mapped\folder\file.png", ".png")]
[DataRow(@"C:\local\folder\file", "")]
[DataRow("", "")]
public void GetExtensionHandlesUncAndLocalPaths(string path, string expected)
{
Assert.AreEqual(expected, PathHelper.GetExtension(path));
}

[TestMethod]
[DataRow(@"\\server\share\file.png" + "\n")]
[DataRow(@"\\server\share\bad""name.png")]
[DataRow(@"""\\server\share\file.png""")]
public void GetExtensionDoesNotThrowOnMalformedUncPaths(string path)
{
Assert.AreEqual(".png", PathHelper.GetExtension(path));
}

[TestMethod]
[DataRow(@"\\server\share\file.lnk", true)]
[DataRow(@"\\server\share\file.LNK", true)]
[DataRow(@"\\wsl$\Ubuntu\home\file.png", false)]
[DataRow(@"C:\local\folder\file.lnk", true)]
[DataRow(@"C:\local\folder\file.png", false)]
[DataRow(@"\\wsl$\Ubuntu\home", false)]
public void IsShortcutHandlesUncAndLocalPaths(string path, bool expected)
{
Assert.AreEqual(expected, PathHelper.IsShortcut(path));
}

[TestMethod]
[DataRow(@"\\server\share\file.png", true)]
[DataRow(@"\\wsl$\Ubuntu\home\file.png", true)]
[DataRow(@"""\\server\share\file.png""", true)]
[DataRow(@"C:\local\folder\file.png", false)]
[DataRow(@"Z:\mapped\file.png", false)]
[DataRow(@"\server\share\file.png", false)]
[DataRow("", false)]
public void IsUncPathHandlesUncAndLocalPaths(string path, bool expected)
{
Assert.AreEqual(expected, PathHelper.IsUncPath(path));
}

[TestMethod]
[DataRow(@"\\server\share\folder\file.png", "file.png")]
[DataRow(@"\\wsl$\Ubuntu\home\user\file.png", "file.png")]
[DataRow(@"C:\local\folder\file.png", "file.png")]
[DataRow(@"\\server\share\bad""name.png", "bad\"name.png")]
public void GetFileNameHandlesUncAndMalformedPaths(string path, string expected)
{
Assert.AreEqual(expected, PathHelper.GetFileName(path));
}

[TestMethod]
[DataRow(@"\\server\share\folder\file.png", @"\\server\share")]
[DataRow(@"\\wsl$\Ubuntu\home\user\file.png", @"\\wsl$\Ubuntu")]
[DataRow(@"C:\local\folder\file.png", @"C:\")]
[DataRow(@"Z:\mapped\file.png", @"Z:\")]
public void GetPathRootHandlesUncAndLocalPaths(string path, string expected)
{
Assert.AreEqual(expected, PathHelper.GetPathRoot(path));
}
}
20 changes: 20 additions & 0 deletions QuickLook.Tests/QuickLook.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.4" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\QuickLook.Common\Helpers\PathHelper.cs" Link="PathHelper.cs" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion QuickLook/Helpers/ExtensionFilterHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static bool IsExtensionAllowed(string path)
if (string.IsNullOrEmpty(path))
return true;

var extension = Path.GetExtension(path);
var extension = PathHelper.GetExtension(path);

// Files without extensions are always allowed (includes directories)
if (string.IsNullOrEmpty(extension))
Expand Down
22 changes: 16 additions & 6 deletions QuickLook/NativeMethods/QuickLook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using QuickLook.Common.Helpers;
using System;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -133,14 +134,23 @@ private static string ResolveShortcut(string path)
{
if (string.IsNullOrEmpty(path)) return path;

if (Path.GetExtension(path).ToLower() != ".lnk") return path;
if (!PathHelper.IsShortcut(path)) return path;

var link = new ShellLink();
((IPersistFile)link).Load(path, 0);
var sb = new StringBuilder(MaxPath);
((IShellLinkW)link).GetPath(sb, sb.Capacity, out _, 0);
try
{
var link = new ShellLink();
((IPersistFile)link).Load(path, 0);
var sb = new StringBuilder(MaxPath);
((IShellLinkW)link).GetPath(sb, sb.Capacity, out _, 0);

return sb.Length == 0 ? path : sb.ToString();
return sb.Length == 0 ? path : sb.ToString();
}
catch (Exception e)
{
// An unreadable shortcut must not break the whole preview.
Debug.WriteLine(e);
return path;
}
}

internal enum FocusedWindowType
Expand Down
Loading