feat: add 7.x source code

This commit is contained in:
Julien Neuhart
2021-08-22 12:52:44 +02:00
parent e457155950
commit 0f5e8fd314
111 changed files with 31188 additions and 0 deletions

3
pkg/modules/pdftk/doc.go Normal file
View File

@@ -0,0 +1,3 @@
// Package pdftk provides a module which abstracts the CLI tool PDFtk and
// implements the gotenberg.PDFEngine interface.
package pdftk

View File

@@ -0,0 +1,84 @@
package pdftk
import (
"context"
"errors"
"fmt"
"os"
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
"go.uber.org/zap"
)
func init() {
gotenberg.MustRegisterModule(PDFtk{})
}
// PDFtk abstracts the CLI tool PDFtk and implements the gotenberg.PDFEngine
// interface.
type PDFtk struct {
binPath string
}
// Descriptor returns a PDFtk's module descriptor.
func (engine PDFtk) Descriptor() gotenberg.ModuleDescriptor {
return gotenberg.ModuleDescriptor{
ID: "pdftk",
New: func() gotenberg.Module { return new(PDFtk) },
}
}
// Provision sets the modules properties. It returns an error if the
// environment variable PDFTK_BIN_PATH is not set.
func (engine *PDFtk) Provision(_ *gotenberg.Context) error {
binPath, ok := os.LookupEnv("PDFTK_BIN_PATH")
if !ok {
return errors.New("PDFTK_BIN_PATH environment variable is not set")
}
engine.binPath = binPath
return nil
}
// Validate validates the module properties.
func (engine PDFtk) Validate() error {
_, err := os.Stat(engine.binPath)
if os.IsNotExist(err) {
return fmt.Errorf("PDFtk binary path does not exist: %w", err)
}
return nil
}
// Merge merges the given PDFs into a unique PDF.
func (engine PDFtk) Merge(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
var args []string
args = append(args, inputPaths...)
args = append(args, "cat", "output", outputPath)
cmd, err := gotenberg.CommandContext(ctx, logger, engine.binPath, args...)
if err != nil {
return fmt.Errorf("create command: %w", err)
}
err = cmd.Exec()
if err == nil {
return nil
}
return fmt.Errorf("merge PDFs with PDFtk: %w", err)
}
// Convert is not available for this PDF engine.
func (engine PDFtk) Convert(_ context.Context, _ *zap.Logger, format, _, _ string) error {
return fmt.Errorf("convert PDF to '%s' with PDFtk: %w", format, gotenberg.ErrPDFEngineMethodNotAvailable)
}
// Interface guards.
var (
_ gotenberg.Module = (*PDFtk)(nil)
_ gotenberg.Provisioner = (*PDFtk)(nil)
_ gotenberg.Validator = (*PDFtk)(nil)
_ gotenberg.PDFEngine = (*PDFtk)(nil)
)

View File

@@ -0,0 +1,136 @@
package pdftk
import (
"context"
"errors"
"os"
"reflect"
"testing"
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
"go.uber.org/zap"
)
func TestPDFtk_Descriptor(t *testing.T) {
descriptor := PDFtk{}.Descriptor()
actual := reflect.TypeOf(descriptor.New())
expect := reflect.TypeOf(new(PDFtk))
if actual != expect {
t.Errorf("expected '%s' but got '%s'", expect, actual)
}
}
func TestPDFtk_Provision(t *testing.T) {
mod := new(PDFtk)
ctx := gotenberg.NewContext(gotenberg.ParsedFlags{}, nil)
err := mod.Provision(ctx)
if err != nil {
t.Errorf("expected no error but got: %v", err)
}
}
func TestPDFtk_Validate(t *testing.T) {
for i, tc := range []struct {
binPath string
expectErr bool
}{
{
expectErr: true,
},
{
binPath: "/foo",
expectErr: true,
},
{
binPath: os.Getenv("PDFTK_BIN_PATH"),
},
} {
mod := new(PDFtk)
mod.binPath = tc.binPath
err := mod.Validate()
if tc.expectErr && err == nil {
t.Errorf("test %d: expected error but got: %v", i, err)
}
if !tc.expectErr && err != nil {
t.Errorf("test %d: expected no error but got: %v", i, err)
}
}
}
func TestPDFtk_Merge(t *testing.T) {
for i, tc := range []struct {
ctx context.Context
inputPaths []string
expectErr bool
}{
{
ctx: context.TODO(),
inputPaths: []string{
"/tests/test/testdata/pdfengines/sample1.pdf",
},
},
{
ctx: context.TODO(),
inputPaths: []string{
"/tests/test/testdata/pdfengines/sample1.pdf",
"/tests/test/testdata/pdfengines/sample2.pdf",
},
},
{
ctx: nil,
expectErr: true,
},
{
ctx: context.TODO(),
inputPaths: []string{
"foo",
},
expectErr: true,
},
} {
func() {
mod := new(PDFtk)
err := mod.Provision(nil)
if err != nil {
t.Fatalf("test %d: expected error but got: %v", i, err)
}
outputDir, err := gotenberg.MkdirAll()
if err != nil {
t.Fatalf("test %d: expected error but got: %v", i, err)
}
defer func() {
err := os.RemoveAll(outputDir)
if err != nil {
t.Fatalf("test %d: expected no error but got: %v", i, err)
}
}()
err = mod.Merge(tc.ctx, zap.NewNop(), tc.inputPaths, outputDir+"/foo.pdf")
if tc.expectErr && err == nil {
t.Errorf("test %d: expected error but got: %v", i, err)
}
if !tc.expectErr && err != nil {
t.Errorf("test %d: expected no error but got: %v", i, err)
}
}()
}
}
func TestPDFtk_Convert(t *testing.T) {
mod := new(PDFtk)
err := mod.Convert(context.TODO(), zap.NewNop(), "", "", "")
if !errors.Is(err, gotenberg.ErrPDFEngineMethodNotAvailable) {
t.Errorf("expected error %v, but got: %v", gotenberg.ErrPDFEngineMethodNotAvailable, err)
}
}