Skip to content

Commit

Permalink
Ensure parent dir exists in PDFExporter
Browse files Browse the repository at this point in the history
#214, closes #216
  • Loading branch information
cyanfish committed Dec 7, 2023
1 parent e431157 commit 2596f8e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions NAPS2.Sdk.Tests/Pdf/PdfImportExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Pdf/PdfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ public void CopyFromStream(MemoryStream inputStream)
}
else
{
FileSystemHelper.EnsureParentDirExists(Path!);
using var fileStream = new FileStream(Path!, FileMode.Create);
inputStream.CopyTo(fileStream);
}
Expand All @@ -777,6 +778,7 @@ public void SaveDoc(Pdfium.PdfDocument doc)
}
else
{
FileSystemHelper.EnsureParentDirExists(Path!);
doc.Save(Path!);
}
}
Expand Down

0 comments on commit 2596f8e

Please sign in to comment.