[Peek] Support for archives (#26839)

* support for archives in peek

* fix spellcheck

* horizontal scrolling

* fix height

* removed redundant helper
This commit is contained in:
Davide Giacometti
2023-06-28 09:38:53 +02:00
committed by GitHub
parent 67ce81ded8
commit 6ba8596d52
19 changed files with 735 additions and 3 deletions

View File

@@ -0,0 +1,216 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Dispatching;
using Peek.Common.Extensions;
using Peek.Common.Helpers;
using Peek.Common.Models;
using Peek.FilePreviewer.Models;
using Peek.FilePreviewer.Previewers.Archives.Helpers;
using Peek.FilePreviewer.Previewers.Archives.Models;
using Peek.FilePreviewer.Previewers.Interfaces;
using SharpCompress.Archives;
using SharpCompress.Common;
using SharpCompress.Readers;
using Windows.ApplicationModel.Resources;
namespace Peek.FilePreviewer.Previewers.Archives
{
public partial class ArchivePreviewer : ObservableObject, IArchivePreviewer
{
private readonly IconCache _iconCache = new();
private int _directoryCount;
private int _fileCount;
private ulong _size;
private ulong _extractedSize;
[ObservableProperty]
private PreviewState state;
[ObservableProperty]
private string? _directoryCountText;
[ObservableProperty]
private string? _fileCountText;
[ObservableProperty]
private string? _sizeText;
private IFileSystemItem Item { get; }
private DispatcherQueue Dispatcher { get; }
public ObservableCollection<ArchiveItem> Tree { get; }
public ArchivePreviewer(IFileSystemItem file)
{
Item = file;
Dispatcher = DispatcherQueue.GetForCurrentThread();
Tree = new ObservableCollection<ArchiveItem>();
}
public async Task CopyAsync()
{
await Dispatcher.RunOnUiThread(async () =>
{
var storageItem = await Item.GetStorageItemAsync();
ClipboardHelper.SaveToClipboard(storageItem);
});
}
public Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
{
return Task.FromResult(new PreviewSize { MonitorSize = null });
}
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
{
State = PreviewState.Loading;
using var stream = File.OpenRead(Item.Path);
if (Item.Path.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase) || Item.Path.EndsWith(".tgz", StringComparison.OrdinalIgnoreCase))
{
using var archive = ArchiveFactory.Open(stream);
_extractedSize = (ulong)archive.TotalUncompressSize;
stream.Seek(0, SeekOrigin.Begin);
using var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
cancellationToken.ThrowIfCancellationRequested();
await AddEntryAsync(reader.Entry, cancellationToken);
}
}
else
{
using var archive = ArchiveFactory.Open(stream);
_extractedSize = (ulong)archive.TotalUncompressSize;
foreach (var entry in archive.Entries)
{
cancellationToken.ThrowIfCancellationRequested();
await AddEntryAsync(entry, cancellationToken);
}
}
_size = (ulong)new FileInfo(Item.Path).Length; // archive.TotalSize isn't accurate
DirectoryCountText = string.Format(CultureInfo.CurrentCulture, ResourceLoader.GetForViewIndependentUse().GetString("Archive_Directory_Count"), _directoryCount);
FileCountText = string.Format(CultureInfo.CurrentCulture, ResourceLoader.GetForViewIndependentUse().GetString("Archive_File_Count"), _fileCount);
SizeText = string.Format(CultureInfo.CurrentCulture, ResourceLoader.GetForViewIndependentUse().GetString("Archive_Size"), ReadableStringHelper.BytesToReadableString(_size), ReadableStringHelper.BytesToReadableString(_extractedSize));
State = PreviewState.Loaded;
}
public static bool IsFileTypeSupported(string fileExt)
{
return _supportedFileTypes.Contains(fileExt);
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
private async Task AddEntryAsync(IEntry entry, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(entry, nameof(entry));
var levels = entry!.Key
.Split('/', '\\')
.Where(l => !string.IsNullOrWhiteSpace(l))
.ToArray();
ArchiveItem? parent = null;
for (var i = 0; i < levels.Length; i++)
{
var type = (!entry.IsDirectory && i == levels.Length - 1) ? ArchiveItemType.File : ArchiveItemType.Directory;
var icon = type == ArchiveItemType.Directory
? await _iconCache.GetDirectoryIconAsync(cancellationToken)
: await _iconCache.GetFileExtIconAsync(entry.Key, cancellationToken);
var item = new ArchiveItem(levels[i], type, icon);
if (type == ArchiveItemType.Directory)
{
item.IsExpanded = parent == null; // Only the root level is expanded
}
else if (type == ArchiveItemType.File)
{
item.Size = (ulong)entry.Size;
}
if (parent == null)
{
var existing = Tree.FirstOrDefault(e => e.Name == item.Name);
if (existing == null)
{
var index = GetIndex(Tree, item);
Tree.Insert(index, item);
CountItem(item);
}
parent = existing ?? Tree.First(e => e.Name == item.Name);
}
else
{
var existing = parent.Children.FirstOrDefault(e => e.Name == item.Name);
if (existing == null)
{
var index = GetIndex(parent.Children, item);
parent.Children.Insert(index, item);
CountItem(item);
}
parent = existing ?? parent.Children.First(e => e.Name == item.Name);
}
}
}
private int GetIndex(ObservableCollection<ArchiveItem> collection, ArchiveItem item)
{
for (var i = 0; i < collection.Count; i++)
{
if (item.Type == collection[i].Type && string.Compare(collection[i].Name, item.Name, StringComparison.OrdinalIgnoreCase) > 0)
{
return i;
}
}
return item.Type switch
{
ArchiveItemType.Directory => collection.Count(e => e.Type == ArchiveItemType.Directory),
ArchiveItemType.File => collection.Count,
_ => 0,
};
}
private void CountItem(ArchiveItem item)
{
if (item.Type == ArchiveItemType.Directory)
{
_directoryCount++;
}
else if (item.Type == ArchiveItemType.File)
{
_fileCount++;
}
}
private static readonly HashSet<string> _supportedFileTypes = new()
{
".zip", ".rar", ".7z", ".tar", ".nupkg", ".jar", ".gz", ".tar", ".tar.gz", ".tgz",
};
}
}

