Refactor thumbnail extraction

This commit is contained in:
ema
2025-07-10 06:14:42 +08:00
parent 2ee54695e7
commit d60b10a8cf
4 changed files with 79 additions and 201 deletions

View File

@@ -18,21 +18,19 @@
using PureSharpCompress.Archives.Zip;
using PureSharpCompress.Common;
using PureSharpCompress.Readers;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace QuickLook.Plugin.ThumbnailViewer;
internal static class Handler
{
public static void Prepare(string path, ContextObject context)
{
if (path.EndsWith(".xd", StringComparison.OrdinalIgnoreCase))
{
try
{
@@ -40,105 +38,22 @@ internal static class Handler
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
}
catch (Exception e)
catch (Exception ex)
{
_ = e;
context.PreferredSize = new Size { Width = 1200, Height = 900 };
}
}
else if (path.EndsWith(".fig", StringComparison.OrdinalIgnoreCase))
{
try
{
using Stream imageData = ViewImage(path);
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.PreferredSize = new Size { Width = bitmap.PixelWidth * 1.4d, Height = bitmap.PixelHeight * 1.8d };
}
catch (Exception e)
{
_ = e;
context.PreferredSize = new Size { Width = 100, Height = 100 };
}
}
else if (path.EndsWith(".pip", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".pix", StringComparison.OrdinalIgnoreCase))
{
try
{
using Stream imageData = ViewImage(path);
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
}
catch (Exception e)
{
_ = e;
context.PreferredSize = new Size { Width = 100, Height = 100 };
}
}
else if (path.EndsWith(".sketch", StringComparison.OrdinalIgnoreCase))
{
try
{
using Stream imageData = ViewImage(path);
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
}
catch (Exception e)
{
_ = e;
context.PreferredSize = new Size { Width = 100, Height = 100 };
}
}
else if (path.EndsWith(".xmind", StringComparison.OrdinalIgnoreCase))
{
try
{
using Stream imageData = ViewImage(path);
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
}
catch (Exception e)
{
_ = e;
context.PreferredSize = new Size { Width = 1200, Height = 900 };
}
}
else if (path.EndsWith(".kra", StringComparison.OrdinalIgnoreCase))
{
try
{
using Stream imageData = ViewImage(path);
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
}
catch (Exception e)
{
_ = e;
Debug.WriteLine($"Error reading thumbnail from {path}: {ex.Message}");
context.PreferredSize = new Size { Width = 800, Height = 600 };
}
}
else if (path.EndsWith(".cdr", StringComparison.OrdinalIgnoreCase))
{
try
{
using Stream imageData = ViewImage(path);
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
}
catch (Exception e)
{
_ = e;
context.PreferredSize = new Size { Width = 800, Height = 600 };
}
}
}
public static Stream ViewImage(string path)
{
if (path.EndsWith(".xd", StringComparison.OrdinalIgnoreCase))
try
{
using ZipArchive archive = ZipArchive.Open(path, new());
using IReader reader = archive.ExtractAllEntries();
if (path.EndsWith(".xd", StringComparison.OrdinalIgnoreCase))
{
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.Equals("preview.png", StringComparison.OrdinalIgnoreCase))
@@ -148,7 +63,7 @@ internal static class Handler
stream.CopyTo(ms);
return ms;
}
if (reader.Entry.Key!.Equals("thumbnail.png", StringComparison.OrdinalIgnoreCase))
else if (reader.Entry.Key!.Equals("thumbnail.png", StringComparison.OrdinalIgnoreCase))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
@@ -159,11 +74,6 @@ internal static class Handler
}
else if (path.EndsWith(".fig", StringComparison.OrdinalIgnoreCase))
{
try
{
using ZipArchive archive = ZipArchive.Open(path, new());
using IReader reader = archive.ExtractAllEntries();
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.Equals("thumbnail.png", StringComparison.OrdinalIgnoreCase))
@@ -175,21 +85,8 @@ internal static class Handler
}
}
}
catch
{
///
}
StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/QuickLook.Plugin.ThumbnailViewer;component/Resources/broken.png"));
return info?.Stream;
}
else if (path.EndsWith(".pip", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".pix", StringComparison.OrdinalIgnoreCase))
{
try
{
using ZipArchive archive = ZipArchive.Open(path, new());
using IReader reader = archive.ExtractAllEntries();
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.EndsWith(".thumb.png", StringComparison.OrdinalIgnoreCase))
@@ -201,19 +98,8 @@ internal static class Handler
}
}
}
catch
{
///
}
StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/QuickLook.Plugin.ThumbnailViewer;component/Resources/broken.png"));
return info?.Stream;
}
else if (path.EndsWith(".sketch", StringComparison.OrdinalIgnoreCase))
{
using ZipArchive archive = ZipArchive.Open(path, new());
using IReader reader = archive.ExtractAllEntries();
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.EndsWith("previews/preview.png", StringComparison.OrdinalIgnoreCase))
@@ -227,9 +113,6 @@ internal static class Handler
}
else if (path.EndsWith(".xmind", StringComparison.OrdinalIgnoreCase))
{
using ZipArchive archive = ZipArchive.Open(path, new());
using IReader reader = archive.ExtractAllEntries();
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.Equals("Thumbnails/thumbnail.png", StringComparison.OrdinalIgnoreCase))
@@ -243,13 +126,8 @@ internal static class Handler
}
else if (path.EndsWith(".kra", StringComparison.OrdinalIgnoreCase))
{
using ZipArchive archive = ZipArchive.Open(path, new());
using IReader reader = archive.ExtractAllEntries();
while (reader.MoveToNextEntry())
{
Debug.WriteLine(reader.Entry.Key);
if (reader.Entry.Key!.Contains("mergedimage"))
{
MemoryStream ms = new();
@@ -261,9 +139,6 @@ internal static class Handler
}
else if (path.EndsWith(".cdr", StringComparison.OrdinalIgnoreCase))
{
using ZipArchive archive = ZipArchive.Open(path, new());
using IReader reader = archive.ExtractAllEntries();
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.Equals("previews/thumbnail.png", StringComparison.OrdinalIgnoreCase))
@@ -275,6 +150,12 @@ internal static class Handler
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error reading thumbnail from {path}: {ex.Message}");
ProcessHelper.WriteLog(ex.ToString());
}
return null;
}

View File

@@ -64,6 +64,7 @@ public class Plugin : IViewer
ContextObject = context,
SaveAsVisibility = Visibility.Visible,
ReverseColorVisibility = Visibility.Visible,
MetaIconVisibility = Visibility.Collapsed,
};
_ = Task.Run(() =>

View File

@@ -51,10 +51,6 @@
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Resource Include="Resources\*.png" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="PureSharpCompress" Version="0.40.0">
<PrivateAssets>all</PrivateAssets>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB