Skip to content

Commit

Permalink
Merge pull request #500 from PixiEditor/filename-crash
Browse files Browse the repository at this point in the history
Fixed export filename crash
  • Loading branch information
flabbet authored Apr 7, 2023
2 parents c1c33c2 + badb17c commit 9a7d9a8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
21 changes: 21 additions & 0 deletions src/PixiEditor/Helpers/SupportedFilesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ public static FileTypeDialogData GetFileTypeDialogData(FileType type)
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());
Expand Down
23 changes: 1 addition & 22 deletions src/PixiEditor/Models/IO/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,14 @@ public static DialogSaveResult TrySaveWithDialog(DocumentViewModel document, out
/// </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 = "";

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>
Expand Down Expand Up @@ -120,16 +109,6 @@ public static SaveResult TrySave(DocumentViewModel document, string pathWithExte
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()
Expand Down
12 changes: 10 additions & 2 deletions src/PixiEditor/ViewModels/SaveFilePopupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,21 @@ private string ChoosePath()
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;
}
}
Expand Down

0 comments on commit 9a7d9a8

Please sign in to comment.