mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-18 05:02:15 +01:00
fix(libreoffice): wrong HTTP status codes if invalid PDF formats
This commit is contained in:
@@ -21,9 +21,9 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrInvalidPdfFormat happens if the PDF format option cannot be handled
|
// ErrInvalidPdfFormats happens if the PDF formats option cannot be handled
|
||||||
// by LibreOffice.
|
// by LibreOffice.
|
||||||
ErrInvalidPdfFormat = errors.New("invalid PDF format")
|
ErrInvalidPdfFormats = errors.New("invalid PDF formats")
|
||||||
|
|
||||||
// ErrMalformedPageRanges happens if the page ranges option cannot be
|
// ErrMalformedPageRanges happens if the page ranges option cannot be
|
||||||
// interpreted by LibreOffice.
|
// interpreted by LibreOffice.
|
||||||
|
|||||||
@@ -282,7 +282,7 @@ func (p *libreOfficeProcess) pdf(ctx context.Context, logger *zap.Logger, inputP
|
|||||||
case gotenberg.PdfA3b:
|
case gotenberg.PdfA3b:
|
||||||
args = append(args, "--export", "SelectPdfVersion=3")
|
args = append(args, "--export", "SelectPdfVersion=3")
|
||||||
default:
|
default:
|
||||||
return ErrInvalidPdfFormat
|
return ErrInvalidPdfFormats
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.PdfFormats.PdfUa {
|
if options.PdfFormats.PdfUa {
|
||||||
|
|||||||
@@ -235,7 +235,7 @@ func TestLibreOfficeProcess_pdf(t *testing.T) {
|
|||||||
expectError: true,
|
expectError: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scenario: "ErrInvalidPdfFormat",
|
scenario: "ErrInvalidPdfFormats",
|
||||||
libreOffice: func() libreOffice {
|
libreOffice: func() libreOffice {
|
||||||
p := new(libreOfficeProcess)
|
p := new(libreOfficeProcess)
|
||||||
p.socketPort = 12345
|
p.socketPort = 12345
|
||||||
@@ -247,7 +247,7 @@ func TestLibreOfficeProcess_pdf(t *testing.T) {
|
|||||||
cancelledCtx: false,
|
cancelledCtx: false,
|
||||||
start: false,
|
start: false,
|
||||||
expectError: true,
|
expectError: true,
|
||||||
expectedError: ErrInvalidPdfFormat,
|
expectedError: ErrInvalidPdfFormats,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scenario: "ErrMalformedPageRanges",
|
scenario: "ErrMalformedPageRanges",
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func (engine *LibreOfficePdfEngine) Convert(ctx context.Context, logger *zap.Log
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if errors.Is(err, api.ErrInvalidPdfFormat) {
|
if errors.Is(err, api.ErrInvalidPdfFormats) {
|
||||||
return fmt.Errorf("convert PDF to '%+v' with LibreOffice: %w", formats, gotenberg.ErrPdfFormatNotSupported)
|
return fmt.Errorf("convert PDF to '%+v' with LibreOffice: %w", formats, gotenberg.ErrPdfFormatNotSupported)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ func TestLibreOfficePdfEngine_Convert(t *testing.T) {
|
|||||||
scenario: "invalid PDF format",
|
scenario: "invalid PDF format",
|
||||||
api: &api.ApiMock{
|
api: &api.ApiMock{
|
||||||
PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options api.Options) error {
|
PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options api.Options) error {
|
||||||
return api.ErrInvalidPdfFormat
|
return api.ErrInvalidPdfFormats
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
|||||||
@@ -67,6 +67,16 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap
|
|||||||
|
|
||||||
err = libreOffice.Pdf(ctx, ctx.Log(), inputPath, outputPaths[i], options)
|
err = libreOffice.Pdf(ctx, ctx.Log(), inputPath, outputPaths[i], options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, libreofficeapi.ErrInvalidPdfFormats) {
|
||||||
|
return api.WrapError(
|
||||||
|
fmt.Errorf("convert to PDF: %w", err),
|
||||||
|
api.NewSentinelHttpError(
|
||||||
|
http.StatusBadRequest,
|
||||||
|
fmt.Sprintf("A PDF format in '%+v' is not supported", pdfFormats),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if errors.Is(err, libreofficeapi.ErrMalformedPageRanges) {
|
if errors.Is(err, libreofficeapi.ErrMalformedPageRanges) {
|
||||||
return api.WrapError(
|
return api.WrapError(
|
||||||
fmt.Errorf("convert to PDF: %w", err),
|
fmt.Errorf("convert to PDF: %w", err),
|
||||||
|
|||||||
@@ -37,6 +37,28 @@ func TestConvertRoute(t *testing.T) {
|
|||||||
expectHttpStatus: http.StatusBadRequest,
|
expectHttpStatus: http.StatusBadRequest,
|
||||||
expectOutputPathsCount: 0,
|
expectOutputPathsCount: 0,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "ErrPdfFormatNotSupported (nativePdfFormats)",
|
||||||
|
ctx: func() *api.ContextMock {
|
||||||
|
ctx := &api.ContextMock{Context: new(api.Context)}
|
||||||
|
ctx.SetFiles(map[string]string{
|
||||||
|
"document.docx": "/document.docx",
|
||||||
|
})
|
||||||
|
return ctx
|
||||||
|
}(),
|
||||||
|
libreOffice: &libreofficeapi.ApiMock{
|
||||||
|
PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options libreofficeapi.Options) error {
|
||||||
|
return libreofficeapi.ErrInvalidPdfFormats
|
||||||
|
},
|
||||||
|
ExtensionsMock: func() []string {
|
||||||
|
return []string{".docx"}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectError: true,
|
||||||
|
expectHttpError: true,
|
||||||
|
expectHttpStatus: http.StatusBadRequest,
|
||||||
|
expectOutputPathsCount: 0,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
scenario: "ErrMalformedPageRanges",
|
scenario: "ErrMalformedPageRanges",
|
||||||
ctx: func() *api.ContextMock {
|
ctx: func() *api.ContextMock {
|
||||||
@@ -44,6 +66,11 @@ func TestConvertRoute(t *testing.T) {
|
|||||||
ctx.SetFiles(map[string]string{
|
ctx.SetFiles(map[string]string{
|
||||||
"document.docx": "/document.docx",
|
"document.docx": "/document.docx",
|
||||||
})
|
})
|
||||||
|
ctx.SetValues(map[string][]string{
|
||||||
|
"pdfa": {
|
||||||
|
"foo",
|
||||||
|
},
|
||||||
|
})
|
||||||
return ctx
|
return ctx
|
||||||
}(),
|
}(),
|
||||||
libreOffice: &libreofficeapi.ApiMock{
|
libreOffice: &libreofficeapi.ApiMock{
|
||||||
|
|||||||
Reference in New Issue
Block a user