From 0429709175749f6071b688e8baef2c145b11a591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Fri, 12 May 2023 16:54:35 +0200 Subject: [PATCH] Copy base64 hash of content --- .../UserCommand/CopyBase64Command.cs | 13 +++++++++ .../ToolUserCommandHandlerService.cs | 29 ++++++++++++++++++- ...faultIdentifiableCommandHandlerRegister.cs | 1 + .../Configuration/MainConfiguration.cs | 3 +- .../Services/SystemClipboardService.cs | 1 + 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/AppCommon/FileTime.App.Core.Abstraction/UserCommand/CopyBase64Command.cs diff --git a/src/AppCommon/FileTime.App.Core.Abstraction/UserCommand/CopyBase64Command.cs b/src/AppCommon/FileTime.App.Core.Abstraction/UserCommand/CopyBase64Command.cs new file mode 100644 index 0000000..a671883 --- /dev/null +++ b/src/AppCommon/FileTime.App.Core.Abstraction/UserCommand/CopyBase64Command.cs @@ -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; +} \ No newline at end of file diff --git a/src/AppCommon/FileTime.App.Core/Services/UserCommandHandler/ToolUserCommandHandlerService.cs b/src/AppCommon/FileTime.App.Core/Services/UserCommandHandler/ToolUserCommandHandlerService.cs index 353573e..45ed290 100644 --- a/src/AppCommon/FileTime.App.Core/Services/UserCommandHandler/ToolUserCommandHandlerService.cs +++ b/src/AppCommon/FileTime.App.Core/Services/UserCommandHandler/ToolUserCommandHandlerService.cs @@ -1,7 +1,11 @@ using System.Diagnostics; +using System.Security.Cryptography; +using System.Text; using FileTime.App.Core.UserCommand; using FileTime.App.Core.ViewModels; using FileTime.App.Search; +using FileTime.Core.ContentAccess; +using FileTime.Core.Enums; using FileTime.Core.Interactions; using FileTime.Core.Models; using FileTime.Core.Timeline; @@ -16,6 +20,7 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase private readonly IItemNameConverterService _itemNameConverterService; private readonly ITimelessContentProvider _timelessContentProvider; private readonly IUserCommandHandlerService _userCommandHandlerService; + private readonly IContentAccessorFactory _contentAccessorFactory; private IContainer? _currentLocation; private IItemViewModel? _currentSelectedItem; @@ -26,7 +31,8 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase ISearchManager searchManager, IItemNameConverterService itemNameConverterService, ITimelessContentProvider timelessContentProvider, - IUserCommandHandlerService userCommandHandlerService) : base(appState) + IUserCommandHandlerService userCommandHandlerService, + IContentAccessorFactory contentAccessorFactory) : base(appState) { _systemClipboardService = systemClipboardService; _userCommunicationService = userCommunicationService; @@ -34,6 +40,7 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase _itemNameConverterService = itemNameConverterService; _timelessContentProvider = timelessContentProvider; _userCommandHandlerService = userCommandHandlerService; + _contentAccessorFactory = contentAccessorFactory; SaveCurrentLocation(l => _currentLocation = l); SaveCurrentSelectedItem(i => _currentSelectedItem = i); @@ -41,10 +48,30 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase { new TypeUserCommandHandler(OpenInDefaultFileExplorer), new TypeUserCommandHandler(CopyNativePath), + new TypeUserCommandHandler(CopyBase64), new TypeUserCommandHandler(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) { if (_currentLocation is null) return; diff --git a/src/AppCommon/FileTime.App.Core/StartupServices/DefaultIdentifiableCommandHandlerRegister.cs b/src/AppCommon/FileTime.App.Core/StartupServices/DefaultIdentifiableCommandHandlerRegister.cs index 1a6f755..5aa239c 100644 --- a/src/AppCommon/FileTime.App.Core/StartupServices/DefaultIdentifiableCommandHandlerRegister.cs +++ b/src/AppCommon/FileTime.App.Core/StartupServices/DefaultIdentifiableCommandHandlerRegister.cs @@ -13,6 +13,7 @@ public class DefaultIdentifiableCommandHandlerRegister : IStartupHandler AddUserCommand(CloseTabCommand.Instance); AddUserCommand(CopyCommand.Instance); + AddUserCommand(CopyBase64Command.Instance); AddUserCommand(CopyNativePathCommand.Instance); AddUserCommand(CreateContainer.Instance); AddUserCommand(CreateElement.Instance); diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.Abstractions/Configuration/MainConfiguration.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.Abstractions/Configuration/MainConfiguration.cs index 77480f7..013f85b 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.Abstractions/Configuration/MainConfiguration.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.Abstractions/Configuration/MainConfiguration.cs @@ -40,12 +40,13 @@ public static class MainConfiguration } private static List InitDefaultKeyBindings() => - new List() + new List { //new CommandBindingConfiguration(ConfigCommand.AutoRefresh, new KeyConfig(Key.R, shift: true)), //new CommandBindingConfiguration(ConfigCommand.ChangeTimelineMode, new[] { Key.T, Key.M }), new(CloseTabCommand.CommandName, Key.Q), //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 CommandBindingConfiguration(ConfigCommand.CopyHash, new[] { Key.C, Key.H }), new(CopyNativePathCommand.CommandName, new[] {Key.C, Key.P}), diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp/Services/SystemClipboardService.cs b/src/GuiApp/Avalonia/FileTime.GuiApp/Services/SystemClipboardService.cs index e903c4c..bacbba1 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp/Services/SystemClipboardService.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp/Services/SystemClipboardService.cs @@ -23,6 +23,7 @@ public class SystemClipboardService : ISystemClipboardService await clipboard.ClearAsync(); var formats = await clipboard.GetFormatsAsync(); + if (!formats.Contains("asd")) return; var obj = (await clipboard.GetDataAsync("PNG")); /*var ms = new MemoryStream();