mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-13 02:42:14 +01:00
minor refactoring of const + better timeout handling in pinter package + api package tests
This commit is contained in:
@@ -39,83 +39,89 @@ type ChromeOptions struct {
|
||||
}
|
||||
|
||||
func (p *chrome) Print(destination string) error {
|
||||
const op = "printer.chrome.Print"
|
||||
const op string = "printer.chrome.Print"
|
||||
ctx, cancel := timeout.Context(p.opts.WaitTimeout + p.opts.WaitDelay)
|
||||
defer cancel()
|
||||
devt, err := devtool.New("http://localhost:9222").Version(ctx)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
resolver := func() error {
|
||||
devt, err := devtool.New("http://localhost:9222").Version(ctx)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: 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}
|
||||
}
|
||||
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}
|
||||
}
|
||||
// create a new blank target with the new browser context.
|
||||
createTargetArgs := target.
|
||||
NewCreateTargetArgs("about:blank").
|
||||
SetBrowserContextID(newContextTarget.BrowserContextID)
|
||||
newTarget, err := devtClient.Target.CreateTarget(ctx, createTargetArgs)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: 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}
|
||||
}
|
||||
defer newContextConn.Close() // nolint: errcheck
|
||||
// create a new CDP Client that uses newContextConn.
|
||||
targetClient := cdp.NewClient(newContextConn)
|
||||
closeTargetArgs := target.NewCloseTargetArgs(newTarget.TargetID)
|
||||
// close the target when done.
|
||||
defer targetClient.Target.CloseTarget(ctx, closeTargetArgs) // nolint: errcheck
|
||||
if err := runBatch(
|
||||
// enable all the domain events that we're interested in.
|
||||
func() error { return targetClient.DOM.Enable(ctx) },
|
||||
func() error { return targetClient.Network.Enable(ctx, network.NewEnableArgs()) },
|
||||
func() error { return targetClient.Page.Enable(ctx) },
|
||||
func() error { return targetClient.Runtime.Enable(ctx) },
|
||||
); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
if err := p.navigate(ctx, targetClient); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
print, err := targetClient.Page.PrintToPDF(
|
||||
ctx,
|
||||
page.NewPrintToPDFArgs().
|
||||
SetPaperWidth(p.opts.PaperWidth).
|
||||
SetPaperHeight(p.opts.PaperHeight).
|
||||
SetMarginTop(p.opts.MarginTop).
|
||||
SetMarginBottom(p.opts.MarginBottom).
|
||||
SetMarginLeft(p.opts.MarginLeft).
|
||||
SetMarginRight(p.opts.MarginRight).
|
||||
SetLandscape(p.opts.Landscape).
|
||||
SetDisplayHeaderFooter(true).
|
||||
SetHeaderTemplate(p.opts.HeaderHTML).
|
||||
SetFooterTemplate(p.opts.FooterHTML).
|
||||
SetPrintBackground(true),
|
||||
)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// connect to WebSocket URL (page) that speaks the Chrome DevTools Protocol.
|
||||
devtConn, err := rpcc.DialContext(ctx, devt.WebSocketDebuggerURL)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: 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 handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
// create a new blank target with the new browser context.
|
||||
createTargetArgs := target.
|
||||
NewCreateTargetArgs("about:blank").
|
||||
SetBrowserContextID(newContextTarget.BrowserContextID)
|
||||
newTarget, err := devtClient.Target.CreateTarget(ctx, createTargetArgs)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: 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 handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
defer newContextConn.Close() // nolint: errcheck
|
||||
// create a new CDP Client that uses newContextConn.
|
||||
targetClient := cdp.NewClient(newContextConn)
|
||||
closeTargetArgs := target.NewCloseTargetArgs(newTarget.TargetID)
|
||||
// close the target when done.
|
||||
defer targetClient.Target.CloseTarget(ctx, closeTargetArgs) // nolint: errcheck
|
||||
if err := runBatch(
|
||||
// enable all the domain events that we're interested in.
|
||||
func() error { return targetClient.DOM.Enable(ctx) },
|
||||
func() error { return targetClient.Network.Enable(ctx, network.NewEnableArgs()) },
|
||||
func() error { return targetClient.Page.Enable(ctx) },
|
||||
func() error { return targetClient.Runtime.Enable(ctx) },
|
||||
); err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
if err := p.navigate(ctx, targetClient); err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
print, err := targetClient.Page.PrintToPDF(
|
||||
ctx,
|
||||
page.NewPrintToPDFArgs().
|
||||
SetPaperWidth(p.opts.PaperWidth).
|
||||
SetPaperHeight(p.opts.PaperHeight).
|
||||
SetMarginTop(p.opts.MarginTop).
|
||||
SetMarginBottom(p.opts.MarginBottom).
|
||||
SetMarginLeft(p.opts.MarginLeft).
|
||||
SetMarginRight(p.opts.MarginRight).
|
||||
SetLandscape(p.opts.Landscape).
|
||||
SetDisplayHeaderFooter(true).
|
||||
SetHeaderTemplate(p.opts.HeaderHTML).
|
||||
SetFooterTemplate(p.opts.FooterHTML).
|
||||
SetPrintBackground(true),
|
||||
)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
if err := resolver(); err != nil {
|
||||
return timeout.Err(ctx, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *chrome) navigate(ctx context.Context, client *cdp.Client) error {
|
||||
const op = "printer.chrome.navigate"
|
||||
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}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
// NewMarkdown returns a Markdown printer.
|
||||
func NewMarkdown(fpath string, opts *ChromeOptions) (Printer, error) {
|
||||
const op = "printer.NewMarkdown"
|
||||
const op string = "printer.NewMarkdown"
|
||||
tmpl, err := template.
|
||||
New(filepath.Base(fpath)).
|
||||
Funcs(template.FuncMap{"toHTML": markdownToHTML}).
|
||||
@@ -46,7 +46,7 @@ type templateData struct {
|
||||
}
|
||||
|
||||
func markdownToHTML(dirPath, filename string) (template.HTML, error) {
|
||||
const op = "printer.markdownToHTML"
|
||||
const op string = "printer.markdownToHTML"
|
||||
fpath := fmt.Sprintf("%s/%s", dirPath, filename)
|
||||
b, err := ioutil.ReadFile(fpath)
|
||||
if err != nil {
|
||||
|
||||
@@ -29,19 +29,25 @@ func NewMerge(fpaths []string, opts *MergeOptions) Printer {
|
||||
}
|
||||
|
||||
func (p *merge) Print(destination string) error {
|
||||
const op = "printer.merge.Print"
|
||||
const op string = "printer.merge.Print"
|
||||
if p.ctx == nil {
|
||||
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
|
||||
defer cancel()
|
||||
p.ctx = ctx
|
||||
}
|
||||
var cmdArgs []string
|
||||
cmdArgs = append(cmdArgs, p.fpaths...)
|
||||
cmdArgs = append(cmdArgs, "cat", "output", destination)
|
||||
cmd := exec.CommandContext(p.ctx, "pdftk", cmdArgs...)
|
||||
_, err := cmd.Output()
|
||||
if err != nil {
|
||||
return handleErrContext(p.ctx, &standarderror.Error{Op: op, Err: err})
|
||||
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()
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := resolver(); err != nil {
|
||||
return timeout.Err(p.ctx, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -34,18 +34,24 @@ func NewOffice(fpaths []string, opts *OfficeOptions) Printer {
|
||||
}
|
||||
|
||||
func (p *office) Print(destination string) error {
|
||||
const op = "printer.office.Print"
|
||||
const op string = "printer.office.Print"
|
||||
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
|
||||
defer cancel()
|
||||
fpaths := make([]string, len(p.fpaths))
|
||||
dirPath := filepath.Dir(destination)
|
||||
for i, fpath := range p.fpaths {
|
||||
baseFilename := random.String(32)
|
||||
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
|
||||
if err := unoconv(ctx, fpath, tmpDest, p.opts); err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
resolver := func() error {
|
||||
dirPath := filepath.Dir(destination)
|
||||
for i, fpath := range p.fpaths {
|
||||
baseFilename := random.String(32)
|
||||
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}
|
||||
}
|
||||
fpaths[i] = tmpDest
|
||||
}
|
||||
fpaths[i] = tmpDest
|
||||
return nil
|
||||
}
|
||||
if err := resolver(); err != nil {
|
||||
return timeout.Err(ctx, err)
|
||||
}
|
||||
if len(fpaths) == 1 {
|
||||
if err := os.Rename(fpaths[0], destination); err != nil {
|
||||
@@ -67,7 +73,7 @@ func (p *office) Print(destination string) error {
|
||||
var mu sync.Mutex
|
||||
|
||||
func unoconv(ctx context.Context, fpath, destination string, opts *OfficeOptions) error {
|
||||
const op = "printer.unoconv"
|
||||
const op string = "printer.unoconv"
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
cmdArgs := []string{
|
||||
|
||||
@@ -1,39 +1,7 @@
|
||||
package printer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
)
|
||||
|
||||
// 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 handleErrContext(ctx context.Context, previousErr error) error {
|
||||
const op = "printer.handleErrContext"
|
||||
if previousErr == nil {
|
||||
panic(fmt.Sprintf("%s: previous error should not be nil", op))
|
||||
}
|
||||
err := ctx.Err()
|
||||
if err == nil {
|
||||
return previousErr
|
||||
}
|
||||
if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
|
||||
return &standarderror.Error{
|
||||
Code: standarderror.Timeout,
|
||||
Message: "context has timed out",
|
||||
Op: op,
|
||||
Err: previousErr,
|
||||
}
|
||||
}
|
||||
return &standarderror.Error{
|
||||
Message: "context finished with an error",
|
||||
Op: op,
|
||||
Err: previousErr,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package printer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
|
||||
"github.com/thecodingmachine/gotenberg/test"
|
||||
)
|
||||
|
||||
func TestHandlerErr(t *testing.T) {
|
||||
previousErr := errors.New("previous error")
|
||||
// should be OK.
|
||||
ctx, cancel := timeout.Context(5)
|
||||
defer cancel()
|
||||
assert.NotNil(t, handleErrContext(ctx, previousErr))
|
||||
// should timeout.
|
||||
ctx, cancel = timeout.Context(0.5)
|
||||
defer cancel()
|
||||
time.Sleep(timeout.Duration(1))
|
||||
err := handleErrContext(ctx, previousErr)
|
||||
assert.NotNil(t, err)
|
||||
standardized := test.RequireStandardError(t, err)
|
||||
assert.Equal(t, standarderror.Timeout, standardized.Code)
|
||||
// should failed.
|
||||
ctx, cancel = timeout.Context(5)
|
||||
cancel()
|
||||
err = handleErrContext(ctx, previousErr)
|
||||
assert.NotNil(t, err)
|
||||
standardized = test.RequireStandardError(t, err)
|
||||
assert.Equal(t, standarderror.Internal, standarderror.Code(err))
|
||||
}
|
||||
Reference in New Issue
Block a user