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

Commit

Permalink
Merge pull request #4 from thecodingmachine/5.0.0
Browse files Browse the repository at this point in the history
updating for Gotenberg 5.0.0
  • Loading branch information
gulien authored Apr 14, 2019
2 parents 8fcda3b + e1bf4ab commit cbf5207
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 389 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GOLANG_VERSION=1.12
GOTENBERG_VERSION=4.2.1
GOTENBERG_VERSION=5.0.0
VERSION=snapshot

# gofmt and goimports all go files.
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ A simple Go client for interacting with a Gotenberg API.
## Install

```bash
$ go get -u github.com/thecodingmachine/gotenberg-go-client/v4
$ go get -u github.com/thecodingmachine/gotenberg-go-client/v5
```

## Usage

```golang
import "github.com/thecodingmachine/gotenberg-go-client/v4"
import "github.com/thecodingmachine/gotenberg-go-client/v5"

func main() {
// HTML conversion example.
c := &gotenberg.Client{Hostname: "http://localhost:3000"}
req, _ := gotenberg.NewHTMLRequest("index.html")
req.SetHeader("header.html")
req.SetFooter("footer.html")
req.SetAssets(
req.Header("header.html")
req.Footer("footer.html")
req.Assets(
"font.woff",
"img.gif",
"style.css",
)
req.SetPaperSize(gotenberg.A4)
req.SetMargins(gotenberg.NormalMargins)
req.SetLandscape(false)
req.PaperSize(gotenberg.A4)
req.Margins(gotenberg.NormalMargins)
req.Landscape(false)
dest := "foo.pdf"
c.Store(req, dest)
}
Expand Down
4 changes: 2 additions & 2 deletions build/lint/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ FROM golang:${GOLANG_VERSION}-stretch
# | than gometalinter.
# |

ENV GOLANGCI_LINT_VERSION 1.15.0
ENV GOLANGCI_LINT_VERSION 1.16.0

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

CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl", "--disable=lll", "--disable=errcheck", "--disable=gosec", "--disable=gochecknoglobals", "--disable=gochecknoinits" ]
CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl" ]
2 changes: 1 addition & 1 deletion build/tests/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set -xe
# Testing Go client.
gotenberg &
sleep 10
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v4
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v5
sleep 10 # allows Gotenberg to remove generated files (concurrent requests).
106 changes: 106 additions & 0 deletions chrome.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package gotenberg

import (
"fmt"
"strconv"
)

const (
waitDelay string = "waitDelay"
paperWidth string = "paperWidth"
paperHeight string = "paperHeight"
marginTop string = "marginTop"
marginBottom string = "marginBottom"
marginLeft string = "marginLeft"
marginRight string = "marginRight"
landscapeChrome string = "landscape"
)

// nolint: gochecknoglobals
var (
// A3 paper size.
A3 = [2]float64{11.7, 16.5}
// A4 paper size.
A4 = [2]float64{8.27, 11.7}
// A5 paper size.
A5 = [2]float64{5.8, 8.3}
// A6 paper size.
A6 = [2]float64{4.1, 5.8}
// Letter paper size.
Letter = [2]float64{8.5, 11}
// Legal paper size.
Legal = [2]float64{8.5, 14}
// Tabloid paper size.
Tabloid = [2]float64{11, 17}
)

// nolint: gochecknoglobals
var (
// NoMargins removes margins.
NoMargins = [4]float64{0, 0, 0, 0}
// NormalMargins uses 1 inche margins.
NormalMargins = [4]float64{1, 1, 1, 1}
// LargeMargins uses 2 inche margins.
LargeMargins = [4]float64{2, 2, 2, 2}
)

type chromeRequest struct {
headerFilePath string
footerFilePath string

*request
}

func newChromeRequest() *chromeRequest {
return &chromeRequest{"", "", newRequest()}
}

// WaitDelay sets waitDelay form field.
func (req *chromeRequest) WaitDelay(delay float64) {
req.values[waitDelay] = strconv.FormatFloat(delay, 'f', 2, 64)
}

// Header sets header form file.
func (req *chromeRequest) Header(fpath string) error {
if !fileExists(fpath) {
return fmt.Errorf("%s: header file does not exist", fpath)
}
req.headerFilePath = fpath
return nil
}

// Footer sets footer form file.
func (req *chromeRequest) Footer(fpath string) error {
if !fileExists(fpath) {
return fmt.Errorf("%s: footer file does not exist", fpath)
}
req.footerFilePath = fpath
return nil
}

// PaperSize sets paperWidth and paperHeight form fields.
func (req *chromeRequest) PaperSize(size [2]float64) {
req.values[paperWidth] = fmt.Sprintf("%f", size[0])
req.values[paperHeight] = fmt.Sprintf("%f", size[1])
}

// Margins sets marginTop, marginBottom,
// marginLeft and marginRight form fields.
func (req *chromeRequest) Margins(margins [4]float64) {
req.values[marginTop] = fmt.Sprintf("%f", margins[0])
req.values[marginBottom] = fmt.Sprintf("%f", margins[1])
req.values[marginLeft] = fmt.Sprintf("%f", margins[2])
req.values[marginRight] = fmt.Sprintf("%f", margins[3])
}

// Landscape sets landscape form field.
func (req *chromeRequest) Landscape(isLandscape bool) {
req.values[landscapeChrome] = strconv.FormatBool(isLandscape)
}

func (req *chromeRequest) formFiles() map[string]string {
files := make(map[string]string)
files["header.html"] = req.headerFilePath
files["footer.html"] = req.footerFilePath
return files
}
102 changes: 40 additions & 62 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,13 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
)

const (
remoteURL string = "remoteURL"
webhookURL string = "webhookURL"
paperWidth string = "paperWidth"
paperHeight string = "paperHeight"
marginTop string = "marginTop"
marginBottom string = "marginBottom"
marginLeft string = "marginLeft"
marginRight string = "marginRight"
landscape string = "landscape"
webFontsTimeout string = "webFontsTimeout"
)

var (
// A3 paper size.
A3 = [2]float64{11.7, 16.5}
// A4 paper size.
A4 = [2]float64{8.27, 11.7}
// A5 paper size.
A5 = [2]float64{5.8, 8.3}
// A6 paper size.
A6 = [2]float64{4.1, 5.8}
// Letter paper size.
Letter = [2]float64{8.5, 11}
// Legal paper size.
Legal = [2]float64{8.5, 14}
// Tabloid paper size.
Tabloid = [2]float64{11, 17}
)

var (
// NoMargins removes margins.
NoMargins = [4]float64{0, 0, 0, 0}
// NormalMargins uses 1 inche margins.
NormalMargins = [4]float64{1, 1, 1, 1}
// LargeMargins uses 2 inche margins.
LargeMargins = [4]float64{2, 2, 2, 2}
resultFilename string = "resultFilename"
waitTimeout string = "waitTimeout"
webhookURL string = "webhookURL"
)

// Client facilitates interacting with
Expand All @@ -61,29 +29,38 @@ type Client struct {
// form values and form files to
// the Gotenberg API.
type Request interface {
SetWebhookURL(webhookURL string)
getPostURL() string
getFormValues() map[string]string
getFormFiles() map[string]string
postURL() string
formValues() map[string]string
formFiles() map[string]string
}

type request struct {
values map[string]string
}

func newRequest() *request {
return &request{
values: make(map[string]string),
}
}

// ResultFilename sets resultFilename form field.
func (req *request) ResultFilename(filename string) {
req.values[resultFilename] = filename
}

// WaitTiemout sets waitTimeout form field.
func (req *request) WaitTimeout(timeout float64) {
req.values[waitTimeout] = strconv.FormatFloat(timeout, 'f', 2, 64)
}

// ChromeRequest is a type for sending
// conversion requests which will be
// handle by Google Chrome.
type ChromeRequest interface {
SetHeader(fpath string) error
SetFooter(fpath string) error
SetPaperSize(size [2]float64)
SetMargins(margins [4]float64)
SetLandscape(isLandscape bool)
SetWebFontsTimeout(timeout int64)
// WebhookURL sets webhookURL form field.
func (req *request) WebhookURL(url string) {
req.values[webhookURL] = url
}

// UnoconvRequest is a type for sending
// conversion requests which will be
// handle by unoconv.
type UnoconvRequest interface {
SetLandscape(landscape bool)
func (req *request) formValues() map[string]string {
return req.values
}

// Post sends a request to the Gotenberg API
Expand All @@ -93,8 +70,8 @@ func (c *Client) Post(req Request) (*http.Response, error) {
if err != nil {
return nil, err
}
URL := fmt.Sprintf("%s%s", c.Hostname, req.getPostURL())
resp, err := http.Post(URL, contentType, body)
URL := fmt.Sprintf("%s%s", c.Hostname, req.postURL())
resp, err := http.Post(URL, contentType, body) /* #nosec */
if err != nil {
return nil, err
}
Expand All @@ -114,7 +91,7 @@ func (c *Client) Store(req Request, dest string) error {
}

func hasWebhook(req Request) bool {
webhookURL, ok := req.getFormValues()[webhookURL]
webhookURL, ok := req.formValues()[webhookURL]
if !ok {
return false
}
Expand All @@ -129,7 +106,7 @@ func writeNewFile(fpath string, in io.Reader) error {
if err != nil {
return fmt.Errorf("%s: creating new file: %v", fpath, err)
}
defer out.Close()
defer out.Close() // nolint: errcheck
err = out.Chmod(0644)
if err != nil && runtime.GOOS != "windows" {
return fmt.Errorf("%s: changing file mode: %v", fpath, err)
Expand All @@ -149,8 +126,8 @@ func fileExists(name string) bool {
func multipartForm(req Request) (*bytes.Buffer, string, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
defer writer.Close()
for filename, fpath := range req.getFormFiles() {
defer writer.Close() // nolint: errcheck
for filename, fpath := range req.formFiles() {
// https://github.com/thecodingmachine/gotenberg-go-client/issues/3
if fpath == "" {
continue
Expand All @@ -159,6 +136,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
if err != nil {
return nil, "", fmt.Errorf("%s: opening file: %v", filename, err)
}
defer in.Close() // nolint: errcheck
part, err := writer.CreateFormFile("files", filename)
if err != nil {
return nil, "", fmt.Errorf("%s: creating form file: %v", filename, err)
Expand All @@ -168,7 +146,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
return nil, "", fmt.Errorf("%s: copying file: %v", filename, err)
}
}
for name, value := range req.getFormValues() {
for name, value := range req.formValues() {
if err := writer.WriteField(name, value); err != nil {
return nil, "", fmt.Errorf("%s: writing form field: %v", name, err)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/thecodingmachine/gotenberg-go-client/v4
module github.com/thecodingmachine/gotenberg-go-client/v5

go 1.12

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.3.0
github.com/thecodingmachine/gotenberg-go-client/v4 v4.3.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/thecodingmachine/gotenberg-go-client/v4 v4.3.1 h1:jHoid7JbIIOHflhCIZzGpnU5YD9VHnXyeZh39s3uSKc=
github.com/thecodingmachine/gotenberg-go-client/v4 v4.3.1/go.mod h1:D3CcQY/dW6i38DL/hMiUMFwYESvXL4bs5zyyJYuHahw=
Loading

0 comments on commit cbf5207

Please sign in to comment.