Copy base64 hash of content

This commit is contained in:
2023-05-12 16:54:35 +02:00
parent 568bfde2fb
commit 0429709175
5 changed files with 45 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class CopyBase64Command : IIdentifiableUserCommand
{
public const string CommandName = "copy_base64";
public static CopyBase64Command Instance { get; } = new();
private CopyBase64Command()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -1,7 +1,11 @@
using System.Diagnostics; using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using FileTime.App.Core.UserCommand; using FileTime.App.Core.UserCommand;
using FileTime.App.Core.ViewModels; using FileTime.App.Core.ViewModels;
using FileTime.App.Search; using FileTime.App.Search;
using FileTime.Core.ContentAccess;
using FileTime.Core.Enums;
using FileTime.Core.Interactions; using FileTime.Core.Interactions;
using FileTime.Core.Models; using FileTime.Core.Models;
using FileTime.Core.Timeline; using FileTime.Core.Timeline;
@@ -16,6 +20,7 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase
private readonly IItemNameConverterService _itemNameConverterService; private readonly IItemNameConverterService _itemNameConverterService;
private readonly ITimelessContentProvider _timelessContentProvider; private readonly ITimelessContentProvider _timelessContentProvider;
private readonly IUserCommandHandlerService _userCommandHandlerService; private readonly IUserCommandHandlerService _userCommandHandlerService;
private readonly IContentAccessorFactory _contentAccessorFactory;
private IContainer? _currentLocation; private IContainer? _currentLocation;
private IItemViewModel? _currentSelectedItem; private IItemViewModel? _currentSelectedItem;
@@ -26,7 +31,8 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase
ISearchManager searchManager, ISearchManager searchManager,
IItemNameConverterService itemNameConverterService, IItemNameConverterService itemNameConverterService,
ITimelessContentProvider timelessContentProvider, ITimelessContentProvider timelessContentProvider,
IUserCommandHandlerService userCommandHandlerService) : base(appState) IUserCommandHandlerService userCommandHandlerService,
IContentAccessorFactory contentAccessorFactory) : base(appState)
{ {
_systemClipboardService = systemClipboardService; _systemClipboardService = systemClipboardService;
_userCommunicationService = userCommunicationService; _userCommunicationService = userCommunicationService;
@@ -34,6 +40,7 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase
_itemNameConverterService = itemNameConverterService; _itemNameConverterService = itemNameConverterService;
_timelessContentProvider = timelessContentProvider; _timelessContentProvider = timelessContentProvider;
_userCommandHandlerService = userCommandHandlerService; _userCommandHandlerService = userCommandHandlerService;
_contentAccessorFactory = contentAccessorFactory;
SaveCurrentLocation(l => _currentLocation = l); SaveCurrentLocation(l => _currentLocation = l);
SaveCurrentSelectedItem(i => _currentSelectedItem = i); SaveCurrentSelectedItem(i => _currentSelectedItem = i);
@@ -41,10 +48,30 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase
{ {
new TypeUserCommandHandler<OpenInDefaultFileExplorerCommand>(OpenInDefaultFileExplorer), new TypeUserCommandHandler<OpenInDefaultFileExplorerCommand>(OpenInDefaultFileExplorer),
new TypeUserCommandHandler<CopyNativePathCommand>(CopyNativePath), new TypeUserCommandHandler<CopyNativePathCommand>(CopyNativePath),
new TypeUserCommandHandler<CopyBase64Command>(CopyBase64),
new TypeUserCommandHandler<SearchCommand>(Search), new TypeUserCommandHandler<SearchCommand>(Search),
}); });
} }
private async Task CopyBase64()
{
var item = _currentSelectedItem?.BaseItem;
if (item?.Type != AbsolutePathType.Element || item is not IElement element) return;
var contentReader = await _contentAccessorFactory.GetContentReaderFactory(element.Provider).CreateContentReaderAsync(element);
using var ms = new MemoryStream();
while (true)
{
//TODO handle large files
var data = await contentReader.ReadBytesAsync(1048576);
if (data.Length == 0) break;
await ms.WriteAsync(data);
}
var base64Hash = Convert.ToBase64String(ms.ToArray());
await _systemClipboardService.CopyToClipboardAsync(base64Hash);
}
private async Task Search(SearchCommand searchCommand) private async Task Search(SearchCommand searchCommand)
{ {
if (_currentLocation is null) return; if (_currentLocation is null) return;

View File

@@ -13,6 +13,7 @@ public class DefaultIdentifiableCommandHandlerRegister : IStartupHandler
AddUserCommand(CloseTabCommand.Instance); AddUserCommand(CloseTabCommand.Instance);
AddUserCommand(CopyCommand.Instance); AddUserCommand(CopyCommand.Instance);
AddUserCommand(CopyBase64Command.Instance);
AddUserCommand(CopyNativePathCommand.Instance); AddUserCommand(CopyNativePathCommand.Instance);
AddUserCommand(CreateContainer.Instance); AddUserCommand(CreateContainer.Instance);
AddUserCommand(CreateElement.Instance); AddUserCommand(CreateElement.Instance);

View File

@@ -40,12 +40,13 @@ public static class MainConfiguration
} }
private static List<CommandBindingConfiguration> InitDefaultKeyBindings() => private static List<CommandBindingConfiguration> InitDefaultKeyBindings() =>
new List<CommandBindingConfiguration>() new List<CommandBindingConfiguration>
{ {
//new CommandBindingConfiguration(ConfigCommand.AutoRefresh, new KeyConfig(Key.R, shift: true)), //new CommandBindingConfiguration(ConfigCommand.AutoRefresh, new KeyConfig(Key.R, shift: true)),
//new CommandBindingConfiguration(ConfigCommand.ChangeTimelineMode, new[] { Key.T, Key.M }), //new CommandBindingConfiguration(ConfigCommand.ChangeTimelineMode, new[] { Key.T, Key.M }),
new(CloseTabCommand.CommandName, Key.Q), new(CloseTabCommand.CommandName, Key.Q),
//new CommandBindingConfiguration(ConfigCommand.Compress, new[] { Key.Y, Key.C }), //new CommandBindingConfiguration(ConfigCommand.Compress, new[] { Key.Y, Key.C }),
new(CopyBase64Command.CommandName, new[] {Key.C, Key.B}),
new(CopyCommand.CommandName, new[] {Key.Y, Key.Y}), new(CopyCommand.CommandName, new[] {Key.Y, Key.Y}),
//new CommandBindingConfiguration(ConfigCommand.CopyHash, new[] { Key.C, Key.H }), //new CommandBindingConfiguration(ConfigCommand.CopyHash, new[] { Key.C, Key.H }),
new(CopyNativePathCommand.CommandName, new[] {Key.C, Key.P}), new(CopyNativePathCommand.CommandName, new[] {Key.C, Key.P}),

View File

@@ -23,6 +23,7 @@ public class SystemClipboardService : ISystemClipboardService
await clipboard.ClearAsync(); await clipboard.ClearAsync();
var formats = await clipboard.GetFormatsAsync(); var formats = await clipboard.GetFormatsAsync();
if (!formats.Contains("asd")) return; if (!formats.Contains("asd")) return;
var obj = (await clipboard.GetDataAsync("PNG")); var obj = (await clipboard.GetDataAsync("PNG"));
/*var ms = new MemoryStream(); /*var ms = new MemoryStream();