mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-16 04:12:16 +01:00
chore(libreoffice): switch to supervisor (#708)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Package pdfengine provides a module which interacts with the UNO
|
||||
// (Universal Network Objects) API and implements the gotenberg.PDFEngine
|
||||
// Package pdfengine provides a module which interacts with LibreOffice via the
|
||||
// UNO (Universal Network Objects) API and implements the [gotenberg.PDFEngine]
|
||||
// interface.
|
||||
package pdfengine
|
||||
|
||||
@@ -8,37 +8,37 @@ import (
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/modules/libreoffice/uno"
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/modules/libreoffice/api"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gotenberg.MustRegisterModule(UNO{})
|
||||
gotenberg.MustRegisterModule(new(LibreOfficePdfEngine))
|
||||
}
|
||||
|
||||
// UNO interacts with the UNO (Universal Network Objects) API and implements
|
||||
// the gotenberg.PDFEngine interface.
|
||||
type UNO struct {
|
||||
unoAPI uno.API
|
||||
// LibreOfficePdfEngine interacts with the LibreOffice (Universal Network Objects) API
|
||||
// and implements the [gotenberg.PDFEngine] interface.
|
||||
type LibreOfficePdfEngine struct {
|
||||
unoAPI api.Uno
|
||||
}
|
||||
|
||||
// Descriptor returns a UNO's module descriptor.
|
||||
func (UNO) Descriptor() gotenberg.ModuleDescriptor {
|
||||
// Descriptor returns a [LibreOfficePdfEngine]'s module descriptor.
|
||||
func (engine *LibreOfficePdfEngine) Descriptor() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{
|
||||
ID: "uno-pdfengine",
|
||||
New: func() gotenberg.Module { return new(UNO) },
|
||||
ID: "libreoffice-pdfengine",
|
||||
New: func() gotenberg.Module { return new(LibreOfficePdfEngine) },
|
||||
}
|
||||
}
|
||||
|
||||
// Provision sets the module properties.
|
||||
func (engine *UNO) Provision(ctx *gotenberg.Context) error {
|
||||
provider, err := ctx.Module(new(uno.Provider))
|
||||
func (engine *LibreOfficePdfEngine) Provision(ctx *gotenberg.Context) error {
|
||||
provider, err := ctx.Module(new(api.Provider))
|
||||
if err != nil {
|
||||
return fmt.Errorf("get unoconv provider: %w", err)
|
||||
return fmt.Errorf("get LibreOffice Uno provider: %w", err)
|
||||
}
|
||||
|
||||
unoAPI, err := provider.(uno.Provider).UNO()
|
||||
unoAPI, err := provider.(api.Provider).LibreOffice()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get unoconv API: %w", err)
|
||||
return fmt.Errorf("get LibreOffice Uno: %w", err)
|
||||
}
|
||||
|
||||
engine.unoAPI = unoAPI
|
||||
@@ -47,24 +47,24 @@ func (engine *UNO) Provision(ctx *gotenberg.Context) error {
|
||||
}
|
||||
|
||||
// Merge is not available for this PDF engine.
|
||||
func (engine UNO) Merge(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return fmt.Errorf("merge PDFs with unoconv: %w", gotenberg.ErrPDFEngineMethodNotAvailable)
|
||||
func (engine *LibreOfficePdfEngine) Merge(_ context.Context, _ *zap.Logger, _ []string, _ string) error {
|
||||
return fmt.Errorf("merge PDFs with LibreOffice: %w", gotenberg.ErrPDFEngineMethodNotAvailable)
|
||||
}
|
||||
|
||||
// Convert converts the given PDF to a specific PDF format. Currently, only the
|
||||
// PDF/A-1a, PDF/A-2b and PDF/A-3b formats are available. If another PDF format
|
||||
// is requested, it returns a gotenberg.ErrPDFFormatNotAvailable error.
|
||||
func (engine UNO) Convert(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
err := engine.unoAPI.PDF(ctx, logger, inputPath, outputPath, uno.Options{
|
||||
PDFformat: format,
|
||||
// is requested, it returns a [gotenberg.ErrPDFFormatNotAvailable] error.
|
||||
func (engine *LibreOfficePdfEngine) Convert(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error {
|
||||
err := engine.unoAPI.Pdf(ctx, logger, inputPath, outputPath, api.Options{
|
||||
PdfFormat: format,
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if errors.Is(err, uno.ErrInvalidPDFformat) {
|
||||
return fmt.Errorf("convert PDF to '%s' with unoconv: %w", format, gotenberg.ErrPDFFormatNotAvailable)
|
||||
if errors.Is(err, api.ErrInvalidPdfFormat) {
|
||||
return fmt.Errorf("convert PDF to '%s' with LibreOffice: %w", format, gotenberg.ErrPDFFormatNotAvailable)
|
||||
}
|
||||
|
||||
return fmt.Errorf("convert PDF to '%s' with unoconv: %w", format, err)
|
||||
@@ -72,7 +72,7 @@ func (engine UNO) Convert(ctx context.Context, logger *zap.Logger, format, input
|
||||
|
||||
// Interface guards.
|
||||
var (
|
||||
_ gotenberg.Module = (*UNO)(nil)
|
||||
_ gotenberg.Provisioner = (*UNO)(nil)
|
||||
_ gotenberg.PDFEngine = (*UNO)(nil)
|
||||
_ gotenberg.Module = (*LibreOfficePdfEngine)(nil)
|
||||
_ gotenberg.Provisioner = (*LibreOfficePdfEngine)(nil)
|
||||
_ gotenberg.PDFEngine = (*LibreOfficePdfEngine)(nil)
|
||||
)
|
||||
|
||||
@@ -9,166 +9,159 @@ import (
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/modules/libreoffice/uno"
|
||||
"github.com/gotenberg/gotenberg/v7/pkg/modules/libreoffice/api"
|
||||
)
|
||||
|
||||
func TestUNO_Descriptor(t *testing.T) {
|
||||
descriptor := UNO{}.Descriptor()
|
||||
func TestLibreOfficePdfEngine_Descriptor(t *testing.T) {
|
||||
descriptor := new(LibreOfficePdfEngine).Descriptor()
|
||||
|
||||
actual := reflect.TypeOf(descriptor.New())
|
||||
expect := reflect.TypeOf(new(UNO))
|
||||
expect := reflect.TypeOf(new(LibreOfficePdfEngine))
|
||||
|
||||
if actual != expect {
|
||||
t.Errorf("expected '%s' but got '%s'", expect, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUNO_Provider(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx *gotenberg.Context
|
||||
expectProvisionErr bool
|
||||
func TestLibreOfficePdfEngine_Provider(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
scenario string
|
||||
ctx *gotenberg.Context
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "nominal behavior",
|
||||
ctx: func() *gotenberg.Context {
|
||||
provider := &struct {
|
||||
gotenberg.ModuleMock
|
||||
uno.ProviderMock
|
||||
}{}
|
||||
provider.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module {
|
||||
return provider
|
||||
}}
|
||||
}
|
||||
provider.UNOMock = func() (uno.API, error) {
|
||||
return uno.APIMock{}, nil
|
||||
}
|
||||
|
||||
return gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(UNO).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
provider.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
},
|
||||
{
|
||||
name: "no UNO API provider",
|
||||
scenario: "no LibreOffice API provider",
|
||||
ctx: gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(UNO).Descriptor().FlagSet,
|
||||
FlagSet: new(LibreOfficePdfEngine).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{},
|
||||
),
|
||||
expectProvisionErr: true,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "no API from UNO API provider",
|
||||
scenario: "no API from LibreOffice API provider",
|
||||
ctx: func() *gotenberg.Context {
|
||||
provider := &struct {
|
||||
gotenberg.ModuleMock
|
||||
uno.ProviderMock
|
||||
api.ProviderMock
|
||||
}{}
|
||||
provider.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module {
|
||||
return provider
|
||||
}}
|
||||
}
|
||||
provider.UNOMock = func() (uno.API, error) {
|
||||
return uno.APIMock{}, errors.New("foo")
|
||||
provider.LibreOfficeMock = func() (api.Uno, error) {
|
||||
return nil, errors.New("foo")
|
||||
}
|
||||
|
||||
return gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(UNO).Descriptor().FlagSet,
|
||||
FlagSet: new(LibreOfficePdfEngine).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
provider.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
expectProvisionErr: true,
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
{
|
||||
scenario: "provision success",
|
||||
ctx: func() *gotenberg.Context {
|
||||
provider := &struct {
|
||||
gotenberg.ModuleMock
|
||||
api.ProviderMock
|
||||
}{}
|
||||
provider.DescriptorMock = func() gotenberg.ModuleDescriptor {
|
||||
return gotenberg.ModuleDescriptor{ID: "foo", New: func() gotenberg.Module {
|
||||
return provider
|
||||
}}
|
||||
}
|
||||
provider.LibreOfficeMock = func() (api.Uno, error) {
|
||||
return new(api.ApiMock), nil
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
mod := new(UNO)
|
||||
return gotenberg.NewContext(
|
||||
gotenberg.ParsedFlags{
|
||||
FlagSet: new(LibreOfficePdfEngine).Descriptor().FlagSet,
|
||||
},
|
||||
[]gotenberg.ModuleDescriptor{
|
||||
provider.Descriptor(),
|
||||
},
|
||||
)
|
||||
}(),
|
||||
expectError: false,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.scenario, func(t *testing.T) {
|
||||
mod := new(LibreOfficePdfEngine)
|
||||
err := mod.Provision(tc.ctx)
|
||||
|
||||
if tc.expectProvisionErr && err == nil {
|
||||
t.Error("expected mod.Provision() error, but got none")
|
||||
if !tc.expectError && err != nil {
|
||||
t.Fatalf("expected no error but got: %v", err)
|
||||
}
|
||||
|
||||
if !tc.expectProvisionErr && err != nil {
|
||||
t.Errorf("expected no error from mod.Provision(), but got: %v", err)
|
||||
if tc.expectError && err == nil {
|
||||
t.Fatal("expected error but got none")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUNO_Merge(t *testing.T) {
|
||||
mod := new(UNO)
|
||||
func TestLibreOfficePdfEngine_Merge(t *testing.T) {
|
||||
mod := new(LibreOfficePdfEngine)
|
||||
err := mod.Merge(context.Background(), zap.NewNop(), nil, "")
|
||||
|
||||
if !errors.Is(err, gotenberg.ErrPDFEngineMethodNotAvailable) {
|
||||
t.Errorf("expected error %v from mod.Merge(), but got: %v", gotenberg.ErrPDFEngineMethodNotAvailable, err)
|
||||
t.Errorf("expected error %v, but got: %v", gotenberg.ErrPDFEngineMethodNotAvailable, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUNO_Convert(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mod UNO
|
||||
expectConvertErr bool
|
||||
func TestLibreOfficePdfEngine_Convert(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
scenario string
|
||||
api api.Uno
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "nominal behavior",
|
||||
mod: UNO{
|
||||
unoAPI: uno.APIMock{
|
||||
PDFMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options uno.Options) error {
|
||||
return nil
|
||||
},
|
||||
scenario: "convert success",
|
||||
api: &api.ApiMock{
|
||||
PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options api.Options) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "invalid PDF format",
|
||||
mod: UNO{
|
||||
unoAPI: uno.APIMock{
|
||||
PDFMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options uno.Options) error {
|
||||
return uno.ErrInvalidPDFformat
|
||||
},
|
||||
scenario: "invalid PDF format",
|
||||
api: &api.ApiMock{
|
||||
PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options api.Options) error {
|
||||
return api.ErrInvalidPdfFormat
|
||||
},
|
||||
},
|
||||
expectConvertErr: true,
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "convert fail",
|
||||
mod: UNO{
|
||||
unoAPI: uno.APIMock{
|
||||
PDFMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options uno.Options) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
scenario: "convert fail",
|
||||
api: &api.ApiMock{
|
||||
PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options api.Options) error {
|
||||
return errors.New("foo")
|
||||
},
|
||||
},
|
||||
expectConvertErr: true,
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
} {
|
||||
t.Run(tc.scenario, func(t *testing.T) {
|
||||
engine := &LibreOfficePdfEngine{unoAPI: tc.api}
|
||||
err := engine.Convert(context.Background(), zap.NewNop(), "", "", "")
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.mod.Convert(context.Background(), zap.NewNop(), "", "", "")
|
||||
|
||||
if tc.expectConvertErr && err == nil {
|
||||
t.Errorf("expected mod.Convert() error, but got none")
|
||||
if !tc.expectError && err != nil {
|
||||
t.Fatalf("expected no error but got: %v", err)
|
||||
}
|
||||
|
||||
if !tc.expectConvertErr && err != nil {
|
||||
t.Fatalf("expected no error from mod.Convert(), but got: %v", err)
|
||||
if tc.expectError && err == nil {
|
||||
t.Fatal("expected error but got none")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user