From 1b49b35365fdeab773de47261ce1cb5d9f837e2e Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Thu, 21 Nov 2024 09:55:10 +0100 Subject: [PATCH] fix(exiftool): convert interface{} array to string array --- pkg/modules/exiftool/exiftool.go | 14 +++++++++++- pkg/modules/exiftool/exiftool_test.go | 33 ++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/pkg/modules/exiftool/exiftool.go b/pkg/modules/exiftool/exiftool.go index 13ed02b8..7d2cb8d9 100644 --- a/pkg/modules/exiftool/exiftool.go +++ b/pkg/modules/exiftool/exiftool.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "reflect" "github.com/barasher/go-exiftool" "go.uber.org/zap" @@ -109,6 +110,17 @@ func (engine *ExifTool) WriteMetadata(ctx context.Context, logger *zap.Logger, m fileMetadata[0].SetString(key, val) case []string: fileMetadata[0].SetStrings(key, val) + case []interface{}: + // See https://github.com/gotenberg/gotenberg/issues/1048. + strings := make([]string, len(val)) + for i, entry := range val { + if str, ok := entry.(string); ok { + strings[i] = str + continue + } + return fmt.Errorf("write PDF metadata with ExifTool: %s %+v %s %w", key, val, reflect.TypeOf(val), gotenberg.ErrPdfEngineMetadataValueNotSupported) + } + fileMetadata[0].SetStrings(key, strings) case bool: fileMetadata[0].SetString(key, fmt.Sprintf("%t", val)) case int: @@ -122,7 +134,7 @@ func (engine *ExifTool) WriteMetadata(ctx context.Context, logger *zap.Logger, m // TODO: support more complex cases, e.g., arrays and nested objects // (limitations in underlying library). default: - return fmt.Errorf("write PDF metadata with ExifTool: %w", gotenberg.ErrPdfEngineMetadataValueNotSupported) + return fmt.Errorf("write PDF metadata with ExifTool: %s %+v %s %w", key, val, reflect.TypeOf(val), gotenberg.ErrPdfEngineMetadataValueNotSupported) } } diff --git a/pkg/modules/exiftool/exiftool_test.go b/pkg/modules/exiftool/exiftool_test.go index 96f3eeab..949087ec 100644 --- a/pkg/modules/exiftool/exiftool_test.go +++ b/pkg/modules/exiftool/exiftool_test.go @@ -162,7 +162,20 @@ func TestExiftool_WriteMetadata(t *testing.T) { expectError: true, }, { - scenario: "gotenberg.ErrPdfEngineMetadataValueNotSupported", + scenario: "gotenberg.ErrPdfEngineMetadataValueNotSupported (not string array)", + createCopy: true, + inputPath: "/tests/test/testdata/pdfengines/sample1.pdf", + metadata: map[string]interface{}{ + "Unsupported": []interface{}{ + "foo", + 1, + }, + }, + expectError: true, + expectedError: gotenberg.ErrPdfEngineMetadataValueNotSupported, + }, + { + scenario: "gotenberg.ErrPdfEngineMetadataValueNotSupported (default)", createCopy: true, inputPath: "/tests/test/testdata/pdfengines/sample1.pdf", metadata: map[string]interface{}{ @@ -171,6 +184,24 @@ func TestExiftool_WriteMetadata(t *testing.T) { expectError: true, expectedError: gotenberg.ErrPdfEngineMetadataValueNotSupported, }, + { + scenario: "success (interface array to string array)", + createCopy: true, + inputPath: "/tests/test/testdata/pdfengines/sample1.pdf", + metadata: map[string]interface{}{ + "Keywords": []interface{}{ + "first", + "second", + }, + }, + expectMetadata: map[string]interface{}{ + "Keywords": []interface{}{ + "first", + "second", + }, + }, + expectError: false, + }, { scenario: "success", createCopy: true,