From 2596f8e590f0d61db5f532a1d13f5d9167f3d4eb Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Wed, 6 Dec 2023 19:40:26 -0800 Subject: [PATCH] Ensure parent dir exists in PDFExporter #214, closes #216 --- NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs | 15 +++++++++++++++ NAPS2.Sdk.Tests/Pdf/PdfImportExportTests.cs | 15 +++++++++++++++ NAPS2.Sdk/Pdf/PdfExporter.cs | 2 ++ 3 files changed, 32 insertions(+) diff --git a/NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs b/NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs index 59f9f01465..0a658ce5e3 100644 --- a/NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs +++ b/NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs @@ -47,6 +47,21 @@ public async Task ExportJpegImageToStream(StorageConfig storageConfig) PdfAsserts.AssertImageFilter(filePath, 0, "DCTDecode"); } + [Theory] + [ClassData(typeof(StorageAwareTestData))] + public async Task ExportJpegImageToNonexistentFolder(StorageConfig storageConfig) + { + storageConfig.Apply(this); + + var filePath = Path.Combine(FolderPath, "blah", "test.pdf"); + using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog)); + + await _exporter.Export(filePath, new[] { image }); + + PdfAsserts.AssertImages(filePath, ImageResources.dog); + PdfAsserts.AssertImageFilter(filePath, 0, "DCTDecode"); + } + [Theory] [ClassData(typeof(StorageAwareTestData))] public async Task ExportGrayJpegImage(StorageConfig storageConfig) diff --git a/NAPS2.Sdk.Tests/Pdf/PdfImportExportTests.cs b/NAPS2.Sdk.Tests/Pdf/PdfImportExportTests.cs index 1e51cd5456..b296518846 100644 --- a/NAPS2.Sdk.Tests/Pdf/PdfImportExportTests.cs +++ b/NAPS2.Sdk.Tests/Pdf/PdfImportExportTests.cs @@ -52,6 +52,21 @@ public async Task ImportExportToStream(OcrTestConfig config) PdfAsserts.AssertImages(_exportPath, PdfResources.word_p1, PdfResources.word_p2); } + [Theory] + [ClassData(typeof(OcrTestData))] + public async Task ImportExportToNonexistentFolder(OcrTestConfig config) + { + config.StorageConfig.Apply(this); + SetUpFakeOcr(); + + var exportPath = Path.Combine(FolderPath, "blah", "export.pdf"); + var images = await _importer.Import(_importPath).ToListAsync(); + Assert.Equal(2, images.Count); + await _exporter.Export(exportPath, images, ocrParams: config.OcrParams); + + PdfAsserts.AssertImages(exportPath, PdfResources.word_p1, PdfResources.word_p2); + } + [Theory] [ClassData(typeof(OcrTestData))] public async Task ImportInsertExport(OcrTestConfig config) diff --git a/NAPS2.Sdk/Pdf/PdfExporter.cs b/NAPS2.Sdk/Pdf/PdfExporter.cs index f2662c8680..0dcee6137b 100644 --- a/NAPS2.Sdk/Pdf/PdfExporter.cs +++ b/NAPS2.Sdk/Pdf/PdfExporter.cs @@ -764,6 +764,7 @@ public void CopyFromStream(MemoryStream inputStream) } else { + FileSystemHelper.EnsureParentDirExists(Path!); using var fileStream = new FileStream(Path!, FileMode.Create); inputStream.CopyTo(fileStream); } @@ -777,6 +778,7 @@ public void SaveDoc(Pdfium.PdfDocument doc) } else { + FileSystemHelper.EnsureParentDirExists(Path!); doc.Save(Path!); } }