fix(pdfcpu): correct sorting for the output paths of the split method

This commit is contained in:
Julien Neuhart
2025-02-12 11:47:45 +01:00
parent 3dc8c18849
commit 80f3f89a89
7 changed files with 77 additions and 157 deletions

View File

@@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"syscall"
@@ -128,11 +129,25 @@ func (engine *PdfCpu) Split(ctx context.Context, logger *zap.Logger, mode gotenb
return nil, fmt.Errorf("split PDFs with pdfcpu: %w", err)
}
outputPaths, err := gotenberg.WalkDir(outputDirPath, ".pdf")
var outputPaths []string
err = filepath.Walk(outputDirPath, func(path string, info os.FileInfo, pathErr error) error {
if pathErr != nil {
return pathErr
}
if info.IsDir() {
return nil
}
if strings.EqualFold(filepath.Ext(info.Name()), ".pdf") {
outputPaths = append(outputPaths, path)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("walk directory to find resulting PDFs from split with pdfcpu: %w", err)
}
sort.Sort(gotenberg.AlphanumericSort(outputPaths))
return outputPaths, nil
}

View File

@@ -230,15 +230,15 @@ func TestPdfCpu_Split(t *testing.T) {
scenario: "success (intervals)",
ctx: context.TODO(),
mode: gotenberg.SplitMode{Mode: gotenberg.SplitModeIntervals, Span: "1"},
inputPath: "/tests/test/testdata/pdfengines/sample1.pdf",
inputPath: "/tests/test/testdata/pdfengines/sample4.pdf",
expectError: false,
expectOutputPathsCount: 3,
expectOutputPathsCount: 20,
},
{
scenario: "success (pages)",
ctx: context.TODO(),
mode: gotenberg.SplitMode{Mode: gotenberg.SplitModePages, Span: "1"},
inputPath: "/tests/test/testdata/pdfengines/sample1.pdf",
inputPath: "/tests/test/testdata/pdfengines/sample4.pdf",
expectError: false,
expectOutputPathsCount: 1,
},
@@ -246,7 +246,7 @@ func TestPdfCpu_Split(t *testing.T) {
scenario: "success (pages & unify)",
ctx: context.TODO(),
mode: gotenberg.SplitMode{Mode: gotenberg.SplitModePages, Span: "1-2", Unify: true},
inputPath: "/tests/test/testdata/pdfengines/sample1.pdf",
inputPath: "/tests/test/testdata/pdfengines/sample4.pdf",
expectError: false,
expectOutputPathsCount: 1,
},