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,13 +18,13 @@
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;
@@ -32,138 +32,48 @@ internal static class Handler
{
public static void Prepare(string path, ContextObject context)
{
if (path.EndsWith(".xd", StringComparison.OrdinalIgnoreCase))
try
{
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 };
}
using Stream imageData = ViewImage(path);
BitmapImage bitmap = imageData.ReadAsBitmapImage();
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
}
else if (path.EndsWith(".fig", StringComparison.OrdinalIgnoreCase))
catch (Exception ex)
{
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;
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 };
}
Debug.WriteLine($"Error reading thumbnail from {path}: {ex.Message}");
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();
while (reader.MoveToNextEntry())
if (path.EndsWith(".xd", StringComparison.OrdinalIgnoreCase))
{
if (reader.Entry.Key!.Equals("preview.png", StringComparison.OrdinalIgnoreCase))
while (reader.MoveToNextEntry())
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
if (reader.Entry.Key!.Equals("thumbnail.png", StringComparison.OrdinalIgnoreCase))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
if (reader.Entry.Key!.Equals("preview.png", StringComparison.OrdinalIgnoreCase))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
else if (reader.Entry.Key!.Equals("thumbnail.png", StringComparison.OrdinalIgnoreCase))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
}
}
}
else if (path.EndsWith(".fig", StringComparison.OrdinalIgnoreCase))
{
try
else if (path.EndsWith(".fig", StringComparison.OrdinalIgnoreCase))
{
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
else if (path.EndsWith(".pip", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".pix", StringComparison.OrdinalIgnoreCase))
{
///
}
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,79 +98,63 @@ internal static class Handler
}
}
}
catch
else if (path.EndsWith(".sketch", StringComparison.OrdinalIgnoreCase))
{
///
}
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))
while (reader.MoveToNextEntry())
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
if (reader.Entry.Key!.EndsWith("previews/preview.png", StringComparison.OrdinalIgnoreCase))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
}
}
else if (path.EndsWith(".xmind", StringComparison.OrdinalIgnoreCase))
{
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.Equals("Thumbnails/thumbnail.png", StringComparison.OrdinalIgnoreCase))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
}
}
else if (path.EndsWith(".kra", StringComparison.OrdinalIgnoreCase))
{
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.Contains("mergedimage"))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
}
}
else if (path.EndsWith(".cdr", StringComparison.OrdinalIgnoreCase))
{
while (reader.MoveToNextEntry())
{
if (reader.Entry.Key!.Equals("previews/thumbnail.png", StringComparison.OrdinalIgnoreCase))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
}
}
}
else if (path.EndsWith(".xmind", StringComparison.OrdinalIgnoreCase))
catch (Exception ex)
{
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))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
}
}
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();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
}
}
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))
{
MemoryStream ms = new();
using EntryStream stream = reader.OpenEntryStream();
stream.CopyTo(ms);
return ms;
}
}
Debug.WriteLine($"Error reading thumbnail from {path}: {ex.Message}");
ProcessHelper.WriteLog(ex.ToString());
}
return null;