Browse Source

Merge pull request #500 from PixiEditor/filename-crash

Fixed export filename crash
Krzysztof Krysiński 2 years ago
parent
commit
9a7d9a8a66

+ 21 - 0
src/PixiEditor/Helpers/SupportedFilesHelper.cs

@@ -36,6 +36,27 @@ internal class SupportedFilesHelper
         return allFileTypeDialogsData.Where(i => i.FileType == type).Single();
     }
 
+    public static string FixFileExtension(string pathWithOrWithoutExtension, FileType requestedType)
+    {
+        if (requestedType == FileType.Unset)
+            throw new ArgumentException("A valid filetype is required", nameof(requestedType));
+
+        var typeFromPath = SupportedFilesHelper.ParseImageFormat(Path.GetExtension(pathWithOrWithoutExtension));
+        if (typeFromPath != FileType.Unset && typeFromPath == requestedType)
+            return pathWithOrWithoutExtension;
+        return AppendExtension(pathWithOrWithoutExtension, SupportedFilesHelper.GetFileTypeDialogData(requestedType));
+    }
+
+    public static string AppendExtension(string path, FileTypeDialogData data)
+    {
+        string ext = data.Extensions.First();
+        string filename = Path.GetFileName(path);
+        if (filename.Length + ext.Length > 255)
+            filename = filename.Substring(0, 255 - ext.Length);
+        filename += ext;
+        return Path.Combine(Path.GetDirectoryName(path), filename);
+    }
+
     public static bool IsSupportedFile(string path)
     {
         var ext = Path.GetExtension(path.ToLower());

+ 1 - 22
src/PixiEditor/Models/IO/Exporter.cs

@@ -67,7 +67,7 @@ internal class Exporter
     /// </summary>
     public static SaveResult TrySaveUsingDataFromDialog(DocumentViewModel document, string pathFromDialog, FileType fileTypeFromDialog, out string finalPath, VecI? exportSize = null)
     {
-        finalPath = FixFileExtension(pathFromDialog, fileTypeFromDialog);
+        finalPath = SupportedFilesHelper.FixFileExtension(pathFromDialog, fileTypeFromDialog);
         var saveResult = TrySave(document, finalPath, exportSize);
         if (saveResult != SaveResult.Success)
             finalPath = "";
@@ -75,17 +75,6 @@ internal class Exporter
         return saveResult;
     }
 
-    private static string FixFileExtension(string pathWithOrWithoutExtension, FileType requestedType)
-    {
-        if (requestedType == FileType.Unset)
-            throw new ArgumentException("A valid filetype is required", nameof(requestedType));
-
-        var typeFromPath = SupportedFilesHelper.ParseImageFormat(Path.GetExtension(pathWithOrWithoutExtension));
-        if (typeFromPath != FileType.Unset && typeFromPath == requestedType)
-            return pathWithOrWithoutExtension;
-        return AppendExtension(pathWithOrWithoutExtension, SupportedFilesHelper.GetFileTypeDialogData(requestedType));
-    }
-
     /// <summary>
     /// Attempts to save the document into the given location, filetype is inferred from path
     /// </summary>
@@ -120,16 +109,6 @@ internal class Exporter
         return SaveResult.Success;
     }
 
-    private static string AppendExtension(string path, FileTypeDialogData data)
-    {
-        string ext = data.Extensions.First();
-        string filename = Path.GetFileName(path);
-        if (filename.Length + ext.Length > 255)
-            filename = filename.Substring(0, 255 - ext.Length);
-        filename += ext;
-        return Path.Combine(Path.GetDirectoryName(path), filename);
-    }
-
     static Dictionary<FileType, Func<BitmapEncoder>> encodersFactory = new Dictionary<FileType, Func<BitmapEncoder>>();
 
     static Exporter()

+ 10 - 2
src/PixiEditor/ViewModels/SaveFilePopupViewModel.cs

@@ -59,13 +59,21 @@ internal class SaveFilePopupViewModel : ViewModelBase
             Title = "Export path",
             CheckPathExists = true,
             Filter = SupportedFilesHelper.BuildSaveFilter(false),
-            FilterIndex = 0
+            FilterIndex = 0,
+            AddExtension = true
         };
         if (path.ShowDialog() == true)
         {
             if (string.IsNullOrEmpty(path.FileName) == false)
             {
-                ChosenFormat = SupportedFilesHelper.ParseImageFormat(Path.GetExtension(path.SafeFileName));
+                ChosenFormat = SupportedFilesHelper.GetSaveFileTypeFromFilterIndex(false, path.FilterIndex);
+                if (ChosenFormat == FileType.Unset)
+                {
+                    return null;
+                }
+
+                path.FileName = SupportedFilesHelper.FixFileExtension(path.FileName, ChosenFormat);
+
                 return path.FileName;
             }
         }