Skip to content

Commit c594931

Browse files
authored
Fix: Fixed crash when manipulating invalid images (#14258)
1 parent cf569f2 commit c594931

File tree

4 files changed

+58
-37
lines changed

4 files changed

+58
-37
lines changed

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public static void HandleAppUnhandledException(Exception? ex, bool showToastNoti
317317
{
318318
await Launcher.LaunchUriAsync(new Uri("files-uwp:"));
319319
})
320-
.Wait(1000);
320+
.Wait(100);
321321
}
322322
Process.GetCurrentProcess().Kill();
323323
}

src/Files.App/Helpers/BitmapHelper.cs

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using Microsoft.UI.Xaml.Controls;
45
using Microsoft.UI.Xaml.Media.Imaging;
56
using System.IO;
7+
using Windows.Foundation.Metadata;
68
using Windows.Graphics.Imaging;
79
using Windows.Storage;
810
using Windows.Storage.Streams;
@@ -47,43 +49,60 @@ internal static class BitmapHelper
4749
/// </remarks>
4850
public static async Task RotateAsync(string filePath, BitmapRotation rotation)
4951
{
50-
if (string.IsNullOrEmpty(filePath))
52+
try
5153
{
52-
return;
53-
}
54+
if (string.IsNullOrEmpty(filePath))
55+
{
56+
return;
57+
}
5458

55-
var file = await StorageHelpers.ToStorageItem<IStorageFile>(filePath);
56-
if (file is null)
57-
{
58-
return;
59-
}
59+
var file = await StorageHelpers.ToStorageItem<IStorageFile>(filePath);
60+
if (file is null)
61+
{
62+
return;
63+
}
6064

61-
var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask());
62-
using IRandomAccessStream fileStream = fileStreamRes.Result;
63-
if (fileStream is null)
64-
{
65-
return;
66-
}
65+
var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask());
66+
using IRandomAccessStream fileStream = fileStreamRes.Result;
67+
if (fileStream is null)
68+
{
69+
return;
70+
}
6771

68-
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
69-
using var memStream = new InMemoryRandomAccessStream();
70-
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
72+
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
73+
using var memStream = new InMemoryRandomAccessStream();
74+
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
75+
76+
for (int i = 0; i < decoder.FrameCount - 1; i++)
77+
{
78+
encoder.BitmapTransform.Rotation = rotation;
79+
await encoder.GoToNextFrameAsync();
80+
}
7181

72-
for (int i = 0; i < decoder.FrameCount - 1; i++)
73-
{
7482
encoder.BitmapTransform.Rotation = rotation;
75-
await encoder.GoToNextFrameAsync();
76-
}
7783

78-
encoder.BitmapTransform.Rotation = rotation;
84+
await encoder.FlushAsync();
7985

80-
await encoder.FlushAsync();
86+
memStream.Seek(0);
87+
fileStream.Seek(0);
88+
fileStream.Size = 0;
8189

82-
memStream.Seek(0);
83-
fileStream.Seek(0);
84-
fileStream.Size = 0;
90+
await RandomAccessStream.CopyAsync(memStream, fileStream);
91+
}
92+
catch (Exception ex)
93+
{
94+
var errorDialog = new ContentDialog()
95+
{
96+
Title = "FailedToRotateImage".GetLocalizedResource(),
97+
Content = ex.Message,
98+
PrimaryButtonText = "OK".GetLocalizedResource(),
99+
};
85100

86-
await RandomAccessStream.CopyAsync(memStream, fileStream);
101+
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
102+
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
103+
104+
await errorDialog.TryShowAsync();
105+
}
87106
}
88107

89108
/// <summary>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,4 +3659,7 @@
36593659
<data name="SortPriority" xml:space="preserve">
36603660
<value>Sort priority</value>
36613661
</data>
3662+
<data name="FailedToRotateImage" xml:space="preserve">
3663+
<value>Failed to rotate the image</value>
3664+
</data>
36623665
</root>

src/Files.App/Utils/Global/WallpaperHelpers.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,25 @@ public static class WallpaperHelpers
1313
{
1414
public static async Task SetAsBackgroundAsync(WallpaperType type, string filePath)
1515
{
16-
17-
if (type == WallpaperType.Desktop)
16+
try
1817
{
19-
try
18+
if (type == WallpaperType.Desktop)
2019
{
2120
// Set the desktop background
2221
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
2322
wallpaper.GetMonitorDevicePathAt(0, out var monitorId);
2423
wallpaper.SetWallpaper(monitorId, filePath);
2524
}
26-
catch (Exception ex)
25+
else if (type == WallpaperType.LockScreen)
2726
{
28-
ShowErrorPrompt(ex.Message);
27+
// Set the lockscreen background
28+
IStorageFile sourceFile = await StorageFile.GetFileFromPathAsync(filePath);
29+
await LockScreen.SetImageFileAsync(sourceFile);
2930
}
3031
}
31-
else if (type == WallpaperType.LockScreen)
32+
catch (Exception ex)
3233
{
33-
// Set the lockscreen background
34-
IStorageFile sourceFile = await StorageFile.GetFileFromPathAsync(filePath);
35-
await LockScreen.SetImageFileAsync(sourceFile);
34+
ShowErrorPrompt(ex.Message);
3635
}
3736
}
3837

0 commit comments

Comments
 (0)