mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-11 09:49:07 +00:00
Refactor thumbnail extraction
This commit is contained in:
@@ -18,21 +18,19 @@
|
|||||||
using PureSharpCompress.Archives.Zip;
|
using PureSharpCompress.Archives.Zip;
|
||||||
using PureSharpCompress.Common;
|
using PureSharpCompress.Common;
|
||||||
using PureSharpCompress.Readers;
|
using PureSharpCompress.Readers;
|
||||||
|
using QuickLook.Common.Helpers;
|
||||||
using QuickLook.Common.Plugin;
|
using QuickLook.Common.Plugin;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
using System.Windows.Resources;
|
|
||||||
|
|
||||||
namespace QuickLook.Plugin.ThumbnailViewer;
|
namespace QuickLook.Plugin.ThumbnailViewer;
|
||||||
|
|
||||||
internal static class Handler
|
internal static class Handler
|
||||||
{
|
{
|
||||||
public static void Prepare(string path, ContextObject context)
|
public static void Prepare(string path, ContextObject context)
|
||||||
{
|
|
||||||
if (path.EndsWith(".xd", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -40,105 +38,22 @@ internal static class Handler
|
|||||||
BitmapImage bitmap = imageData.ReadAsBitmapImage();
|
BitmapImage bitmap = imageData.ReadAsBitmapImage();
|
||||||
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
|
context.SetPreferredSizeFit(new Size(bitmap.PixelWidth, bitmap.PixelHeight), 0.8d);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_ = e;
|
Debug.WriteLine($"Error reading thumbnail from {path}: {ex.Message}");
|
||||||
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;
|
|
||||||
context.PreferredSize = new Size { Width = 800, Height = 600 };
|
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)
|
public static Stream ViewImage(string path)
|
||||||
{
|
{
|
||||||
if (path.EndsWith(".xd", StringComparison.OrdinalIgnoreCase))
|
try
|
||||||
{
|
{
|
||||||
using ZipArchive archive = ZipArchive.Open(path, new());
|
using ZipArchive archive = ZipArchive.Open(path, new());
|
||||||
using IReader reader = archive.ExtractAllEntries();
|
using IReader reader = archive.ExtractAllEntries();
|
||||||
|
|
||||||
|
if (path.EndsWith(".xd", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
while (reader.MoveToNextEntry())
|
while (reader.MoveToNextEntry())
|
||||||
{
|
{
|
||||||
if (reader.Entry.Key!.Equals("preview.png", StringComparison.OrdinalIgnoreCase))
|
if (reader.Entry.Key!.Equals("preview.png", StringComparison.OrdinalIgnoreCase))
|
||||||
@@ -148,7 +63,7 @@ internal static class Handler
|
|||||||
stream.CopyTo(ms);
|
stream.CopyTo(ms);
|
||||||
return 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();
|
MemoryStream ms = new();
|
||||||
using EntryStream stream = reader.OpenEntryStream();
|
using EntryStream stream = reader.OpenEntryStream();
|
||||||
@@ -159,11 +74,6 @@ internal static class Handler
|
|||||||
}
|
}
|
||||||
else if (path.EndsWith(".fig", StringComparison.OrdinalIgnoreCase))
|
else if (path.EndsWith(".fig", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
using ZipArchive archive = ZipArchive.Open(path, new());
|
|
||||||
using IReader reader = archive.ExtractAllEntries();
|
|
||||||
|
|
||||||
while (reader.MoveToNextEntry())
|
while (reader.MoveToNextEntry())
|
||||||
{
|
{
|
||||||
if (reader.Entry.Key!.Equals("thumbnail.png", StringComparison.OrdinalIgnoreCase))
|
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))
|
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())
|
while (reader.MoveToNextEntry())
|
||||||
{
|
{
|
||||||
if (reader.Entry.Key!.EndsWith(".thumb.png", StringComparison.OrdinalIgnoreCase))
|
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))
|
else if (path.EndsWith(".sketch", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
using ZipArchive archive = ZipArchive.Open(path, new());
|
|
||||||
using IReader reader = archive.ExtractAllEntries();
|
|
||||||
|
|
||||||
while (reader.MoveToNextEntry())
|
while (reader.MoveToNextEntry())
|
||||||
{
|
{
|
||||||
if (reader.Entry.Key!.EndsWith("previews/preview.png", StringComparison.OrdinalIgnoreCase))
|
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))
|
else if (path.EndsWith(".xmind", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
using ZipArchive archive = ZipArchive.Open(path, new());
|
|
||||||
using IReader reader = archive.ExtractAllEntries();
|
|
||||||
|
|
||||||
while (reader.MoveToNextEntry())
|
while (reader.MoveToNextEntry())
|
||||||
{
|
{
|
||||||
if (reader.Entry.Key!.Equals("Thumbnails/thumbnail.png", StringComparison.OrdinalIgnoreCase))
|
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))
|
else if (path.EndsWith(".kra", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
using ZipArchive archive = ZipArchive.Open(path, new());
|
|
||||||
using IReader reader = archive.ExtractAllEntries();
|
|
||||||
|
|
||||||
while (reader.MoveToNextEntry())
|
while (reader.MoveToNextEntry())
|
||||||
{
|
{
|
||||||
Debug.WriteLine(reader.Entry.Key);
|
|
||||||
|
|
||||||
if (reader.Entry.Key!.Contains("mergedimage"))
|
if (reader.Entry.Key!.Contains("mergedimage"))
|
||||||
{
|
{
|
||||||
MemoryStream ms = new();
|
MemoryStream ms = new();
|
||||||
@@ -261,9 +139,6 @@ internal static class Handler
|
|||||||
}
|
}
|
||||||
else if (path.EndsWith(".cdr", StringComparison.OrdinalIgnoreCase))
|
else if (path.EndsWith(".cdr", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
using ZipArchive archive = ZipArchive.Open(path, new());
|
|
||||||
using IReader reader = archive.ExtractAllEntries();
|
|
||||||
|
|
||||||
while (reader.MoveToNextEntry())
|
while (reader.MoveToNextEntry())
|
||||||
{
|
{
|
||||||
if (reader.Entry.Key!.Equals("previews/thumbnail.png", StringComparison.OrdinalIgnoreCase))
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -64,6 +64,7 @@ public class Plugin : IViewer
|
|||||||
ContextObject = context,
|
ContextObject = context,
|
||||||
SaveAsVisibility = Visibility.Visible,
|
SaveAsVisibility = Visibility.Visible,
|
||||||
ReverseColorVisibility = Visibility.Visible,
|
ReverseColorVisibility = Visibility.Visible,
|
||||||
|
MetaIconVisibility = Visibility.Collapsed,
|
||||||
};
|
};
|
||||||
|
|
||||||
_ = Task.Run(() =>
|
_ = Task.Run(() =>
|
||||||
|
@@ -51,10 +51,6 @@
|
|||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Resource Include="Resources\*.png" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="PureSharpCompress" Version="0.40.0">
|
<PackageReference Include="PureSharpCompress" Version="0.40.0">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 2.3 KiB |
Reference in New Issue
Block a user