diff --git a/NAPS2.Lib.Tests/Scan/AutoSaverTests.cs b/NAPS2.Lib.Tests/Scan/AutoSaverTests.cs index 951b4d635c..d69f5c6861 100644 --- a/NAPS2.Lib.Tests/Scan/AutoSaverTests.cs +++ b/NAPS2.Lib.Tests/Scan/AutoSaverTests.cs @@ -199,6 +199,53 @@ public async Task PdfSplitPatchT() PdfAsserts.AssertImages(Path.Combine(FolderPath, "test2.pdf"), ImageResources.dog_h_n300); } + [Fact] + public async Task PromptForFilePath() + { + var settings = new AutoSaveSettings + { + FilePath = Path.Combine(FolderPath, "test_a_$(n).pdf"), + PromptForFilePath = true + }; + _dialogHelper.PromptToSavePdfOrImage(Arg.Any(), out Arg.Any()).Returns(x => + { + x[1] = Path.Combine(FolderPath, "test_b_$(n).pdf"); + return true; + }); + + var scanned = CreateScannedImages(ImageResources.dog); + var output = await _autoSaver.Save(settings, scanned.ToAsyncEnumerable()).ToListAsync(); + + Assert.Single(output); + Assert.False(IsDisposed(output[0])); + Assert.True(IsDisposed(scanned[0])); + Assert.Single(Folder.GetFiles()); + PdfAsserts.AssertImages(Path.Combine(FolderPath, "test_b_1.pdf"), ImageResources.dog); + } + + [Fact] + public async Task CancelPromptForFilePath() + { + var settings = new AutoSaveSettings + { + FilePath = Path.Combine(FolderPath, "test$(n).pdf"), + PromptForFilePath = true + }; + _dialogHelper.PromptToSavePdfOrImage(Arg.Any(), out Arg.Any()).Returns(x => + { + x[1] = Path.Combine(FolderPath, "test$(n).pdf"); + return false; + }); + + var scanned = CreateScannedImages(ImageResources.dog); + var output = await _autoSaver.Save(settings, scanned.ToAsyncEnumerable()).ToListAsync(); + + Assert.Single(output); + Assert.False(IsDisposed(output[0])); + Assert.True(IsDisposed(scanned[0])); + Assert.Empty(Folder.GetFiles()); + } + // TODO: Finish out tests // diff --git a/NAPS2.Lib/ImportExport/AutoSaver.cs b/NAPS2.Lib/ImportExport/AutoSaver.cs index deff610046..171acd72c7 100644 --- a/NAPS2.Lib/ImportExport/AutoSaver.cs +++ b/NAPS2.Lib/ImportExport/AutoSaver.cs @@ -117,13 +117,15 @@ private async Task InternalSave(AutoSaveSettings settings, List + string? newPath = null!; + if (Invoker.Current.InvokeGet(() => _dialogHelper.PromptToSavePdfOrImage(subPath, out newPath))) { - if (_dialogHelper.PromptToSavePdfOrImage(subPath, out string? newPath)) - { - subPath = placeholders.Substitute(newPath!, true, i); - } - }); + subPath = placeholders.Substitute(newPath!, true, i); + } + else + { + return (false, null); + } } // TODO: This placeholder handling is complex and wrong in some cases (e.g. FilePerScan with ext = "jpg") // TODO: Maybe have initial placeholders that replace date, then rely on the ops to increment the file num