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: merging PDFs or joining TIFF pages never passes a file through unturned

SeoJCalive7e835b20d1d6
diff --git a/src/Filee.App/Services/PresetAvailability.cs b/src/Filee.App/Services/PresetAvailability.cs
index 6b0206ba..d3ffc447 100644
--- a/src/Filee.App/Services/PresetAvailability.cs
+++ b/src/Filee.App/Services/PresetAvailability.cs
@@ -29,20 +29,27 @@ public sealed class PresetAvailability(ConverterCatalog catalog, ILocalizer loc,
var merge = preset.Pdf.MergeIntoSingle && preset.TargetFormat == "pdf";
var turns = preset.Image.TurnsOrMirrors;
string TargetFor(FileFormat f) => preset.TargetFormat == BuiltInData.SameAsSource ? f.Id : preset.TargetFormat;
+ var copiesUnturned = false;
bool Possible(RoutePlanner planner) => formats.Any(f =>
{
var target = TargetFor(f);
- if (merge && f.Id == "pdf")
- return true;
+ if (merge && f.Id == "pdf" && !turns)
+ return true; // merged as it is
+ if (!turns)
+ return planner.CanConvert(f.Id, target);
// Rotate / flip: a route only counts when an engine on it turns images (not a PDF or a video copied).
- return turns ? planner.Plan(f.Id, target)?.FirstTransformStep() >= 0 : planner.CanConvert(f.Id, target);
+ if (planner.Plan(f.Id, target) is not { } route)
+ return false;
+ if (route.FirstTransformStep() >= 0)
+ return true;
+ copiesUnturned = true;
+ return false;
});
- var planner = catalog.CreatePlanner();
- if (Possible(planner))
+ if (Possible(catalog.CreatePlanner()))
return null;
// It converts these files, just without turning them: say that, not "no engine" or "download one".
- if (turns && formats.Any(f => planner.CanConvert(f.Id, TargetFor(f))))
+ if (copiesUnturned)
return loc["error.cannot_turn"];
return downloads.PackageEnabling(Possible) is { } package
? loc.Format("engine.reason.needs_package", package.Name)
diff --git a/src/Filee.Core/Conversion/JobQueue.cs b/src/Filee.Core/Conversion/JobQueue.cs
index e2b02354..8753f790 100644
--- a/src/Filee.Core/Conversion/JobQueue.cs
+++ b/src/Filee.Core/Conversion/JobQueue.cs
@@ -123,7 +123,11 @@ public sealed class JobQueue : IAsyncDisposable
var file = job.Files[i];
if (FormatRegistry.Detect(file.SourcePath)?.Id == "pdf")
{
- parts[i] = file.SourcePath; // already a PDF, merge as-is
+ // Already a PDF, merged as it is: its pages can't be turned (only images going into it can).
+ if (job.Preset.Image.TurnsOrMirrors)
+ FailUnturned(job, file, "pdf → pdf");
+ else
+ parts[i] = file.SourcePath;
return;
}
var produced = await RunFileAsync(job, file, i + 1, planner, workDir, finalOutput: false);
@@ -165,7 +169,8 @@ public sealed class JobQueue : IAsyncDisposable
async (i, _) =>
{
var file = job.Files[i];
- parts[i] = FormatRegistry.Detect(file.SourcePath)?.Id == "tiff"
+ // A TIFF joins as it is, unless it is to be turned: then it goes through the engine like the rest.
+ parts[i] = FormatRegistry.Detect(file.SourcePath)?.Id == "tiff" && !job.Preset.Image.TurnsOrMirrors
? [file.SourcePath]
: await RunFileAsync(job, file, i + 1, planner, workDir, finalOutput: false, intermediate: "tiff");
});
@@ -192,6 +197,16 @@ public sealed class JobQueue : IAsyncDisposable
Notify(job);
}
+ /// <summary>A file that would be used as it is although the preset turns or mirrors it.</summary>
+ private void FailUnturned(ConversionJob job, FileResult file, string detail)
+ {
+ file.State = FileState.Failed;
+ file.ErrorKey = "error.cannot_turn";
+ file.ErrorDetail = detail;
+ file.Progress = 1;
+ Notify(job);
+ }
+
/// <summary>Packs every source file, unchanged, into one archive named after the first file.</summary>
private async Task RunCombinedAsync(ConversionJob job, string workDir)
{
@@ -325,7 +340,7 @@ public sealed class JobQueue : IAsyncDisposable
var turnAt = job.Preset.Image.TurnsOrMirrors ? route.FirstTransformStep() : -1;
if (job.Preset.Image.TurnsOrMirrors && turnAt < 0)
return Fail(file, "error.cannot_turn", $"{source.Id} → {target}");
- var turned = turnAt < 0 ? job.Preset : Unturned(job.Preset);
+ var unturned = turnAt < 0 ? job.Preset : Unturned(job.Preset);
file.State = FileState.Running;
Notify(job);
@@ -360,7 +375,8 @@ public sealed class JobQueue : IAsyncDisposable
await gate.WaitAsync(ct);
try
{
- var preset = turnAt >= 0 && s > turnAt ? turned : job.Preset; // turned once, in step turnAt
+ // Turned once, in step turnAt: the steps after it get the preset without rotation and flip.
+ var preset = turnAt >= 0 && s > turnAt ? unturned : job.Preset;
var stepInfo = new ConversionStep(input, step.From, step.To, preset, allocator, workDir);
outputs.AddRange(await step.Converter.ConvertAsync(stepInfo, progress, ct));
}
diff --git a/tests/Filee.App.Tests/DonutGroupTests.cs b/tests/Filee.App.Tests/DonutGroupTests.cs
index 9f8030f4..84cebfdd 100644
--- a/tests/Filee.App.Tests/DonutGroupTests.cs
+++ b/tests/Filee.App.Tests/DonutGroupTests.cs
@@ -283,6 +283,11 @@ public class DonutGroupTests
Assert.Null(availability.Check(rotate, [Filee.Core.Formats.FormatRegistry.Get("png")]));
// PDF → PDF exists (split, extract), but it copies pages unturned: not "no engine", and no download offered.
Assert.Equal("Rotate and flip work on images only", availability.Check(rotate, [Filee.Core.Formats.FormatRegistry.Get("pdf")]));
+ // Merging PDFs takes them as they are: turning them is not on offer either.
+ var mergeTurned = rotate.Clone();
+ mergeTurned.TargetFormat = "pdf";
+ mergeTurned.Pdf.MergeIntoSingle = true;
+ Assert.Equal("Rotate and flip work on images only", availability.Check(mergeTurned, [Filee.Core.Formats.FormatRegistry.Get("pdf")]));
}
[AvaloniaFact]
diff --git a/tests/Filee.Core.Tests/JobQueueTests.cs b/tests/Filee.Core.Tests/JobQueueTests.cs
index 0edc7fbb..12f1e219 100644
--- a/tests/Filee.Core.Tests/JobQueueTests.cs
+++ b/tests/Filee.Core.Tests/JobQueueTests.cs
@@ -197,6 +197,29 @@ public class JobQueueTests
Assert.Equal(fax, merger.Inputs[^1]);
}
+ [Fact]
+ public async Task Merging_or_joining_pages_with_a_rotation_never_passes_a_file_through_unturned()
+ {
+ using var dir = new TempDir();
+ var photo = dir.File("photo.jpg");
+ var doc = dir.File("doc.pdf");
+ var fax = dir.File("fax.tiff");
+ var turner = new FakeTurner("magick", new ConversionEdge("jpg", "pdf"), new ConversionEdge("jpg", "tiff"), new ConversionEdge("tiff", "tiff"));
+ var pdfMerger = new FakeMerger();
+ var tiffMerger = new FakeTiffMerger();
+ await using var queue = new JobQueue(new ConverterCatalog([turner]), pdfMerger, tiffMerger: tiffMerger);
+
+ // A PDF can't be turned: merged unturned it would say "done"; it fails, the photo is still merged.
+ var merged = await Run(queue, [photo, doc], new Preset { TargetFormat = "pdf", Pdf = { MergeIntoSingle = true }, Image = { Rotation = ImageRotation.Right90 } });
+ Assert.Equal("error.cannot_turn", merged.Files[1].ErrorKey);
+ Assert.Equal(["photo.pdf"], pdfMerger.Inputs.Select(Path.GetFileName));
+
+ // A TIFF is turned like the photo before the pages are joined, not added as it is.
+ await Run(queue, [photo, fax], new Preset { TargetFormat = "tiff", Image = { MultiPageTiff = true, Rotation = ImageRotation.Right90 } });
+ Assert.DoesNotContain(fax, tiffMerger.Inputs);
+ Assert.Contains(("tiff", ImageRotation.Right90), turner.Asked);
+ }
+
[Fact]
public async Task Combine_packs_every_file_as_is_even_one_or_of_unknown_type()
{