Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit cbb10ad

Browse files
authored
Merge branch 'master' into MI-380
2 parents 2121b47 + 6de9f02 commit cbb10ad

File tree

19 files changed

+279
-392
lines changed

19 files changed

+279
-392
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
GOLANG_VERSION=1.12
2-
GOTENBERG_VERSION=4.2.1
1+
GOLANG_VERSION=1.13
2+
GOTENBERG_VERSION=6.0.1
33
VERSION=snapshot
4+
GOLANGCI_LINT_VERSION=1.19.1
45

56
# gofmt and goimports all go files.
67
fmt:
@@ -9,7 +10,7 @@ fmt:
910

1011
# run all linters.
1112
lint:
12-
docker build --build-arg GOLANG_VERSION=$(GOLANG_VERSION) -t thecodingmachine/gotenberg-go-client:lint -f build/lint/Dockerfile .
13+
docker build --build-arg GOLANG_VERSION=$(GOLANG_VERSION) --build-arg GOLANGCI_LINT_VERSION=$(GOLANGCI_LINT_VERSION) -t thecodingmachine/gotenberg-go-client:lint -f build/lint/Dockerfile .
1314
docker run --rm -it -v "$(PWD):/lint" thecodingmachine/gotenberg-go-client:lint
1415

1516
# run all tests.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ A simple Go client for interacting with a Gotenberg API.
55
## Install
66

77
```bash
8-
$ go get -u github.com/thecodingmachine/gotenberg-go-client/v4
8+
$ go get -u github.com/thecodingmachine/gotenberg-go-client/v6
99
```
1010

1111
## Usage
1212

