Skip to content

Commit

Permalink
Add compare file button
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamparter committed Feb 16, 2025
1 parent 4bf1cae commit 27dec66
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -4059,4 +4059,8 @@
<value>Hashes don't match</value>
<comment>Appears when two compared hashes don't match</comment>
</data>
<data name="CompareFile" xml:space="preserve">
<value>Compare a file</value>
<comment>Button that appears in file hash properties that allows the user to compare two files</comment>
</data>
</root>
9 changes: 9 additions & 0 deletions src/Files.App/Views/Properties/HashesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@
VerticalAlignment="Center"
Visibility="Collapsed" />
</Grid>

<!-- Compare File Button -->
<Button
x:Name="CompareFileButton"
Grid.Row="0"
Grid.Column="1"
Margin="12"
Click="CompareFileButton_Click"
Content="{helpers:ResourceString Name=CompareFile}" />
</Grid>
</Grid>
</vm:BasePropertiesPage>
46 changes: 43 additions & 3 deletions src/Files.App/Views/Properties/HashesPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) Files Community
// Copyright (c) Files Community
// Licensed under the MIT License.

using Files.App.Data.Parameters;
using Files.App.Utils;
using Files.App.ViewModels.Properties;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Navigation;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Windows.UI;

namespace Files.App.Views.Properties
{
Expand Down Expand Up @@ -79,6 +79,46 @@ private void CompareHashes()
HashMatchIcon.Visibility = Visibility.Visible;
}

private async void CompareFileButton_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
picker.FileTypeFilter.Add("*");
WinRT.Interop.InitializeWithWindow.Initialize(picker, MainWindow.Instance.WindowHandle);

var file = await picker.PickSingleFileAsync();
if (file != null)
{
var selectedFileHash = await CalculateSHA384HashAsync(file.Path);
var currentFileHash = HashesViewModel.Hashes.FirstOrDefault(h => h.Algorithm == "SHA384")?.HashValue;

if (selectedFileHash == currentFileHash)
{
HashMatchIcon.Glyph = "\uE73E"; // Check mark
ToolTipService.SetToolTip(HashMatchIcon, Strings.HashesMatch.GetLocalizedResource());
}
else
{
HashMatchIcon.Glyph = "\uE711"; // Cross mark
ToolTipService.SetToolTip(HashMatchIcon, Strings.HashesMatch.GetLocalizedResource());
}

HashMatchIcon.Visibility = Visibility.Visible;
}
}

private async Task<string> CalculateSHA384HashAsync(string filePath)
{
using (var stream = File.OpenRead(filePath))
{
using (var sha384 = SHA384.Create())
{
var hash = await Task.Run(() => sha384.ComputeHash(stream));
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
}

private void MenuFlyout_Closing(FlyoutBase sender, FlyoutBaseClosingEventArgs e)
{
e.Cancel = _cancel;
Expand Down

0 comments on commit 27dec66

Please sign in to comment.