huge refactoring

This commit is contained in:
Julien Neuhart
2019-07-23 13:57:18 +02:00
parent f6b357691c
commit 7bfbda4490
105 changed files with 4051 additions and 2667 deletions

View File

@@ -12,19 +12,22 @@ import (
"github.com/mafredri/cdp/protocol/page"
"github.com/mafredri/cdp/protocol/target"
"github.com/mafredri/cdp/rpcc"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
"github.com/thecodingmachine/gotenberg/internal/pkg/xcontext"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
"github.com/thecodingmachine/gotenberg/internal/pkg/xtime"
"golang.org/x/sync/errgroup"
)
type chrome struct {
url string
opts *ChromeOptions
type chromePrinter struct {
logger xlog.Logger
url string
opts ChromePrinterOptions
}
// ChromeOptions helps customizing the
// ChromePrinterOptions helps customizing the
// Google Chrome printer behaviour.
type ChromeOptions struct {
type ChromePrinterOptions struct {
WaitTimeout float64
WaitDelay float64
HeaderHTML string
@@ -38,26 +41,27 @@ type ChromeOptions struct {
Landscape bool
}
func (p *chrome) Print(destination string) error {
const op string = "printer.chrome.Print"
ctx, cancel := timeout.Context(p.opts.WaitTimeout + p.opts.WaitDelay)
func (p chromePrinter) Print(destination string) error {
const op string = "printer.chromePrinter.Print"
logOptions(p.logger, p.opts)
ctx, cancel := xcontext.WithTimeout(p.logger, p.opts.WaitTimeout+p.opts.WaitDelay)
defer cancel()
resolver := func() error {
devt, err := devtool.New("http://localhost:9222").Version(ctx)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
// connect to WebSocket URL (page) that speaks the Chrome DevTools Protocol.
devtConn, err := rpcc.DialContext(ctx, devt.WebSocketDebuggerURL)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
defer devtConn.Close() // nolint: errcheck
// create a new CDP Client that uses conn.
devtClient := cdp.NewClient(devtConn)
newContextTarget, err := devtClient.Target.CreateBrowserContext(ctx)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
// create a new blank target with the new browser context.
createTargetArgs := target.
@@ -65,13 +69,13 @@ func (p *chrome) Print(destination string) error {
SetBrowserContextID(newContextTarget.BrowserContextID)
newTarget, err := devtClient.Target.CreateTarget(ctx, createTargetArgs)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
// connect the client to the new target.
newTargetWsURL := fmt.Sprintf("ws://127.0.0.1:9222/devtools/page/%s", newTarget.TargetID)
newContextConn, err := rpcc.DialContext(ctx, newTargetWsURL)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
defer newContextConn.Close() // nolint: errcheck
// create a new CDP Client that uses newContextConn.
@@ -86,10 +90,10 @@ func (p *chrome) Print(destination string) error {
func() error { return targetClient.Page.Enable(ctx) },
func() error { return targetClient.Runtime.Enable(ctx) },
); err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
if err := p.navigate(ctx, targetClient); err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
print, err := targetClient.Page.PrintToPDF(
ctx,
@@ -107,58 +111,67 @@ func (p *chrome) Print(destination string) error {
SetPrintBackground(true),
)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
return nil
}
if err := resolver(); err != nil {
return timeout.Err(ctx, err)
return xcontext.MustHandleError(
ctx,
xerror.New(op, err),
)
}
return nil
}
func (p *chrome) navigate(ctx context.Context, client *cdp.Client) error {
const op string = "printer.chrome.navigate"
// make sure Page events are enabled.
if err := client.Page.Enable(ctx); err != nil {
return &standarderror.Error{Op: op, Err: err}
func (p chromePrinter) navigate(ctx context.Context, client *cdp.Client) error {
const op string = "printer.chromePrinter.navigate"
resolver := func() error {
// make sure Page events are enabled.
if err := client.Page.Enable(ctx); err != nil {
return err
}
// make sure Network events are enabled.
if err := client.Network.Enable(ctx, nil); err != nil {
return err
}
// create all clients for events.
domContentEventFired, err := client.Page.DOMContentEventFired(ctx)
if err != nil {
return err
}
defer domContentEventFired.Close() // nolint: errcheck
loadEventFired, err := client.Page.LoadEventFired(ctx)
if err != nil {
return err
}
defer loadEventFired.Close() // nolint: errcheck
loadingFinished, err := client.Network.LoadingFinished(ctx)
if err != nil {
return err
}
defer loadingFinished.Close() // nolint: errcheck
if _, err := client.Page.Navigate(ctx, page.NewNavigateArgs(p.url)); err != nil {
return err
}
if err := runBatch(
// wait for all events.
func() error { _, err := domContentEventFired.Recv(); return err },
func() error { _, err := loadEventFired.Recv(); return err },
func() error { _, err := loadingFinished.Recv(); return err },
); err != nil {
return err
}
// wait for a given amount of time (useful for javascript delay).
time.Sleep(xtime.Duration(p.opts.WaitDelay))
return nil
}
// make sure Network events are enabled.
if err := client.Network.Enable(ctx, nil); err != nil {
return &standarderror.Error{Op: op, Err: err}
if err := resolver(); err != nil {
return xerror.New(op, err)
}
// create all clients for events.
domContentEventFired, err := client.Page.DOMContentEventFired(ctx)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
}
defer domContentEventFired.Close() // nolint: errcheck
loadEventFired, err := client.Page.LoadEventFired(ctx)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
}
defer loadEventFired.Close() // nolint: errcheck
loadingFinished, err := client.Network.LoadingFinished(ctx)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
}
defer loadingFinished.Close() // nolint: errcheck
if _, err := client.Page.Navigate(ctx, page.NewNavigateArgs(p.url)); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
if err := runBatch(
// wait for all events.
func() error { _, err := domContentEventFired.Recv(); return err },
func() error { _, err := loadEventFired.Recv(); return err },
func() error { _, err := loadingFinished.Recv(); return err },
); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
// wait for a given amount of time (useful for javascript delay).
time.Sleep(timeout.Duration(p.opts.WaitDelay))
return nil
}
@@ -174,5 +187,5 @@ func runBatch(fn ...func() error) error {
// Compile-time checks to ensure type implements desired interfaces.
var (
_ = Printer(new(chrome))
_ = Printer(new(chromePrinter))
)

View File

@@ -1,5 +1,3 @@
/*
Package printer contains structs which convert
a specific file type to PDF.
*/
// Package printer helps converting
// a specific file type to PDF.
package printer

View File

@@ -2,13 +2,17 @@ package printer
import (
"fmt"
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
)
// NewHTML returns an HTML printer.
func NewHTML(fpath string, opts *ChromeOptions) Printer {
// NewHTMLPrinter returns a Printer which
// is able to convert an HTML file to PDF.
func NewHTMLPrinter(logger xlog.Logger, fpath string, opts ChromePrinterOptions) Printer {
URL := fmt.Sprintf("file://%s", fpath)
return &chrome{
url: URL,
opts: opts,
return chromePrinter{
logger: logger,
url: URL,
opts: opts,
}
}

View File

@@ -7,37 +7,46 @@ import (
"io/ioutil"
"path/filepath"
"github.com/labstack/gommon/random"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
"github.com/thecodingmachine/gotenberg/internal/pkg/xrand"
)
// NewMarkdown returns a Markdown printer.
func NewMarkdown(fpath string, opts *ChromeOptions) (Printer, error) {
const op string = "printer.NewMarkdown"
tmpl, err := template.
New(filepath.Base(fpath)).
Funcs(template.FuncMap{"toHTML": markdownToHTML}).
ParseFiles(fpath)
// NewMarkdownPrinter returns a Printer which
// is able to convert Markdown files to PDF.
func NewMarkdownPrinter(logger xlog.Logger, fpath string, opts ChromePrinterOptions) (Printer, error) {
const op string = "printer.NewMarkdownPrinter"
resolver := func() (string, error) {
tmpl, err := template.
New(filepath.Base(fpath)).
Funcs(template.FuncMap{"toHTML": markdownToHTML}).
ParseFiles(fpath)
if err != nil {
return "", err
}
dirPath := filepath.Dir(fpath)
data := &templateData{DirPath: dirPath}
var buffer bytes.Buffer
if err := tmpl.Execute(&buffer, data); err != nil {
return "", err
}
baseFilename := xrand.Get()
dst := fmt.Sprintf("%s/%s.html", dirPath, baseFilename)
if err := ioutil.WriteFile(dst, buffer.Bytes(), 0644); err != nil {
return "", err
}
return fmt.Sprintf("file://%s", dst), nil
}
URL, err := resolver()
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
return chromePrinter{}, xerror.New(op, err)
}
dirPath := filepath.Dir(fpath)
data := &templateData{DirPath: dirPath}
var buffer bytes.Buffer
if err := tmpl.Execute(&buffer, data); err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
baseFilename := random.String(32)
dst := fmt.Sprintf("%s/%s.html", dirPath, baseFilename)
if err := ioutil.WriteFile(dst, buffer.Bytes(), 0644); err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
URL := fmt.Sprintf("file://%s", dst)
return &chrome{
url: URL,
opts: opts,
return chromePrinter{
logger: logger,
url: URL,
opts: opts,
}, nil
}
@@ -50,7 +59,7 @@ func markdownToHTML(dirPath, filename string) (template.HTML, error) {
fpath := fmt.Sprintf("%s/%s", dirPath, filename)
b, err := ioutil.ReadFile(fpath)
if err != nil {
return "", &standarderror.Error{Op: op, Err: err}
return "", xerror.New(op, err)
}
unsafe := blackfriday.Run(b)
content := bluemonday.UGCPolicy().SanitizeBytes(unsafe)

View File

@@ -2,57 +2,71 @@ package printer
import (
"context"
"os/exec"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
"github.com/thecodingmachine/gotenberg/internal/pkg/xcontext"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xexec"
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
)
type merge struct {
type mergePrinter struct {
ctx context.Context
logger xlog.Logger
fpaths []string
opts *MergeOptions
opts MergePrinterOptions
}
// MergeOptions helps customizing the
// merge printer behaviour.
type MergeOptions struct {
// MergePrinterOptions helps customizing the
// merge Printer behaviour.
type MergePrinterOptions struct {
WaitTimeout float64
}
// NewMerge returns a merge printer.
func NewMerge(fpaths []string, opts *MergeOptions) Printer {
return &merge{
// NewMergePrinter returns a Printer which
// is able to merge PDFs.
func NewMergePrinter(logger xlog.Logger, fpaths []string, opts MergePrinterOptions) Printer {
return mergePrinter{
logger: logger,
fpaths: fpaths,
opts: opts,
}
}
func (p *merge) Print(destination string) error {
const op string = "printer.merge.Print"
func (p mergePrinter) Print(destination string) error {
const op string = "printer.mergePrinter.Print"
logOptions(p.logger, p.opts)
/*
context.Context may be providen from
an officePrinter which needs to merge
its result files.
*/
if p.ctx == nil {
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
ctx, cancel := xcontext.WithTimeout(p.logger, p.opts.WaitTimeout)
defer cancel()
p.ctx = ctx
}
p.logger.DebugfOp(op, "merging '%v'...", p.fpaths)
resolver := func() error {
var cmdArgs []string
cmdArgs = append(cmdArgs, p.fpaths...)
cmdArgs = append(cmdArgs, "cat", "output", destination)
cmd := exec.CommandContext(p.ctx, "pdftk", cmdArgs...)
_, err := cmd.Output()
var args []string
args = append(args, p.fpaths...)
args = append(args, "cat", "output", destination)
cmd, err := xexec.CommandContext(p.ctx, p.logger, "pdftk", args...)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
return err
}
return nil
xexec.LogBeforeExecute(p.logger, cmd)
return cmd.Run()
}
if err := resolver(); err != nil {
return timeout.Err(p.ctx, err)
return xcontext.MustHandleError(
p.ctx,
xerror.New(op, err),
)
}
return nil
}
// Compile-time checks to ensure type implements desired interfaces.
var (
_ = Printer(new(merge))
_ = Printer(new(mergePrinter))
)

View File

@@ -4,67 +4,75 @@ import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"sync"
"github.com/labstack/gommon/random"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
"github.com/thecodingmachine/gotenberg/internal/pkg/xcontext"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xexec"
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
"github.com/thecodingmachine/gotenberg/internal/pkg/xrand"
)
type office struct {
type officePrinter struct {
logger xlog.Logger
fpaths []string
opts *OfficeOptions
opts OfficePrinterOptions
}
// OfficeOptions helps customizing the
// Office printer behaviour.
type OfficeOptions struct {
// OfficePrinterOptions helps customizing the
// Office Printer behaviour.
type OfficePrinterOptions struct {
WaitTimeout float64
Landscape bool
}
// NewOffice returns an Office printer.
func NewOffice(fpaths []string, opts *OfficeOptions) Printer {
return &office{
// NewOfficePrinter returns a Printer which
// is able to convert Office documents to PDF.
func NewOfficePrinter(logger xlog.Logger, fpaths []string, opts OfficePrinterOptions) Printer {
return officePrinter{
logger: logger,
fpaths: fpaths,
opts: opts,
}
}
func (p *office) Print(destination string) error {
const op string = "printer.office.Print"
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
func (p officePrinter) Print(destination string) error {
const op string = "printer.officePrinter.Print"
logOptions(p.logger, p.opts)
ctx, cancel := xcontext.WithTimeout(p.logger, p.opts.WaitTimeout)
defer cancel()
fpaths := make([]string, len(p.fpaths))
resolver := func() error {
fpaths := make([]string, len(p.fpaths))
dirPath := filepath.Dir(destination)
for i, fpath := range p.fpaths {
baseFilename := random.String(32)
baseFilename := xrand.Get()
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
if err := unoconv(ctx, fpath, tmpDest, p.opts); err != nil {
return &standarderror.Error{Op: op, Err: err}
p.logger.DebugfOp(op, "converting '%s' to PDF...", fpath)
if err := unoconv(ctx, p.logger, fpath, tmpDest, p.opts); err != nil {
return err
}
p.logger.DebugfOp(op, "'%s.pdf' created", baseFilename)
fpaths[i] = tmpDest
}
return nil
if len(fpaths) == 1 {
p.logger.DebugOp(op, "only one PDF created, nothing to merge")
if err := os.Rename(fpaths[0], destination); err != nil {
return err
}
return nil
}
m := mergePrinter{
ctx: ctx,
fpaths: fpaths,
}
return m.Print(destination)
}
if err := resolver(); err != nil {
return timeout.Err(ctx, err)
}
if len(fpaths) == 1 {
if err := os.Rename(fpaths[0], destination); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}
m := &merge{
ctx: ctx,
fpaths: fpaths,
}
if err := m.Print(destination); err != nil {
return &standarderror.Error{Op: op, Err: err}
return xcontext.MustHandleError(
ctx,
xerror.New(op, err),
)
}
return nil
}
@@ -72,31 +80,41 @@ func (p *office) Print(destination string) error {
// nolint: gochecknoglobals
var mu sync.Mutex
func unoconv(ctx context.Context, fpath, destination string, opts *OfficeOptions) error {
func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string, opts OfficePrinterOptions) error {
const op string = "printer.unoconv"
// TODO check if timeout while waiting for the lock.
logger.DebugOp(op, "waiting lock to be released...")
mu.Lock()
defer mu.Unlock()
cmdArgs := []string{
"--format",
"pdf",
logger.DebugOp(op, "lock released")
resolver := func() error {
args := []string{
"--format",
"pdf",
}
if opts.Landscape {
args = append(args, "--printer", "PaperOrientation=landscape")
}
args = append(args, "--output", destination, fpath)
cmd, err := xexec.CommandContext(
ctx,
logger,
"unoconv",
args...,
)
if err != nil {
return err
}
xexec.LogBeforeExecute(logger, cmd)
return cmd.Run()
}
if opts.Landscape {
cmdArgs = append(cmdArgs, "--printer", "PaperOrientation=landscape")
}
cmdArgs = append(cmdArgs, "--output", destination, fpath)
cmd := exec.CommandContext(
ctx,
"unoconv",
cmdArgs...,
)
_, err := cmd.Output()
if err != nil {
return &standarderror.Error{Op: op, Err: err}
if err := resolver(); err != nil {
return xerror.New(op, err)
}
return nil
}
// Compile-time checks to ensure type implements desired interfaces.
var (
_ = Printer(new(office))
_ = Printer(new(officePrinter))
)

View File

@@ -1,7 +1,16 @@
package printer
import (
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
)
// Printer is a type that can create a PDF file from a source.
// The source is defined in the underlying implementation.
type Printer interface {
Print(destination string) error
}
func logOptions(logger xlog.Logger, opts interface{}) {
const op string = "printer.logOptions"
logger.DebugfOp(op, "options: %+v", opts)
}

View File

@@ -1,9 +1,15 @@
package printer
// NewURL returns a URL printer.
func NewURL(url string, opts *ChromeOptions) Printer {
return &chrome{
url: url,
opts: opts,
import (
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
)
// NewURLPrinter returns a Printer which
// is able to convert a URL to PDF.
func NewURLPrinter(logger xlog.Logger, url string, opts ChromePrinterOptions) Printer {
return chromePrinter{
logger: logger,
url: url,
opts: opts,
}
}