1313
```golang
14-
import "github.com/thecodingmachine/gotenberg-go-client/v4"
14+
import "github.com/thecodingmachine/gotenberg-go-client/v6"
1515

1616
func main() {
1717
// HTML conversion example.
1818
c := &gotenberg.Client{Hostname: "http://localhost:3000"}
1919
req, _ := gotenberg.NewHTMLRequest("index.html")
20-
req.SetHeader("header.html")
21-
req.SetFooter("footer.html")
22-
req.SetAssets(
20+
req.Header("header.html")
21+
req.Footer("footer.html")
22+
req.Assets(
2323
"font.woff",
2424
"img.gif",
2525
"style.css",
2626
)
27-
req.SetPaperSize(gotenberg.A4)
28-
req.SetMargins(gotenberg.NormalMargins)
29-
req.SetLandscape(false)
27+
req.PaperSize(gotenberg.A4)
28+
req.Margins(gotenberg.NormalMargins)
29+
req.Landscape(false)
3030
dest := "foo.pdf"
3131
c.Store(req, dest)
3232
}

build/lint/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ FROM golang:${GOLANG_VERSION}-stretch
1010
# | than gometalinter.
1111
# |
1212

13-
ENV GOLANGCI_LINT_VERSION 1.15.0
13+
ARG GOLANGCI_LINT_VERSION
1414

1515
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b /usr/local/bin v${GOLANGCI_LINT_VERSION} &&\
1616
golangci-lint --version
@@ -32,4 +32,4 @@ COPY go.sum .
3232
# Install module dependencies.
3333
RUN go mod download
3434

35-
CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl", "--disable=lll", "--disable=errcheck", "--disable=gosec", "--disable=gochecknoglobals", "--disable=gochecknoinits" ]
35+
CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl" ]

build/tests/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ FROM golang:${GOLANG_VERSION}-stretch AS golang
55

66
FROM thecodingmachine/gotenberg:${GOTENBERG_VERSION}
77

8+
USER root
9+
810
# |--------------------------------------------------------------------------
911
# | Common libraries
1012
# |--------------------------------------------------------------------------

build/tests/docker-entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ set -xe
55
# Testing Go client.
66
gotenberg &
77
sleep 10
8-
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v4
9-
sleep 10 # allows Gotenberg to remove generated files (concurrent requests).
8+
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v6
9+
sleep 10 # allows Gotenberg to remove generated files.

chrome.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package gotenberg
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
const (
9+
waitDelay string = "waitDelay"
10+
paperWidth string = "paperWidth"
11+
paperHeight string = "paperHeight"
12+
marginTop string = "marginTop"
13+
marginBottom string = "marginBottom"
14+
marginLeft string = "marginLeft"
15+
marginRight string = "marginRight"
16+
landscapeChrome string = "landscape"
17+
googleChromeRpccBufferSize string = "googleChromeRpccBufferSize"
18+
)
19+
20+
// nolint: gochecknoglobals
21+
var (
22+
// A3 paper size.
23+
A3 = [2]float64{11.7, 16.5}
24+
// A4 paper size.
25+
A4 = [2]float64{8.27, 11.7}
26+
// A5 paper size.
27+
A5 = [2]float64{5.8, 8.3}
28+
// A6 paper size.
29+
A6 = [2]float64{4.1, 5.8}
30+
// Letter paper size.
31+
Letter = [2]float64{8.5, 11}
32+
// Legal paper size.
33+
Legal = [2]float64{8.5, 14}
34+
// Tabloid paper size.
35+
Tabloid = [2]float64{11, 17}
36+
)
37+
38+
// nolint: gochecknoglobals
39+
var (
40+
// NoMargins removes margins.
41+
NoMargins = [4]float64{0, 0, 0, 0}
42+
// NormalMargins uses 1 inche margins.
43+
NormalMargins = [4]float64{1, 1, 1, 1}
44+
// LargeMargins uses 2 inche margins.
45+
LargeMargins = [4]float64{2, 2, 2, 2}
46+
)
47+
48+
type chromeRequest struct {
49+
headerFilePath string
50+
footerFilePath string
51+
52+
*request
53+
}
54+
55+
func newChromeRequest() *chromeRequest {
56+
return &chromeRequest{"", "", newRequest()}
57+
}
58+
59+
// WaitDelay sets waitDelay form field.
60+
func (req *chromeRequest) WaitDelay(delay float64) {
61+
req.values[waitDelay] = strconv.FormatFloat(delay, 'f', 2, 64)
62+
}
63+
64+
// Header sets header form file.
65+
func (req *chromeRequest) Header(fpath string) error {
66+
if !fileExists(fpath) {
67+
return fmt.Errorf("%s: header file does not exist", fpath)
68+
}
69+
req.headerFilePath = fpath
70+
return nil
71+
}
72+
73+
// Footer sets footer form file.
74+
func (req *chromeRequest) Footer(fpath string) error {
75+
if !fileExists(fpath) {
76+
return fmt.Errorf("%s: footer file does not exist", fpath)
77+
}
78+
req.footerFilePath = fpath
79+
return nil
80+
}
81+
82+
// PaperSize sets paperWidth and paperHeight form fields.
83+
func (req *chromeRequest) PaperSize(size [2]float64) {
84+
req.values[paperWidth] = fmt.Sprintf("%f", size[0])
85+
req.values[paperHeight] = fmt.Sprintf("%f", size[1])
86+
}
87+
88+
// Margins sets marginTop, marginBottom,
89+
// marginLeft and marginRight form fields.
90+
func (req *chromeRequest) Margins(margins [4]float64) {
91+
req.values[marginTop] = fmt.Sprintf("%f", margins[0])
92+
req.values[marginBottom] = fmt.Sprintf("%f", margins[1])
93+
req.values[marginLeft] = fmt.Sprintf("%f", margins[2])
94+
req.values[marginRight] = fmt.Sprintf("%f", margins[3])
95+
}
96+
97+
// Landscape sets landscape form field.
98+
func (req *chromeRequest) Landscape(isLandscape bool) {
99+
req.values[landscapeChrome] = strconv.FormatBool(isLandscape)
100+
}
101+
102+
// GoogleChromeRpccBufferSize sets googleChromeRpccBufferSize form field.
103+
func (req *chromeRequest) GoogleChromeRpccBufferSize(bufferSize int64) {
104+
req.values[googleChromeRpccBufferSize] = strconv.FormatInt(bufferSize, 10)
105+
}
106+
107+
func (req *chromeRequest) formFiles() map[string]string {
108+
files := make(map[string]string)
109+
files["header.html"] = req.headerFilePath
110+
files["footer.html"] = req.footerFilePath
111+
return files
112+
}

client.go

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,14 @@ import (
1010
"os"
1111
"path/filepath"
1212
"runtime"
13+
"strconv"
1314
)
1415

1516
const (
16-
remoteURL string = "remoteURL"
17-
webhookURL string = "webhookURL"
18-
paperWidth string = "paperWidth"
19-
paperHeight string = "paperHeight"
20-
marginTop string = "marginTop"
21-
marginBottom string = "marginBottom"
22-
marginLeft string = "marginLeft"
23-
marginRight string = "marginRight"
24-
landscape string = "landscape"
25-
webFontsTimeout string = "webFontsTimeout"
26-
)
27-
28-
var (
29-
// A3 paper size.
30-
A3 = [2]float64{11.7, 16.5}
31-
// A4 paper size.
32-
A4 = [2]float64{8.27, 11.7}
33-
// A5 paper size.
34-
A5 = [2]float64{5.8, 8.3}
35-
// A6 paper size.
36-
A6 = [2]float64{4.1, 5.8}
37-
// Letter paper size.
38-
Letter = [2]float64{8.5, 11}
39-
// Legal paper size.
40-
Legal = [2]float64{8.5, 14}
41-
// Tabloid paper size.
42-
Tabloid = [2]float64{11, 17}
43-
)
44-
45-
var (
46-
// NoMargins removes margins.
47-
NoMargins = [4]float64{0, 0, 0, 0}
48-
// NormalMargins uses 1 inche margins.
49-
NormalMargins = [4]float64{1, 1, 1, 1}
50-
// LargeMargins uses 2 inche margins.
51-
LargeMargins = [4]float64{2, 2, 2, 2}
17+
resultFilename string = "resultFilename"
18+
waitTimeout string = "waitTimeout"
19+
webhookURL string = "webhookURL"
20+
webhookURLTimeout string = "webhookURLTimeout"
5221
)
5322

5423
// Client facilitates interacting with
@@ -61,29 +30,43 @@ type Client struct {
6130
// form values and form files to
6231
// the Gotenberg API.
6332
type Request interface {
64-
SetWebhookURL(webhookURL string)
65-
getPostURL() string
66-
getFormValues() map[string]string
67-
getFormFiles() map[string]string
33+
postURL() string
34+
formValues() map[string]string
35+
formFiles() map[string]string
36+
}
37+
38+
type request struct {
39+
values map[string]string
40+
}
41+
42+
func newRequest() *request {
43+
return &request{
44+
values: make(map[string]string),
45+
}
46+
}
47+
48+
// ResultFilename sets resultFilename form field.
49+
func (req *request) ResultFilename(filename string) {
50+
req.values[resultFilename] = filename
51+
}
52+
53+
// WaitTiemout sets waitTimeout form field.
54+
func (req *request) WaitTimeout(timeout float64) {
55+
req.values[waitTimeout] = strconv.FormatFloat(timeout, 'f', 2, 64)
56+
}
57+
58+
// WebhookURL sets webhookURL form field.
59+
func (req *request) WebhookURL(url string) {
60+
req.values[webhookURL] = url
6861
}
6962

70-
// ChromeRequest is a type for sending
71-
// conversion requests which will be
72-
// handle by Google Chrome.
73-
type ChromeRequest interface {
74-
SetHeader(fpath string) error
75-
SetFooter(fpath string) error
76-
SetPaperSize(size [2]float64)
77-
SetMargins(margins [4]float64)
78-
SetLandscape(isLandscape bool)
79-
SetWebFontsTimeout(timeout int64)
63+
// WebhookURLTimeout sets webhookURLTimeout form field.
64+
func (req *request) WebhookURLTimeout(timeout float64) {
65+
req.values[webhookURLTimeout] = strconv.FormatFloat(timeout, 'f', 2, 64)
8066
}
8167

82-
// UnoconvRequest is a type for sending
83-
// conversion requests which will be
84-
// handle by unoconv.
85-
type UnoconvRequest interface {
86-
SetLandscape(landscape bool)
68+
func (req *request) formValues() map[string]string {
69+
return req.values
8770
}
8871

8972
// Post sends a request to the Gotenberg API
@@ -93,8 +76,8 @@ func (c *Client) Post(req Request) (*http.Response, error) {
9376
if err != nil {
9477
return nil, err
9578
}
96-
URL := fmt.Sprintf("%s%s", c.Hostname, req.getPostURL())
97-
resp, err := http.Post(URL, contentType, body)
79+
URL := fmt.Sprintf("%s%s", c.Hostname, req.postURL())
80+
resp, err := http.Post(URL, contentType, body) /* #nosec */
9881
if err != nil {
9982
return nil, err
10083
}
@@ -110,15 +93,17 @@ func (c *Client) Store(req Request, dest string) error {
11093
if err != nil {
11194
return err
11295
}
113-
// Check for 2XX Status Codes
96+
defer resp.Body.Close()
97+
98+
// Check for 2XX Status Codes
11499
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
115100
return errors.New("failed to generate the result PDF")
116101
}
117102
return writeNewFile(dest, resp.Body)
118103
}
119104

120105
func hasWebhook(req Request) bool {
121-
webhookURL, ok := req.getFormValues()[webhookURL]
106+
webhookURL, ok := req.formValues()[webhookURL]
122107
if !ok {
123108
return false
124109
}
@@ -133,7 +118,7 @@ func writeNewFile(fpath string, in io.Reader) error {
133118
if err != nil {
134119
return fmt.Errorf("%s: creating new file: %v", fpath, err)
135120
}
136-
defer out.Close()
121+
defer out.Close() // nolint: errcheck
137122
err = out.Chmod(0644)
138123
if err != nil && runtime.GOOS != "windows" {
139124
return fmt.Errorf("%s: changing file mode: %v", fpath, err)
@@ -153,8 +138,8 @@ func fileExists(name string) bool {
153138
func multipartForm(req Request) (*bytes.Buffer, string, error) {
154139
body := &bytes.Buffer{}
155140
writer := multipart.NewWriter(body)
156-
defer writer.Close()
157-
for filename, fpath := range req.getFormFiles() {
141+
defer writer.Close() // nolint: errcheck
142+
for filename, fpath := range req.formFiles() {
158143
// https://github.com/thecodingmachine/gotenberg-go-client/issues/3
159144
if fpath == "" {
160145
continue
@@ -163,6 +148,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
163148
if err != nil {
164149
return nil, "", fmt.Errorf("%s: opening file: %v", filename, err)
165150
}
151+
defer in.Close() // nolint: errcheck
166152
part, err := writer.CreateFormFile("files", filename)
167153
if err != nil {
168154
return nil, "", fmt.Errorf("%s: creating form file: %v", filename, err)
@@ -172,7 +158,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
172158
return nil, "", fmt.Errorf("%s: copying file: %v", filename, err)
173159
}
174160
}
175-
for name, value := range req.getFormValues() {
161+
for name, value := range req.formValues() {
176162
if err := writer.WriteField(name, value); err != nil {
177163
return nil, "", fmt.Errorf("%s: writing form field: %v", name, err)
178164
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
module github.com/thecodingmachine/gotenberg-go-client/v4
1+
module github.com/thecodingmachine/gotenberg-go-client/v6
22

3-
go 1.12
3+
go 1.13
44

55
require (
66
github.com/davecgh/go-spew v1.1.1 // indirect

0 commit comments

Comments
 (0)