Improve PDF magic detection

This commit is contained in:
ema
2025-06-22 21:12:35 +08:00
parent f0d55455e4
commit 0168318f39
2 changed files with 9 additions and 8 deletions

View File

@@ -26,8 +26,7 @@ namespace QuickLook.Plugin.PDFViewer;
internal class AsyncPageToThumbnailConverter : IMultiValueConverter internal class AsyncPageToThumbnailConverter : IMultiValueConverter
{ {
private static readonly BitmapImage Loading = private static readonly BitmapImage Loading =
new BitmapImage( new(new Uri("pack://application:,,,/QuickLook.Plugin.PdfViewer;component/Resources/loading.png"));
new Uri("pack://application:,,,/QuickLook.Plugin.PdfViewer;component/Resources/loading.png"));
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{ {

View File

@@ -22,7 +22,6 @@ using System;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Threading; using System.Windows.Threading;
@@ -46,13 +45,16 @@ public class Plugin : IViewer
if (Directory.Exists(path)) if (Directory.Exists(path))
return false; return false;
if (File.Exists(path) && Path.GetExtension(path).ToLower() == ".pdf") if (File.Exists(path) && Path.GetExtension(path).EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
return true; return true;
using (var br = new BinaryReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))) using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
{ byte[] buffer = new byte[4];
return Encoding.ASCII.GetString(br.ReadBytes(4)) == "%PDF"; if (fs.Read(buffer, 0, 4) < 4) return false;
} return buffer[0] == (byte)'%' &&
buffer[1] == (byte)'P' &&
buffer[2] == (byte)'D' &&
buffer[3] == (byte)'F';
} }
public void Prepare(string path, ContextObject context) public void Prepare(string path, ContextObject context)