chore(libreoffice): switch to supervisor (#708)

This commit is contained in:
Julien Neuhart
2023-10-28 17:55:53 +02:00
committed by GitHub
parent c8b318b315
commit 052448c59a
38 changed files with 2381 additions and 3093 deletions

View File

@@ -9,12 +9,12 @@ import (
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
"github.com/gotenberg/gotenberg/v7/pkg/modules/api"
"github.com/gotenberg/gotenberg/v7/pkg/modules/libreoffice/uno"
libreofficeapi "github.com/gotenberg/gotenberg/v7/pkg/modules/libreoffice/api"
)
// convertRoute returns an api.Route which can convert LibreOffice documents
// convertRoute returns an [api.Route] which can convert LibreOffice documents
// to PDF.
func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PDFEngine) api.Route {
return api.Route{
Method: http.MethodPost,
Path: "/forms/libreoffice/convert",
@@ -27,52 +27,46 @@ func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
inputPaths []string
landscape bool
nativePageRanges string
nativePDFA1aFormat bool
nativePDFformat string
PDFformat string
nativePdfA1aFormat bool
nativePdfFormat string
pdfFormat string
merge bool
)
err := ctx.FormData().
MandatoryPaths(unoAPI.Extensions(), &inputPaths).
MandatoryPaths(libreOffice.Extensions(), &inputPaths).
Bool("landscape", &landscape, false).
String("nativePageRanges", &nativePageRanges, "").
Bool("nativePdfA1aFormat", &nativePDFA1aFormat, false).
String("nativePdfFormat", &nativePDFformat, "").
String("pdfFormat", &PDFformat, "").
Bool("nativePdfA1aFormat", &nativePdfA1aFormat, false).
String("nativePdfFormat", &nativePdfFormat, "").
String("pdfFormat", &pdfFormat, "").
Bool("merge", &merge, false).
Validate()
if err != nil {
return fmt.Errorf("validate form data: %w", err)
}
if nativePDFA1aFormat {
// nativePdfFormat > pdfFormat > nativePdfA1aFormat.
var (
actualPdfFormat string
nativeFormat bool
)
// FIXME: deprecated.
if nativePdfA1aFormat {
ctx.Log().Warn("'nativePdfA1aFormat' is deprecated; prefer 'nativePdfFormat' or 'pdfFormat' form fields instead")
actualPdfFormat = gotenberg.FormatPDFA1a
nativeFormat = true
}
if nativePDFA1aFormat && nativePDFformat != "" {
return api.WrapError(
errors.New("got both 'nativePdfFormat' and 'nativePdfA1aFormat' form fields"),
api.NewSentinelHTTPError(http.StatusBadRequest, "Both 'nativePdfFormat' and 'nativePdfA1aFormat' form fields are provided"),
)
if pdfFormat != "" {
actualPdfFormat = pdfFormat
nativeFormat = false
}
if nativePDFA1aFormat && PDFformat != "" {
return api.WrapError(
errors.New("got both 'pdfFormat' and 'nativePdfA1aFormat' form fields"),
api.NewSentinelHTTPError(http.StatusBadRequest, "Both 'pdfFormat' and 'nativePdfA1aFormat' form fields are provided"),
)
}
if nativePDFformat != "" && PDFformat != "" {
return api.WrapError(
errors.New("got both 'pdfFormat' and 'nativePdfFormat' form fields"),
api.NewSentinelHTTPError(http.StatusBadRequest, "Both 'pdfFormat' and 'nativePdfFormat' form fields are provided"),
)
}
if nativePDFA1aFormat {
nativePDFformat = gotenberg.FormatPDFA1a
if nativePdfFormat != "" {
actualPdfFormat = nativePdfFormat
nativeFormat = true
}
// Alright, let's convert each document to PDF.
@@ -82,16 +76,16 @@ func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
for i, inputPath := range inputPaths {
outputPaths[i] = ctx.GeneratePath(".pdf")
options := uno.Options{
options := libreofficeapi.Options{
Landscape: landscape,
PageRanges: nativePageRanges,
PDFformat: nativePDFformat,
PdfFormat: nativePdfFormat,
}
err = unoAPI.PDF(ctx, ctx.Log(), inputPath, outputPaths[i], options)
err = libreOffice.Pdf(ctx, ctx.Log(), inputPath, outputPaths[i], options)
if err != nil {
if errors.Is(err, uno.ErrMalformedPageRanges) {
if errors.Is(err, libreofficeapi.ErrMalformedPageRanges) {
return api.WrapError(
fmt.Errorf("convert to PDF: %w", err),
api.NewSentinelHTTPError(http.StatusBadRequest, fmt.Sprintf("Malformed page ranges '%s' (nativePageRanges)", options.PageRanges)),
@@ -116,14 +110,11 @@ func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
// Now, let's check if the client want to convert this result
// PDF to a specific PDF format.
// Note: nativePdfA1aFormat/nativePdfFormat have not been
// specified if PDFformat is not empty.
if PDFformat != "" {
if !nativeFormat && actualPdfFormat != "" {
convertInputPath := outputPath
convertOutputPath := ctx.GeneratePath(".pdf")
err = engine.Convert(ctx, ctx.Log(), PDFformat, convertInputPath, convertOutputPath)
err = engine.Convert(ctx, ctx.Log(), actualPdfFormat, convertInputPath, convertOutputPath)
if err != nil {
if errors.Is(err, gotenberg.ErrPDFFormatNotAvailable) {
@@ -131,7 +122,7 @@ func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
fmt.Errorf("convert PDF: %w", err),
api.NewSentinelHTTPError(
http.StatusBadRequest,
fmt.Sprintf("At least one PDF engine does not handle the PDF format '%s' (pdfFormat), while other have failed to convert for other reasons", PDFformat),
fmt.Sprintf("At least one PDF engine does not handle the PDF format '%s' (pdfFormat), while other have failed to convert for other reasons", actualPdfFormat),
),
)
}
@@ -144,7 +135,7 @@ func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
}
// Last but not least, add the output path to the context so that
// the API is able to send it as a response to the client.
// the Uno is able to send it as a response to the client.
err = ctx.AddOutputPaths(outputPath)
if err != nil {
@@ -157,17 +148,14 @@ func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
// Ok, we don't have to merge the PDFs. Let's check if the client
// want to convert each PDF to a specific PDF format.
// Note: nativePdfA1aFormat/nativePdfFormat have not been
// specified if PDFformat is not empty.
if PDFformat != "" {
if !nativeFormat && actualPdfFormat != "" {
convertOutputPaths := make([]string, len(outputPaths))
for i, outputPath := range outputPaths {
convertInputPath := outputPath
convertOutputPaths[i] = ctx.GeneratePath(".pdf")
err = engine.Convert(ctx, ctx.Log(), PDFformat, convertInputPath, convertOutputPaths[i])
err = engine.Convert(ctx, ctx.Log(), actualPdfFormat, convertInputPath, convertOutputPaths[i])
if err != nil {
if errors.Is(err, gotenberg.ErrPDFFormatNotAvailable) {
@@ -175,7 +163,7 @@ func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
fmt.Errorf("convert PDF: %w", err),
api.NewSentinelHTTPError(
http.StatusBadRequest,
fmt.Sprintf("At least one PDF engine does not handle the PDF format '%s' (pdfFormat), while other have failed to convert for other reasons", PDFformat),
fmt.Sprintf("At least one PDF engine does not handle the PDF format '%s' (pdfFormat), while other have failed to convert for other reasons", actualPdfFormat),
),
)
}
@@ -190,7 +178,7 @@ func convertRoute(unoAPI uno.API, engine gotenberg.PDFEngine) api.Route {
}
// Last but not least, add the output paths to the context so that
// the API is able to send them as a response to the client.
// the Uno is able to send them as a response to the client.
err = ctx.AddOutputPaths(outputPaths...)
if err != nil {