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.

Merge #310 (subfolder only when a file becomes several) into fix/failed-outputs

KnifeLemonaccdcf92e72e
diff --cc src/Filee.Core/Conversion/JobQueue.cs
index 09d2b927,59939816..a5404653
--- a/src/Filee.Core/Conversion/JobQueue.cs
+++ b/src/Filee.Core/Conversion/JobQueue.cs
@@@ -297,8 -289,7 +297,8 @@@ public sealed class JobQueue : IAsyncDi
return;
var first = job.Files[0];
- var allocator = CreateFinalAllocator(job, first.SourcePath, 1);
- var output = CreateFinalAllocator(job, first.SourcePath, 1, oneFile: true).Allocate("tiff");
++ var allocator = CreateFinalAllocator(job, first.SourcePath, 1, oneFile: true);
+ var output = allocator.Allocate("tiff");
if (output is null)
{
foreach (var f in job.Files) f.State = FileState.Skipped;
@@@ -352,8 -335,7 +352,8 @@@
return;
}
- var allocator = CreateFinalAllocator(job, inputs[0].SourcePath, 1);
- var output = CreateFinalAllocator(job, inputs[0].SourcePath, 1, oneFile: true).Allocate(format.PrimaryExtension);
++ var allocator = CreateFinalAllocator(job, inputs[0].SourcePath, 1, oneFile: true);
+ var output = allocator.Allocate(format.PrimaryExtension);
if (output is null)
{
foreach (var f in inputs)
@@@ -514,7 -494,7 +514,8 @@@
Notify(job);
// A file saved from a browser first has part of its progress already.
var floor = file.Progress;
- // The last step's outputs: what a failed or cancelled conversion started there is undone.
- // The last step's allocator: how many paths it handed out tells a single output from several.
++ // The last step's allocator: how many paths it handed out tells a single output from several, and what a
++ // failed or cancelled conversion started there is undone through it.
FinalAllocator? final = null;
try
@@@ -570,9 -550,10 +571,12 @@@
if (zipResult && inputs.Count > 0)
inputs = PackZip(job, file, index, inputs, ct);
+ // One output, and nothing else handed out for it (a Markdown file's picture folder goes with it).
+ else if (finalOutput && inputs.Count == 1 && final is { Handed: 1 })
+ inputs = [NextToSource(job, file, index, inputs[0])];
+ // A name the engine took but wrote nothing to is free again.
+ final?.Release(except: inputs);
if (finalOutput)
{
file.Outputs.AddRange(inputs);
@@@ -637,8 -615,7 +641,8 @@@
/// </summary>
private IReadOnlyList<string> PackZip(ConversionJob job, FileResult file, int index, IReadOnlyList<string> files, CancellationToken ct)
{
- var allocator = CreateFinalAllocator(job, file.SourcePath, index);
- if (CreateFinalAllocator(job, file.SourcePath, index, oneFile: true).Allocate("zip") is not { } zipPath)
++ var allocator = CreateFinalAllocator(job, file.SourcePath, index, oneFile: true);
+ if (allocator.Allocate("zip") is not { } zipPath)
return [];
try
{
@@@ -748,9 -794,9 +822,12 @@@
/// <summary>Allocates final output paths according to the preset's <see cref="OutputRule"/>.</summary>
private sealed class FinalAllocator(JobQueue queue, OutputRule rule, OutputPathResolver.Tokens tokens) : IOutputAllocator
{
+ /// <summary>Every path handed out, and whether nothing was there before (not a file it overwrites).</summary>
+ private readonly List<(string Path, bool New)> _allocated = [];
+
+ /// <summary>How many paths were handed out: an output and the picture folder that goes with it are two.</summary>
- public int Handed { get; private set; }
++ public int Handed => _allocated.Count;
+
public string? Allocate(string extension, string? suffix = null)
{
// Reserve under a lock so two parallel files never pick the same name.
diff --cc tests/Filee.Core.Tests/JobQueueTests.cs
index 24a5a5e4,9f3dcdc7..6effd9af
--- a/tests/Filee.Core.Tests/JobQueueTests.cs
+++ b/tests/Filee.Core.Tests/JobQueueTests.cs
@@@ -537,101 -537,152 +537,247 @@@ public class JobQueueTest
Assert.Equal(2, job.Outputs.Distinct().Count());
}
+ /// <summary>An engine that writes half a file where it was told to and then fails, or is cancelled.</summary>
+ private sealed class HalfWriter(Action<ConversionStep>? whileWriting = null) : IConverter
+ {
+ public string Id => "half";
+ public string DisplayName => "half";
+ public IReadOnlyList<ConversionEdge> Edges { get; } = [new ConversionEdge("jpg", "png")];
+ public int MaxParallelism => 0;
+ public bool Writes { get; init; } = true;
+ public bool Available { get; set; } = true;
+ public EngineStatus GetStatus() => Available ? EngineStatus.Available() : EngineStatus.Unavailable("engine.reason.not_installed");
+
+ public Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken)
+ {
+ var path = step.Output.Allocate(step.To)!;
+ if (Writes)
+ File.WriteAllText(path, "half");
+ whileWriting?.Invoke(step);
+ cancellationToken.ThrowIfCancellationRequested();
+ throw new InvalidOperationException("the engine crashed");
+ }
+ }
+
+ [Fact]
+ public async Task A_failed_conversion_leaves_no_half_written_file_and_frees_its_name()
+ {
+ using var dir = new TempDir();
+ var photo = dir.File("photo.jpg");
+ await using (var failing = new JobQueue(new ConverterCatalog([new HalfWriter()])))
+ {
+ var job = await Run(failing, [photo], new Preset { TargetFormat = "png" });
+
+ Assert.Equal(JobState.Failed, job.State);
+ Assert.False(File.Exists(Path.Combine(dir.Path, "photo.png")));
+ }
+ }
+
+ [Fact]
+ public async Task A_cancelled_conversion_leaves_no_half_written_file_and_a_retry_gets_the_same_name()
+ {
+ using var dir = new TempDir();
+ var photo = dir.File("photo.jpg");
+ ConversionJob? running = null;
+ var half = new HalfWriter(_ => running!.Cancel());
+ var good = new FakeConverter("magick", new ConversionEdge("jpg", "png"));
+ var catalog = new ConverterCatalog([half, good]);
+ await using var queue = new JobQueue(catalog);
+
+ running = new ConversionJob([photo], new Preset { TargetFormat = "png" }, "Test");
+ good.Available = false;
+ catalog.Refresh();
+ await queue.RunAsync(running);
+ Assert.Equal(JobState.Cancelled, running.State);
+ Assert.False(File.Exists(Path.Combine(dir.Path, "photo.png")));
+
+ // Tried again in the same session: the name the cancelled one had is free again.
+ half.Available = false;
+ good.Available = true;
+ catalog.Refresh();
+ var retry = await Run(queue, [photo], new Preset { TargetFormat = "png" });
+ Assert.Equal([Path.Combine(dir.Path, "photo.png")], retry.Outputs);
+ }
+
+ [Fact]
+ public async Task A_name_a_failed_conversion_took_is_free_for_the_next_one()
+ {
+ using var dir = new TempDir();
+ var photo = dir.File("photo.jpg");
+ var half = new HalfWriter { Writes = false };
+ var good = new FakeConverter("magick", new ConversionEdge("jpg", "png")) { Available = false };
+ var catalog = new ConverterCatalog([half, good]);
+ await using var queue = new JobQueue(catalog);
+
+ Assert.Equal(JobState.Failed, (await Run(queue, [photo], new Preset { TargetFormat = "png" })).State);
+ half.Available = false;
+ good.Available = true;
+ catalog.Refresh();
+ var retry = await Run(queue, [photo], new Preset { TargetFormat = "png" });
+
+ Assert.Equal([Path.Combine(dir.Path, "photo.png")], retry.Outputs);
+ }
+
+ [Fact]
+ public async Task A_failed_conversion_never_deletes_a_file_it_was_to_overwrite()
+ {
+ using var dir = new TempDir();
+ var photo = dir.File("photo.jpg");
+ var older = dir.File("photo.png", "older");
+ await using var queue = new JobQueue(new ConverterCatalog([new HalfWriter { Writes = false }]));
+
+ var job = await Run(queue, [photo], new Preset { TargetFormat = "png", Output = { Conflict = ConflictPolicy.Overwrite } });
+
+ Assert.Equal(JobState.Failed, job.State);
+ Assert.Equal("older", File.ReadAllText(older));
+ }
+
+ [Fact]
+ public async Task Subfolder_only_for_several_files_keeps_a_single_output_next_to_its_source()
+ {
+ using var dir = new TempDir();
+ var photo = dir.File("photo.jpg");
+ var report = dir.File("report.pdf");
+ var catalog = new ConverterCatalog([
+ new FakeConverter("magick", new ConversionEdge("jpg", "png")),
+ new FakeConverter("pdfium", new ConversionEdge("pdf", "png")) { OutputsPerInput = 3 },
+ ]);
+ await using var queue = new JobQueue(catalog);
+ var preset = new Preset
+ {
+ TargetFormat = "png",
+ Output = { Location = OutputLocation.Subfolder, SubfolderName = "{name}", SubfolderOnlyForSeveralFiles = true },
+ };
+
+ var job = await Run(queue, [photo, report], preset);
+
+ Assert.Equal(JobState.Completed, job.State);
+ Assert.Equal([Path.Combine(dir.Path, "photo.png")], job.Files[0].Outputs);
+ Assert.False(Directory.Exists(Path.Combine(dir.Path, "photo")));
+ Assert.Equal(3, job.Files[1].Outputs.Count);
+ Assert.All(job.Files[1].Outputs, o => Assert.Equal(Path.Combine(dir.Path, "report"), Path.GetDirectoryName(o)));
+ Assert.All(job.Outputs, o => Assert.True(File.Exists(o)));
+ }
+
+ [Fact]
+ public async Task Subfolder_only_for_several_files_leaves_a_shared_subfolder_to_the_files_still_in_it()
+ {
+ using var dir = new TempDir();
+ var catalog = new ConverterCatalog([
+ new FakeConverter("magick", new ConversionEdge("jpg", "png")),
+ new FakeConverter("pdfium", new ConversionEdge("pdf", "png")) { OutputsPerInput = 2 },
+ ]);
+ await using var queue = new JobQueue(catalog);
+ var preset = new Preset
+ {
+ TargetFormat = "png",
+ Output = { Location = OutputLocation.Subfolder, SubfolderName = "converted", SubfolderOnlyForSeveralFiles = true },
+ };
+
+ var job = await Run(queue, [dir.File("a.jpg"), dir.File("b.pdf"), dir.File("c.jpg")], preset);
+
+ Assert.Equal(JobState.Completed, job.State);
+ Assert.True(File.Exists(Path.Combine(dir.Path, "a.png")));
+ Assert.True(File.Exists(Path.Combine(dir.Path, "c.png")));
+ Assert.True(File.Exists(Path.Combine(dir.Path, "converted", "b_p1.png")));
+ Assert.True(File.Exists(Path.Combine(dir.Path, "converted", "b_p2.png")));
+ }
+
+ [Fact]
+ public async Task Subfolder_only_for_several_files_applies_the_conflict_policy_next_to_the_source()
+ {
+ using var dir = new TempDir();
+ var photo = dir.File("photo.jpg");
+ dir.File("photo.png", "older");
+ await using var queue = new JobQueue(new ConverterCatalog([new FakeConverter("magick", new ConversionEdge("jpg", "png"))]));
+ var preset = new Preset
+ {
+ TargetFormat = "png",
+ Output = { Location = OutputLocation.Subfolder, SubfolderName = "{name}", SubfolderOnlyForSeveralFiles = true },
+ };
+
+ var job = await Run(queue, [photo], preset);
+
+ Assert.Equal([Path.Combine(dir.Path, "photo (2).png")], job.Outputs);
+ Assert.Equal("older", File.ReadAllText(Path.Combine(dir.Path, "photo.png")));
+ Assert.False(Directory.Exists(Path.Combine(dir.Path, "photo")));
+ }
+
+ [Fact]
+ public async Task Subfolder_only_for_several_files_saves_a_merged_pdf_next_to_the_first_file()
+ {
+ using var dir = new TempDir();
+ var catalog = new ConverterCatalog([new FakeConverter("magick", new ConversionEdge("jpg", "pdf"))]);
+ await using var queue = new JobQueue(catalog, pdfMerger: new FakeMerger());
+ var preset = new Preset
+ {
+ TargetFormat = "pdf",
+ Pdf = { MergeIntoSingle = true },
+ Output = { Location = OutputLocation.Subfolder, SubfolderName = "{name}", SubfolderOnlyForSeveralFiles = true },
+ };
+
+ var job = await Run(queue, [dir.File("a.jpg"), dir.File("b.jpg")], preset);
+
+ Assert.Equal([Path.Combine(dir.Path, "a.pdf")], job.Outputs);
+ Assert.False(Directory.Exists(Path.Combine(dir.Path, "a")));
+ }
+
+ [Theory]
+ [InlineData(true)]
+ [InlineData(false)]
+ public async Task Subfolder_only_for_several_files_keeps_a_markdown_file_with_its_picture_folder(bool reserved)
+ {
+ using var dir = new TempDir();
+ var converter = new FakeConverter("pandoc", new ConversionEdge("docx", "md")) { PictureFolder = true, ReservesPictureFolder = reserved };
+ await using var queue = new JobQueue(new ConverterCatalog([converter]));
+ var preset = new Preset
+ {
+ TargetFormat = "md",
+ Output = { Location = OutputLocation.Subfolder, SubfolderName = "{name}", SubfolderOnlyForSeveralFiles = true },
+ };
+
+ var job = await Run(queue, [dir.File("report.docx")], preset);
+
+ // Its links point into the folder beside it: the two stay together in the subfolder.
+ Assert.Equal([Path.Combine(dir.Path, "report", "report.md")], job.Outputs);
+ Assert.True(File.Exists(Path.Combine(dir.Path, "report", "report_files", "img0001.png")));
+ }
+
+ [Fact]
+ public async Task Subfolder_only_for_several_files_leaves_the_file_in_the_subfolder_when_skip_meets_an_existing_one()
+ {
+ using var dir = new TempDir();
+ var photo = dir.File("photo.jpg");
+ dir.File("photo.png", "older");
+ await using var queue = new JobQueue(new ConverterCatalog([new FakeConverter("magick", new ConversionEdge("jpg", "png"))]));
+ var preset = new Preset
+ {
+ TargetFormat = "png",
+ Output =
+ {
+ Location = OutputLocation.Subfolder, SubfolderName = "{name}", SubfolderOnlyForSeveralFiles = true,
+ Conflict = ConflictPolicy.Skip,
+ },
+ };
+
+ var job = await Run(queue, [photo], preset);
+
+ Assert.Equal([Path.Combine(dir.Path, "photo", "photo.png")], job.Outputs);
+ Assert.Equal("older", File.ReadAllText(Path.Combine(dir.Path, "photo.png")));
+ }
+
+ [Fact]
+ public async Task Without_subfolder_only_for_several_files_a_single_output_stays_in_the_subfolder()
+ {
+ using var dir = new TempDir();
+ await using var queue = new JobQueue(new ConverterCatalog([new FakeConverter("magick", new ConversionEdge("jpg", "png"))]));
+ var preset = new Preset { TargetFormat = "png", Output = { Location = OutputLocation.Subfolder, SubfolderName = "{name}" } };
+
+ var job = await Run(queue, [dir.File("photo.jpg")], preset);
+
+ Assert.Equal([Path.Combine(dir.Path, "photo", "photo.png")], job.Outputs);
+ }
+
[Fact]
public async Task Enqueue_runs_in_background_and_raises_finished()
{