Files
PowerToys/src/modules/peek/Peek.FilePreviewer/Previewers/Archives/ArchivePreviewer.cs

220 lines
7.7 KiB
C#
Raw Normal View History

// 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.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;
namespace Peek.FilePreviewer.Previewers.Archives
{
public partial class ArchivePreviewer : ObservableObject, IArchivePreviewer
{
private static readonly char[] _keySeparators = { '/', '\\' };
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
[General]Reduce installer size by flattening application paths (#27451) * Flatten everything and succeed build * Figure out Settings assets * Remove UseCommonOutputDirectory tag * Proper settings app path * [VCM] Fix assets location * Fix some runtime paths * [RegistryPreview]Use MRTCore specific pri file * [Hosts]Use MRTCore specific pri file * [Settings]Use MRTCore specific pri file * [Peek]Use MRTCore specific pri file * [FileLocksmith]Use MRTCore specific pri file * [ScreenRuler]Use MRTCore specific pri file * [PowerRename]Use MRTCore specific pri file * [Peek]Move assets to own folder * [FileLocksmith] Use own Assets path * [Hosts]Use own assets folder * [PowerRename]Use own assets dir * [MeasureTool] Use its own assets folder * [ImageResizer]Use its own assets path * Fix spellcheck * Fix tab instead of space in project files * Normalize target frameworks and platforms * Remove WINRT_NO_MAKE_DETECTION flag. No longer needed? * Fix AOT and Hosts locations * Fix Dll version differences on dependency * Add Common.UI.csproj as refernce to fix dll versions * Update ControlzEx to normalize dll versions * Update System.Management version to 7.0.2 * Add GPOWrapper to Registry Preview to fix dll versions * [PTRun]Reference Microsoft.Extensions.Hosting to fix dll versions * Fix remaining output paths / dll version conflicts * [KeyboardManager]Executables still on their own directories * Fix Monaco paths * WinAppSDK apps get to play outside again * Enable VCM settings again * Fix KBM Editor path again * [Monaco]Set to own Assets dir * Fix installer preamble; Fix publish. remove unneeded publishes * Remove Hardlink functions * Installer builds again (still needs work to work) * Readd Monaco to spellcheck excludes * Fix spellcheck and call publish.cmd again * [Installer] Remove components that don't need own dirs * [Installer] Refactor Power Launcher * [Installer] Refactor Color Picker * [Installer] Refactor Monaco assets * [Installer]Generate File script no longer needs to remove files * [Installer]Refactor FileLocksmith * [Installer] Refactor Hosts * [Installer]Refactor Image Resizer * [Installer]Refactor MouseUtils * [Installer]Refactor MWB * [Installer]Refactor MeasureTool * [Installer]Refactor Peek * [Installer]Refactor PowerRename and registry fixes * [Installer]Refactor RegistryPreview * [Installer]Refactor ShortcutGuide * [Installer]Refactor Settings * [Installer]Clean up some unused stuff * [Installer]Clean up stuff for user install * [Installer]Fix WinUi3Apps wxs * [Installer]Fix misplaced folders * [Installer]Move x86 VCM dll to right path * Fix monaco language list location * [Installer]Fix VCM directory reference * [CI]Fix signing * [Installer] Fix resources folder for release CI * [ci]Looks like we still ship NLog on PowerToys Run * [Settings]Add dependency to avoid dll collision with Experimentation * [RegistryPreview]Move XAML files to own path * [RegistryPreview]Fix app icon * [Hosts]Move XAML files to their own path * [FileLocksmith]Move XAML files to their own path * [Peek]Move XAML files to own path * [ScreenRuler]Move XAML files to its own path * [Settings]Move XAML to its own path * [ColorPicker]Move Resources to Assets * [ShortcutGuide]Move svgs to own Assets path * [Awake]Move images to assets path * [Runner]Remove debug checks for PowerToys Run assets * [PTRun]Move images to its own assets path * [ImageResizer]Icon for context menu on own assets path * [PowerRename]Move ico to its own path * Remove unneeded intermediary directories * Remove further int dirs * Move tests to its own output path * Fix spellcheck * spellcheck: remove warnings * [CppAnalyzers]Ignore rule in a tool * [CI]Check if all deps.json files reference same versions * fix spellcheck * [ci]Fix task identation * [ci]Add script to guard against asset conflicts * [ci]Add more deps.json audit steps in the release build * Add xbf to spellcheck expects * Fix typo in asset conflict check scripts * Fix some more dependency conflicts * Downgrade CsWinRT to have the same dll version as sdk * [ci]Do a recursive check for every deps.json * Fix spellcheck error inside comment * [ci]Fix asset script error * [ci]Name deps.json verify tasks a bit better * [ci]Improve deps json verify script output * [ci]Update WinRT version to the same running in CI * Also upgrade CsWinRT in NOTICE.MD * [PowerRename]Move XAML files to own path * [Common]Fix Settings executable path * [ci]Verify there's no xbf files in app directories * [installer]Fix firewall path * [Monaco]Move new files to their proper assets path * [Monaco]Fix paths for new files after merge * [Feedback]Removed unneeded build conditions * [Feedback]Clear vcxproj direct reference to frameworks * [Feedback]RunPlugins name to hold PTRun plugins * [Feedback]Remove unneeded foreach * [Feedback]Shortcut Guide svgs with ** in project file * [Feedback]Fix spellcheck
2023-07-20 00:12:46 +01:00
DirectoryCountText = string.Format(CultureInfo.CurrentCulture, ResourceLoaderInstance.ResourceLoader.GetString("Archive_Directory_Count"), _directoryCount);
FileCountText = string.Format(CultureInfo.CurrentCulture, ResourceLoaderInstance.ResourceLoader.GetString("Archive_File_Count"), _fileCount);
SizeText = string.Format(CultureInfo.CurrentCulture, ResourceLoaderInstance.ResourceLoader.GetString("Archive_Size"), ReadableStringHelper.BytesToReadableString(_size), ReadableStringHelper.BytesToReadableString(_extractedSize));
State = PreviewState.Loaded;
}
public static bool IsItemSupported(IFileSystemItem item)
{
return _supportedFileTypes.Contains(item.Extension);
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
private async Task AddEntryAsync(IEntry entry, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(entry, nameof(entry));
if (entry.Key == null)
{
return;
}
var levels = entry.Key.Split(_keySeparators, StringSplitOptions.RemoveEmptyEntries);
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.gz", ".tgz",
};
}
}