Skip to content

Commit

Permalink
Fix: Fixed crash when manipulating invalid images (files-community#14258
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hishitetsu authored Dec 18, 2023
1 parent cf569f2 commit c594931
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Files.App/Helpers/Application/AppLifecycleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static void HandleAppUnhandledException(Exception? ex, bool showToastNoti
{
await Launcher.LaunchUriAsync(new Uri("files-uwp:"));
})
.Wait(1000);
.Wait(100);
}
Process.GetCurrentProcess().Kill();
}
Expand Down
73 changes: 46 additions & 27 deletions src/Files.App/Helpers/BitmapHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Imaging;
using System.IO;
using Windows.Foundation.Metadata;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
Expand Down Expand Up @@ -47,43 +49,60 @@ internal static class BitmapHelper
/// </remarks>
public static async Task RotateAsync(string filePath, BitmapRotation rotation)
{
if (string.IsNullOrEmpty(filePath))
try
{
return;
}
if (string.IsNullOrEmpty(filePath))
{
return;
}

var file = await StorageHelpers.ToStorageItem<IStorageFile>(filePath);
if (file is null)
{
return;
}
var file = await StorageHelpers.ToStorageItem<IStorageFile>(filePath);
if (file is null)
{
return;
}

var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask());
using IRandomAccessStream fileStream = fileStreamRes.Result;
if (fileStream is null)
{
return;
}
var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask());
using IRandomAccessStream fileStream = fileStreamRes.Result;
if (fileStream is null)
{
return;
}

BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
using var memStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
using var memStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);

for (int i = 0; i < decoder.FrameCount - 1; i++)
{
encoder.BitmapTransform.Rotation = rotation;
await encoder.GoToNextFrameAsync();
}

for (int i = 0; i < decoder.FrameCount - 1; i++)
{
encoder.BitmapTransform.Rotation = rotation;
await encoder.GoToNextFrameAsync();
}

encoder.BitmapTransform.Rotation = rotation;
await encoder.FlushAsync();

await encoder.FlushAsync();
memStream.Seek(0);
fileStream.Seek(0);
fileStream.Size = 0;

memStream.Seek(0);
fileStream.Seek(0);
fileStream.Size = 0;
await RandomAccessStream.CopyAsync(memStream, fileStream);
}
catch (Exception ex)
{
var errorDialog = new ContentDialog()
{
Title = "FailedToRotateImage".GetLocalizedResource(),
Content = ex.Message,
PrimaryButtonText = "OK".GetLocalizedResource(),
};

await RandomAccessStream.CopyAsync(memStream, fileStream);
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;

await errorDialog.TryShowAsync();
}
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3659,4 +3659,7 @@
<data name="SortPriority" xml:space="preserve">
<value>Sort priority</value>
</data>
<data name="FailedToRotateImage" xml:space="preserve">
<value>Failed to rotate the image</value>
</data>
</root>
17 changes: 8 additions & 9 deletions src/Files.App/Utils/Global/WallpaperHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ public static class WallpaperHelpers
{
public static async Task SetAsBackgroundAsync(WallpaperType type, string filePath)
{

if (type == WallpaperType.Desktop)
try
{
try
if (type == WallpaperType.Desktop)
{
// Set the desktop background
var wallpaper = (Shell32.IDesktopWallpaper)new Shell32.DesktopWallpaper();
wallpaper.GetMonitorDevicePathAt(0, out var monitorId);
wallpaper.SetWallpaper(monitorId, filePath);
}
catch (Exception ex)
else if (type == WallpaperType.LockScreen)
{
ShowErrorPrompt(ex.Message);
// Set the lockscreen background
IStorageFile sourceFile = await StorageFile.GetFileFromPathAsync(filePath);
await LockScreen.SetImageFileAsync(sourceFile);
}
}
else if (type == WallpaperType.LockScreen)
catch (Exception ex)
{
// Set the lockscreen background
IStorageFile sourceFile = await StorageFile.GetFileFromPathAsync(filePath);
await LockScreen.SetImageFileAsync(sourceFile);
ShowErrorPrompt(ex.Message);
}
}

Expand Down

0 comments on commit c594931

Please sign in to comment.