Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Rotate and flip: a value from a newer Filee doesn't reset every preset

SeoJCalive4bfd9bf74575
diff --git a/src/Filee.App/ViewModels/PresetEditorViewModel.cs b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
index cb6a0e3f..9385d90e 100644
--- a/src/Filee.App/ViewModels/PresetEditorViewModel.cs
+++ b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
@@ -78,8 +78,8 @@ public sealed partial class PresetEditorViewModel : ObservableObject
_name = string.IsNullOrWhiteSpace(_edit.Name) ? loc.DisplayName(_edit) : _edit.Name;
_target = Targets.FirstOrDefault(t => t.Value == _edit.TargetFormat) ?? Targets[1];
_resize = ResizeModes.First(r => r.Value == _edit.Image.Resize);
- _rotation = Rotations.First(r => r.Value == _edit.Image.Rotation);
- _flip = Flips.First(f => f.Value == _edit.Image.Flip);
+ _rotation = Rotations.FirstOrDefault(r => r.Value == _edit.Image.Rotation) ?? Rotations[0];
+ _flip = Flips.FirstOrDefault(f => f.Value == _edit.Image.Flip) ?? Flips[0];
_pageSize = PageSizes.First(p => p.Value == _edit.Pdf.PageSize);
_location = Locations.First(l => l.Value == _edit.Output.Location);
_conflict = Conflicts.First(c => c.Value == _edit.Output.Conflict);
diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.cs
index 6bd7479c..a3a7a98a 100644
--- a/src/Filee.Core/Presets/Preset.cs
+++ b/src/Filee.Core/Presets/Preset.cs
@@ -154,6 +154,26 @@ public enum ImageFlip
Vertical,
}
+/// <summary>
+/// Reads an enum by name, and a value this version doesn't know (a newer Filee's, or a number outside it) as its
+/// first value, "none": the preset loads without it instead of the whole preset file failing (which resets every
+/// preset to the defaults).
+/// </summary>
+public sealed class UnknownAsNoneJsonConverter<TEnum> : JsonConverter<TEnum> where TEnum : struct, Enum
+{
+ public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
+ reader.TokenType == JsonTokenType.String && Enum.TryParse<TEnum>(reader.GetString(), ignoreCase: true, out var value) &&
+ Enum.IsDefined(value)
+ ? value
+ : reader.TokenType == JsonTokenType.Number && reader.TryGetInt32(out var number) &&
+ Enum.IsDefined((TEnum)Enum.ToObject(typeof(TEnum), number))
+ ? (TEnum)Enum.ToObject(typeof(TEnum), number)
+ : default;
+
+ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) =>
+ writer.WriteStringValue(value.ToString());
+}
+
/// <summary>TIFF compression schemes.</summary>
public enum TiffCompression
{
@@ -171,9 +191,11 @@ public sealed class ImageOptions
public ResizeMode Resize { get; set; } = ResizeMode.None;
/// <summary>Turns the image (after its EXIF orientation is applied).</summary>
+ [JsonConverter(typeof(UnknownAsNoneJsonConverter<ImageRotation>))]
public ImageRotation Rotation { get; set; }
/// <summary>Mirrors the image (after <see cref="Rotation"/>).</summary>
+ [JsonConverter(typeof(UnknownAsNoneJsonConverter<ImageFlip>))]
public ImageFlip Flip { get; set; }
/// <summary>Turns or mirrors the image: only engines that do (<see cref="Conversion.IImageTransformer"/>) can run it.</summary>
diff --git a/tests/Filee.Core.Tests/UserDataStoreTests.cs b/tests/Filee.Core.Tests/UserDataStoreTests.cs
index 6f74e43d..a0962593 100644
--- a/tests/Filee.Core.Tests/UserDataStoreTests.cs
+++ b/tests/Filee.Core.Tests/UserDataStoreTests.cs
@@ -293,6 +293,28 @@ public class UserDataStoreTests
Assert.Equal("rotate-left", presets[background + 1].Id);
}
+ [Fact]
+ public void A_rotation_from_a_newer_Filee_reads_as_none_and_the_other_presets_still_load()
+ {
+ using var dir = new TempDir();
+ var store = new UserDataStore(dir.Path);
+ store.Load();
+ store.SaveLibrary();
+ var path = Path.Combine(dir.Path, "presets.json");
+ var json = File.ReadAllText(path);
+ Assert.Contains("\"rotation\": \"Left90\"", json);
+ File.WriteAllText(path, json.Replace("\"rotation\": \"Left90\"", "\"rotation\": \"Transpose\"")
+ .Replace("\"flip\": \"Vertical\"", "\"flip\": 7"));
+
+ var reloaded = new UserDataStore(dir.Path);
+ reloaded.Load();
+
+ Assert.Equal(ImageRotation.None, reloaded.FindPreset("rotate-left")!.Image.Rotation);
+ Assert.Equal(ImageFlip.None, reloaded.FindPreset("flip-vertical")!.Image.Flip);
+ Assert.Equal(ImageRotation.Right90, reloaded.FindPreset("rotate-right")!.Image.Rotation); // the rest as saved
+ Assert.False(File.Exists(path + ".broken"));
+ }
+
[Fact]
public void Back_from_an_older_Filee_rotate_and_flip_work_again_and_keep_the_users_changes()
{