View File

@@ -0,0 +1,85 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Media.Imaging;
using Peek.Common;
using Peek.Common.Helpers;
using Peek.Common.Models;
using Peek.FilePreviewer.Previewers.Helpers;
namespace Peek.FilePreviewer.Previewers.Archives.Helpers
{
public class IconCache
{
private readonly Dictionary<string, BitmapSource> _cache = new();
private BitmapSource? _directoryIconCache;
public async Task<BitmapSource?> GetFileExtIconAsync(string fileName, CancellationToken cancellationToken)
{
var extension = Path.GetExtension(fileName);
if (_cache.TryGetValue(extension, out var cachedIcon))
{
return cachedIcon;
}
try
{
var shFileInfo = default(SHFILEINFO);
if (NativeMethods.SHGetFileInfo(fileName, NativeMethods.FILE_ATTRIBUTE_NORMAL, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_USEFILEATTRIBUTES) != IntPtr.Zero)
{
var imageSource = await BitmapHelper.GetBitmapFromHIconAsync(shFileInfo.HIcon, cancellationToken);
_cache.Add(extension, imageSource);
return imageSource;
}
else
{
Logger.LogError($"Icon extraction for extension {extension} failed with error {Marshal.GetLastWin32Error()}");
}
}
catch (Exception ex)
{
Logger.LogError($"Icon extraction for extension {extension} failed", ex);
}
return null;
}
public async Task<BitmapSource?> GetDirectoryIconAsync(CancellationToken cancellationToken)
{
if (_directoryIconCache != null)
{
return _directoryIconCache;
}
try
{
var shinfo = default(SHFILEINFO);
if (NativeMethods.SHGetFileInfo("directory", NativeMethods.FILE_ATTRIBUTE_DIRECTORY, ref shinfo, (uint)Marshal.SizeOf(shinfo), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_USEFILEATTRIBUTES) != IntPtr.Zero)
{
var imageSource = await BitmapHelper.GetBitmapFromHIconAsync(shinfo.HIcon, cancellationToken);
_directoryIconCache = imageSource;
return imageSource;
}
else
{
Logger.LogError($"Icon extraction for directory failed with error {Marshal.GetLastWin32Error()}");
}
}
catch (Exception ex)
{
Logger.LogError($"Icon extraction for directory failed", ex);
}
return null;
}
}
}

View File

