From 448e8e80ad07b29497bd1a8fe0992d3293bc88ae Mon Sep 17 00:00:00 2001 From: Peter Chakalov Date: Mon, 27 Jan 2025 12:50:37 +0200 Subject: [PATCH] Add flatten option to the merge route --- pkg/modules/pdfengines/routes.go | 9 +++++++ pkg/modules/pdfengines/routes_test.go | 39 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/pkg/modules/pdfengines/routes.go b/pkg/modules/pdfengines/routes.go index 04322f9b..1c592ef2 100644 --- a/pkg/modules/pdfengines/routes.go +++ b/pkg/modules/pdfengines/routes.go @@ -273,8 +273,10 @@ func mergeRoute(engine gotenberg.PdfEngine) api.Route { metadata := FormDataPdfMetadata(form, false) var inputPaths []string + var flatten bool err := form. MandatoryPaths([]string{".pdf"}, &inputPaths). + Bool("flatten", &flatten, false). Validate() if err != nil { return fmt.Errorf("validate form data: %w", err) @@ -296,6 +298,13 @@ func mergeRoute(engine gotenberg.PdfEngine) api.Route { return fmt.Errorf("write metadata: %w", err) } + if flatten { + outputPaths, err = FlattenStub(ctx, engine, outputPaths) + if err != nil { + return fmt.Errorf("flatten PDFs: %w", err) + } + } + err = ctx.AddOutputPaths(outputPaths...) if err != nil { return fmt.Errorf("add output paths: %w", err) diff --git a/pkg/modules/pdfengines/routes_test.go b/pkg/modules/pdfengines/routes_test.go index a0b004fb..a0269bbb 100644 --- a/pkg/modules/pdfengines/routes_test.go +++ b/pkg/modules/pdfengines/routes_test.go @@ -719,6 +719,39 @@ func TestMergeHandler(t *testing.T) { expectHttpError: false, expectOutputPathsCount: 0, }, + { + scenario: "PDF engine flatten error", + ctx: func() *api.ContextMock { + ctx := &api.ContextMock{Context: new(api.Context)} + ctx.SetFiles(map[string]string{ + "file.pdf": "/file.pdf", + "file2.pdf": "/file2.pdf", + }) + ctx.SetValues(map[string][]string{ + "metadata": { + "{\"Creator\": \"foo\", \"Producer\": \"bar\" }", + }, + "flatten": { + "true", + }, + }) + return ctx + }(), + engine: &gotenberg.PdfEngineMock{ + MergeMock: func(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error { + return nil + }, + WriteMetadataMock: func(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error { + return nil + }, + FlattenMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string) error { + return errors.New("foo") + }, + }, + expectError: true, + expectHttpError: false, + expectOutputPathsCount: 0, + }, { scenario: "cannot add output paths", ctx: func() *api.ContextMock { @@ -754,6 +787,9 @@ func TestMergeHandler(t *testing.T) { "metadata": { "{\"Creator\": \"foo\", \"Producer\": \"bar\" }", }, + "flatten": { + "true", + }, }) return ctx }(), @@ -767,6 +803,9 @@ func TestMergeHandler(t *testing.T) { WriteMetadataMock: func(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error { return nil }, + FlattenMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string) error { + return nil + }, }, expectError: false, expectHttpError: false,