feat(pdfengines): preserve filenames in convert route

This commit is contained in:
Julien Neuhart
2024-02-18 18:17:12 +01:00
parent f906ed741d
commit 376a7db545
3 changed files with 20 additions and 8 deletions

View File

@@ -23,10 +23,10 @@ func TestConvertRoute(t *testing.T) {
engine gotenberg.PdfEngine engine gotenberg.PdfEngine
expectOptions libreofficeapi.Options expectOptions libreofficeapi.Options
expectError bool expectError bool
expectOutpoutPaths []string
expectHttpError bool expectHttpError bool
expectHttpStatus int expectHttpStatus int
expectOutputPathsCount int expectOutputPathsCount int
expectOutputPaths []string
}{ }{
{ {
scenario: "missing at least one mandatory file", scenario: "missing at least one mandatory file",
@@ -277,7 +277,7 @@ func TestConvertRoute(t *testing.T) {
expectError: false, expectError: false,
expectHttpError: false, expectHttpError: false,
expectOutputPathsCount: 1, expectOutputPathsCount: 1,
expectOutpoutPaths: []string{"/document.docx.pdf"}, expectOutputPaths: []string{"/document.docx.pdf"},
}, },
{ {
scenario: "success (many files)", scenario: "success (many files)",
@@ -301,7 +301,7 @@ func TestConvertRoute(t *testing.T) {
expectError: false, expectError: false,
expectHttpError: false, expectHttpError: false,
expectOutputPathsCount: 3, expectOutputPathsCount: 3,
expectOutpoutPaths: []string{"/document.docx.pdf", "/document2.docx.pdf", "/document2.doc.pdf"}, expectOutputPaths: []string{"/document.docx.pdf", "/document2.docx.pdf", "/document2.doc.pdf"},
}, },
{ {
scenario: "success with non-native PDF/A & PDF/UA (many files)", scenario: "success with non-native PDF/A & PDF/UA (many files)",
@@ -340,7 +340,7 @@ func TestConvertRoute(t *testing.T) {
expectError: false, expectError: false,
expectHttpError: false, expectHttpError: false,
expectOutputPathsCount: 2, expectOutputPathsCount: 2,
expectOutpoutPaths: []string{"/document.docx.pdf", "/document2.docx.pdf"}, expectOutputPaths: []string{"/document.docx.pdf", "/document2.docx.pdf"},
}, },
{ {
scenario: "success with native PDF/A & PDF/UA (many files)", scenario: "success with native PDF/A & PDF/UA (many files)",
@@ -376,7 +376,7 @@ func TestConvertRoute(t *testing.T) {
expectError: false, expectError: false,
expectHttpError: false, expectHttpError: false,
expectOutputPathsCount: 2, expectOutputPathsCount: 2,
expectOutpoutPaths: []string{"/document.docx.pdf", "/document2.docx.pdf"}, expectOutputPaths: []string{"/document.docx.pdf", "/document2.docx.pdf"},
}, },
{ {
scenario: "ErrMaximumQueueSizeExceeded (merge)", scenario: "ErrMaximumQueueSizeExceeded (merge)",
@@ -759,7 +759,7 @@ func TestConvertRoute(t *testing.T) {
t.Errorf("expected %d output paths but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths())) t.Errorf("expected %d output paths but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths()))
} }
for _, path := range tc.expectOutpoutPaths { for _, path := range tc.expectOutputPaths {
if !slices.Contains(tc.ctx.OutputPaths(), path) { if !slices.Contains(tc.ctx.OutputPaths(), path) {
t.Errorf("expected '%s' in output paths %v", path, tc.ctx.OutputPaths()) t.Errorf("expected '%s' in output paths %v", path, tc.ctx.OutputPaths())
} }

View File

@@ -4,6 +4,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"path/filepath"
"strings"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@@ -152,11 +154,11 @@ func convertRoute(engine gotenberg.PdfEngine) api.Route {
) )
} }
// Alright, let's convert the PDFs. // Alright, let's convert the PDFs.s
outputPaths := make([]string, len(inputPaths)) outputPaths := make([]string, len(inputPaths))
for i, inputPath := range inputPaths { for i, inputPath := range inputPaths {
outputPaths[i] = ctx.GeneratePath("", ".pdf") outputPaths[i] = ctx.GeneratePath(strings.TrimSuffix(filepath.Base(inputPath), filepath.Ext(inputPath)), ".pdf")
err = engine.Convert(ctx, ctx.Log(), pdfFormats, inputPath, outputPaths[i]) err = engine.Convert(ctx, ctx.Log(), pdfFormats, inputPath, outputPaths[i])
if err != nil { if err != nil {

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"net/http" "net/http"
"slices"
"testing" "testing"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@@ -273,6 +274,7 @@ func TestConvertHandler(t *testing.T) {
expectHttpError bool expectHttpError bool
expectHttpStatus int expectHttpStatus int
expectOutputPathsCount int expectOutputPathsCount int
expectOutputPaths []string
}{ }{
{ {
scenario: "missing at least one mandatory file", scenario: "missing at least one mandatory file",
@@ -416,6 +418,7 @@ func TestConvertHandler(t *testing.T) {
expectError: false, expectError: false,
expectHttpError: false, expectHttpError: false,
expectOutputPathsCount: 1, expectOutputPathsCount: 1,
expectOutputPaths: []string{"/file.pdf"},
}, },
{ {
scenario: "success with PDF/A & PDF/UA form fields (many files)", scenario: "success with PDF/A & PDF/UA form fields (many files)",
@@ -443,6 +446,7 @@ func TestConvertHandler(t *testing.T) {
expectError: false, expectError: false,
expectHttpError: false, expectHttpError: false,
expectOutputPathsCount: 2, expectOutputPathsCount: 2,
expectOutputPaths: []string{"/file.pdf", "/file2.pdf"},
}, },
} { } {
t.Run(tc.scenario, func(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) {
@@ -481,6 +485,12 @@ func TestConvertHandler(t *testing.T) {
if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) { if tc.expectOutputPathsCount != len(tc.ctx.OutputPaths()) {
t.Errorf("expected %d output paths but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths())) t.Errorf("expected %d output paths but got %d", tc.expectOutputPathsCount, len(tc.ctx.OutputPaths()))
} }
for _, path := range tc.expectOutputPaths {
if !slices.Contains(tc.ctx.OutputPaths(), path) {
t.Errorf("expected '%s' in output paths %v", path, tc.ctx.OutputPaths())
}
}
}) })
} }
} }