Skip to content

Commit 27dec66

Browse files
committed
Add compare file button
1 parent 4bf1cae commit 27dec66

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,4 +4059,8 @@
40594059
<value>Hashes don't match</value>
40604060
<comment>Appears when two compared hashes don't match</comment>
40614061
</data>
4062+
<data name="CompareFile" xml:space="preserve">
4063+
<value>Compare a file</value>
4064+
<comment>Button that appears in file hash properties that allows the user to compare two files</comment>
4065+
</data>
40624066
</root>

src/Files.App/Views/Properties/HashesPage.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@
240240
VerticalAlignment="Center"
241241
Visibility="Collapsed" />
242242
</Grid>
243+
244+
<!-- Compare File Button -->
245+
<Button
246+
x:Name="CompareFileButton"
247+
Grid.Row="0"
248+
Grid.Column="1"
249+
Margin="12"
250+
Click="CompareFileButton_Click"
251+
Content="{helpers:ResourceString Name=CompareFile}" />
243252
</Grid>
244253
</Grid>
245254
</vm:BasePropertiesPage>

src/Files.App/Views/Properties/HashesPage.xaml.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// Copyright (c) Files Community
1+
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

44
using Files.App.Data.Parameters;
55
using Files.App.Utils;
66
using Files.App.ViewModels.Properties;
77
using Microsoft.UI.Xaml;
8-
using Microsoft.UI.Xaml.Media;
98
using Microsoft.UI.Xaml.Controls;
109
using Microsoft.UI.Xaml.Controls.Primitives;
1110
using Microsoft.UI.Xaml.Navigation;
11+
using System.IO;
12+
using System.Security.Cryptography;
1213
using System.Threading.Tasks;
13-
using Windows.UI;
1414

1515
namespace Files.App.Views.Properties
1616
{
@@ -79,6 +79,46 @@ private void CompareHashes()
7979
HashMatchIcon.Visibility = Visibility.Visible;
8080
}
8181

82+
private async void CompareFileButton_Click(object sender, RoutedEventArgs e)
83+
{
84+
var picker = new Windows.Storage.Pickers.FileOpenPicker();
85+
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
86+
picker.FileTypeFilter.Add("*");
87+
WinRT.Interop.InitializeWithWindow.Initialize(picker, MainWindow.Instance.WindowHandle);
88+
89+
var file = await picker.PickSingleFileAsync();
90+
if (file != null)
91+
{
92+
var selectedFileHash = await CalculateSHA384HashAsync(file.Path);
93+
var currentFileHash = HashesViewModel.Hashes.FirstOrDefault(h => h.Algorithm == "SHA384")?.HashValue;
94+
95+
if (selectedFileHash == currentFileHash)
96+
{
97+
HashMatchIcon.Glyph = "\uE73E"; // Check mark
98+
ToolTipService.SetToolTip(HashMatchIcon, Strings.HashesMatch.GetLocalizedResource());
99+
}
100+
else
101+
{
102+
HashMatchIcon.Glyph = "\uE711"; // Cross mark
103+
ToolTipService.SetToolTip(HashMatchIcon, Strings.HashesMatch.GetLocalizedResource());
104+
}
105+
106+
HashMatchIcon.Visibility = Visibility.Visible;
107+
}
108+
}
109+
110+
private async Task<string> CalculateSHA384HashAsync(string filePath)
111+
{
112+
using (var stream = File.OpenRead(filePath))
113+
{
114+
using (var sha384 = SHA384.Create())
115+
{
116+
var hash = await Task.Run(() => sha384.ComputeHash(stream));
117+
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
118+
}
119+
}
120+
}
121+
82122
private void MenuFlyout_Closing(FlyoutBase sender, FlyoutBaseClosingEventArgs e)
83123
{
84124
e.Cancel = _cancel;

0 commit comments

Comments
 (0)