@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml.Media;
namespace Peek.FilePreviewer.Previewers.Archives.Models
{
public partial class ArchiveItem : ObservableObject
{
[ObservableProperty]
private string _name;
[ObservableProperty]
private ArchiveItemType _type;
[ObservableProperty]
private ImageSource? _icon;
[ObservableProperty]
private ulong _size;
[ObservableProperty]
private bool _isExpanded;
public ObservableCollection<ArchiveItem> Children { get; }
public ArchiveItem(string name, ArchiveItemType type, ImageSource? icon)
{
Name = name;
Type = type;
Icon = icon;
Children = new ObservableCollection<ArchiveItem>();
}
}
}

View File

@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace Peek.FilePreviewer.Previewers.Archives.Models
{
public class ArchiveItemTemplateSelector : DataTemplateSelector
{
public DataTemplate? DirectoryTemplate { get; set; }
public DataTemplate? FileTemplate { get; set; }
protected override DataTemplate? SelectTemplateCore(object item)
{
if (item is ArchiveItem archiveItem)
{
return archiveItem.Type == ArchiveItemType.Directory ? DirectoryTemplate : FileTemplate;
}
throw new ArgumentException("Item must be an ArchiveItem", nameof(item));
}
}
}

View File

@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Peek.FilePreviewer.Previewers.Archives.Models
{
public enum ArchiveItemType
{
Directory = 0,
File = 1,
}
}

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
@@ -18,7 +19,7 @@ namespace Peek.FilePreviewer.Previewers.Helpers
{
try
{
var bitmap = System.Drawing.Image.FromHbitmap(hbitmap);
var bitmap = Image.FromHbitmap(hbitmap);
if (isSupportingTransparency)
{
bitmap.MakeTransparent();
@@ -44,5 +45,32 @@ namespace Peek.FilePreviewer.Previewers.Helpers
NativeMethods.DeleteObject(hbitmap);
}
}
public static async Task<BitmapSource> GetBitmapFromHIconAsync(IntPtr hicon, CancellationToken cancellationToken)
{
try
{
var icon = (Icon)Icon.FromHandle(hicon).Clone();
var bitmap = icon.ToBitmap();
var bitmapImage = new BitmapImage();
using (var stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
stream.Position = 0;
cancellationToken.ThrowIfCancellationRequested();
await bitmapImage.SetSourceAsync(stream.AsRandomAccessStream());
}
return bitmapImage;
}
finally
{
// Delete HIcon to avoid memory leaks
_ = NativeMethods.DestroyIcon(hicon);
}
}
}
}

View File

@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.ObjectModel;
using Peek.FilePreviewer.Previewers.Archives.Models;
namespace Peek.FilePreviewer.Previewers.Interfaces
{
public interface IArchivePreviewer : IPreviewer, IDisposable
{
ObservableCollection<ArchiveItem> Tree { get; }
string? DirectoryCountText { get; }
string? FileCountText { get; }
string? SizeText { get; }
}
}

View File

@@ -10,6 +10,13 @@ namespace Peek.Common
{
public static class NativeMethods
{
internal const uint SHGFI_ICON = 0x000000100;
internal const uint SHGFI_LINKOVERLAY = 0x000008000;
internal const uint SHGFI_SMALLICON = 0x000000001;
internal const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
internal const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
internal const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int SHCreateItemFromParsingName(
[MarshalAs(UnmanagedType.LPWStr)] string path,
@@ -17,8 +24,14 @@ namespace Peek.Common
ref Guid riid,
[MarshalAs(UnmanagedType.Interface)] out IShellItem shellItem);
[DllImport("User32.dll", SetLastError = true)]
internal static extern int DestroyIcon(IntPtr hIcon);
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteObject(IntPtr hObject);
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
}
}

View File

@@ -4,6 +4,7 @@
using Microsoft.PowerToys.Telemetry;
using Peek.Common.Models;
using Peek.FilePreviewer.Previewers.Archives;
using Peek.UI.Telemetry.Events;
namespace Peek.FilePreviewer.Previewers
@@ -24,6 +25,10 @@ namespace Peek.FilePreviewer.Previewers
{
return new WebBrowserPreviewer(file);
}
else if (ArchivePreviewer.IsFileTypeSupported(file.Extension))
{
return new ArchivePreviewer(file);
}
// Other previewer types check their supported file types here
return CreateDefaultPreviewer(file);