浏览代码

Fix image saving outside of current directory

Refactor: extract image save logic to `SaveImageHandler`, remove redundant methods, and simplify resize/save workflow.
Ruben 3 月之前
父节点
当前提交
f5bc779dce

+ 49 - 0
src/PicView.Avalonia/ImageHandling/SaveImageHandler.cs

@@ -0,0 +1,49 @@
+using PicView.Avalonia.Navigation;
+using PicView.Avalonia.UI;
+using PicView.Avalonia.ViewModels;
+using PicView.Core.ImageDecoding;
+using PicView.Core.Localization;
+
+namespace PicView.Avalonia.ImageHandling;
+
+public static class SaveImageHandler
+{
+    public static async Task SaveImageWithPossibleNavigation(MainViewModel vm,
+        string path,
+        string destination,
+        bool sameFile,
+        string ext,
+        uint? width,
+        uint? height,
+        uint? quality,
+        uint? rotationAngle,
+        bool isKeepingAspectRatio)
+    {
+
+        var success = await SaveImageFileHelper.SaveImageAsync(
+            null, path, sameFile ? null : destination, width, height, quality,
+            ext, rotationAngle, null, isKeepingAspectRatio).ConfigureAwait(false);
+
+        if (!success)
+        {
+            await TooltipHelper.ShowTooltipMessageAsync(TranslationManager.Translation.SavingFileFailed);
+            return;
+        }
+        
+        if (Path.GetExtension(path) != ext && sameFile)
+        {
+            // Delete the old file
+            await vm.PlatformService.DeleteFile(path, true); 
+        }
+
+        if (destination == path)
+        {
+            await NavigationManager.QuickReload().ConfigureAwait(false);
+        }
+        else if (Path.GetDirectoryName(path) == Path.GetDirectoryName(destination))
+        {
+            // Load the file if saved within same directory
+            await NavigationManager.LoadPicFromFile(destination, vm).ConfigureAwait(false);
+        }
+    }
+}

+ 4 - 13
src/PicView.Avalonia/Views/ImageInfoView.axaml.cs

@@ -3,11 +3,11 @@ using Avalonia.Controls;
 using Avalonia.Input;
 using PicView.Avalonia.Converters;
 using PicView.Avalonia.FileSystem;
+using PicView.Avalonia.ImageHandling;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.Resizing;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
-using PicView.Core.ImageDecoding;
 using PicView.Core.Titles;
 using R3;
 
@@ -163,18 +163,9 @@ public partial class ImageInfoView : UserControl
         {
             return;
         }
-
-        if (ext != Path.GetExtension(location))
-        {
-            destination = Path.ChangeExtension(destination, ext);
-            await FileRenamer.AttemptRenameAsync(location, destination, DataContext as MainViewModel,
-                widthValue, heightValue);
-        }
-        else
-        {
-            await SaveImageFileHelper.SaveImageAsync(null, location, destination, widthValue, heightValue, null, ext)
-                .ConfigureAwait(false);
-        }
+        var sameFile = destination.Equals(location, StringComparison.OrdinalIgnoreCase);
+        await SaveImageHandler.SaveImageWithPossibleNavigation(DataContext as MainViewModel, location, destination,  sameFile, ext, widthValue, heightValue,
+            null, null, true);
     }
 
     private string GetExtension() => ConversionComboBox.SelectedIndex switch

+ 7 - 35
src/PicView.Avalonia/Views/SingleImageResizeView.axaml.cs

@@ -5,12 +5,11 @@ using Avalonia.Input;
 using Avalonia.Media;
 using Avalonia.Threading;
 using PicView.Avalonia.FileSystem;
-using PicView.Avalonia.Navigation;
+using PicView.Avalonia.ImageHandling;
 using PicView.Avalonia.Resizing;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.ImageDecoding;
-using PicView.Core.Localization;
 using R3;
 
 namespace PicView.Avalonia.Views;
@@ -191,34 +190,19 @@ public partial class SingleImageResizeView : UserControl
         }
 
         await Dispatcher.UIThread.InvokeAsync(() => SetLoadingState(true));
-
-        const int rotationAngle = 0; // TODO: Add rotation control
-
-        var file = vm.PicViewer.FileInfo.CurrentValue.FullName;
+        
+        var path = vm.PicViewer.FileInfo.CurrentValue.FullName;
         var ext = GetSelectedFileExtension(vm, ref destination);
         destination = Path.ChangeExtension(destination, ext);
-        var sameFile = file.Equals(destination, StringComparison.OrdinalIgnoreCase);
+        var sameFile = path.Equals(destination, StringComparison.OrdinalIgnoreCase);
 
         var quality = GetQualityValue(ext, destination);
 
-        var success = await SaveImageFileHelper.SaveImageAsync(
-            null, file, sameFile ? null : destination, width, height, quality,
-            ext, rotationAngle, null, _isKeepingAspectRatio).ConfigureAwait(false);
+        await SaveImageHandler.SaveImageWithPossibleNavigation(vm, path, destination, sameFile, ext, width, height,
+            quality, null, _isKeepingAspectRatio);
 
         await Dispatcher.UIThread.InvokeAsync(() => SetLoadingState(false));
-
-        if (!success)
-        {
-            await TooltipHelper.ShowTooltipMessageAsync(TranslationManager.Translation.SavingFileFailed);
-            return;
-        }
-
-        await HandlePostSaveActions(vm, file, destination);
-
-        if (Path.GetExtension(file) != ext)
-        {
-            await vm.PlatformService.DeleteFile(file, true); 
-        }
+        
     }
 
     private void SetLoadingState(bool isLoading)
@@ -289,18 +273,6 @@ public partial class SingleImageResizeView : UserControl
         return null;
     }
 
-    private static async Task HandlePostSaveActions(MainViewModel vm, string file, string destination)
-    {
-        if (destination == file)
-        {
-            await NavigationManager.QuickReload().ConfigureAwait(false);
-        }
-        else if (Path.GetDirectoryName(file) == Path.GetDirectoryName(destination))
-        {
-            await NavigationManager.LoadPicFromFile(destination, vm).ConfigureAwait(false);
-        }
-    }
-
     private void ResetSettings(MainViewModel vm)
     {
         PixelWidthTextBox.Text = vm.PicViewer.PixelWidth.ToString();

+ 4 - 0
src/PicView.Core/ImageDecoding/SaveImageFileHelper.cs

@@ -61,6 +61,10 @@ public static class SaveImageFileHelper
             {
                 magickImage.Resize(percentage.Value);
             }
+            else if (width is > 0 && height is > 0)
+            {
+                 magickImage.Resize(width.Value, height.Value);
+            }
             else if (width is not null)
             {
                 if (height is not null)