mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-07-08 19:40:01 +02:00
Compare commits
1 Commits
issue/3551
...
issue/2802
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4c858288f |
80
src/modules/peek/Peek.Common/Helpers/SymlinkResolver.cs
Normal file
80
src/modules/peek/Peek.Common/Helpers/SymlinkResolver.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
// PeekSymlinkResolver.cs
|
||||
// Fix for Issue #28028: Peek can't view PDF/HTML soft links
|
||||
// This helper resolves symbolic links to their target paths
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Peek.Common.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves symbolic links and junction points to their target paths.
|
||||
/// </summary>
|
||||
public static class SymlinkResolver
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves a path to its final target if it's a symbolic link or junction.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to resolve.</param>
|
||||
/// <returns>The resolved target path, or the original path if not a link.</returns>
|
||||
public static string ResolveSymlink(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var fileInfo = new FileInfo(path);
|
||||
|
||||
// Check if it's a symbolic link
|
||||
if (fileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint))
|
||||
{
|
||||
// Get the target of the symbolic link
|
||||
var target = fileInfo.LinkTarget;
|
||||
if (!string.IsNullOrEmpty(target))
|
||||
{
|
||||
// If target is relative, make it absolute
|
||||
if (!Path.IsPathRooted(target))
|
||||
{
|
||||
var directory = Path.GetDirectoryName(path);
|
||||
target = Path.GetFullPath(Path.Combine(directory ?? string.Empty, target));
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// If resolution fails, return the original path
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a path is a symbolic link or junction point.
|
||||
/// </summary>
|
||||
public static bool IsSymlink(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || !File.Exists(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var attributes = File.GetAttributes(path);
|
||||
return attributes.HasFlag(FileAttributes.ReparsePoint);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
// PlaintextPreviewSettings.cs
|
||||
// Fix for Issue #35516: Add user-configurable support for plaintext files
|
||||
// Allows users to define which extensions are treated as plaintext
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Peek.Common.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Settings for plaintext file preview in Peek.
|
||||
/// </summary>
|
||||
public class PlaintextPreviewSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Default extensions always treated as plaintext.
|
||||
/// </summary>
|
||||
public static readonly IReadOnlyList<string> DefaultExtensions = new[]
|
||||
{
|
||||
".txt", ".md", ".log", ".ini", ".cfg", ".conf", ".config",
|
||||
".json", ".xml", ".yaml", ".yml", ".toml",
|
||||
".sh", ".bash", ".zsh", ".ps1", ".psm1", ".psd1",
|
||||
".bat", ".cmd",
|
||||
".gitignore", ".gitattributes", ".editorconfig",
|
||||
".env", ".properties"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// User-defined additional extensions to preview as plaintext.
|
||||
/// </summary>
|
||||
[JsonPropertyName("additionalExtensions")]
|
||||
public List<string> AdditionalExtensions { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Maximum file size in bytes to preview (default 5MB).
|
||||
/// </summary>
|
||||
[JsonPropertyName("maxFileSizeBytes")]
|
||||
public long MaxFileSizeBytes { get; set; } = 5 * 1024 * 1024;
|
||||
|
||||
/// <summary>
|
||||
/// Whether to enable syntax highlighting.
|
||||
/// </summary>
|
||||
[JsonPropertyName("enableSyntaxHighlighting")]
|
||||
public bool EnableSyntaxHighlighting { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Checks if an extension should be previewed as plaintext.
|
||||
/// </summary>
|
||||
public bool ShouldPreviewAsPlaintext(string extension)
|
||||
{
|
||||
if (string.IsNullOrEmpty(extension))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var ext = extension.StartsWith(".") ? extension : "." + extension;
|
||||
|
||||
return DefaultExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase)
|
||||
|| AdditionalExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user