From f68e1c1ac5fed758582ff60eb6aeafb5a7d85e63 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Sun, 14 Apr 2019 16:28:48 +0200 Subject: [PATCH 1/2] updating for Gotenberg 5.0.0 --- Makefile | 2 +- README.md | 16 ++--- build/lint/Dockerfile | 4 +- build/tests/docker-entrypoint.sh | 2 +- chrome.go | 106 +++++++++++++++++++++++++++++++ client.go | 102 ++++++++++++----------------- go.mod | 4 +- go.sum | 2 + html.go | 79 ++++------------------- html_test.go | 29 ++++----- markdown.go | 85 ++++--------------------- markdown_test.go | 29 ++++----- merge.go | 22 ++----- merge_test.go | 10 +-- office.go | 29 ++++----- office_test.go | 12 +--- url.go | 73 ++------------------- url_test.go | 27 ++++---- 18 files changed, 249 insertions(+), 384 deletions(-) create mode 100644 chrome.go diff --git a/Makefile b/Makefile index a7d466c..4169a7d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ GOLANG_VERSION=1.12 -GOTENBERG_VERSION=4.2.1 +GOTENBERG_VERSION=snapshot VERSION=snapshot # gofmt and goimports all go files. diff --git a/README.md b/README.md index 6a05aa7..72b3c57 100644 --- a/README.md +++ b/README.md @@ -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) } diff --git a/build/lint/Dockerfile b/build/lint/Dockerfile index 05c3675..937f9fb 100644 --- a/build/lint/Dockerfile +++ b/build/lint/Dockerfile @@ -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 @@ -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" ] \ No newline at end of file +CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl" ] \ No newline at end of file diff --git a/build/tests/docker-entrypoint.sh b/build/tests/docker-entrypoint.sh index b4c4e78..997acbc 100755 --- a/build/tests/docker-entrypoint.sh +++ b/build/tests/docker-entrypoint.sh @@ -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). \ No newline at end of file diff --git a/chrome.go b/chrome.go new file mode 100644 index 0000000..09e4381 --- /dev/null +++ b/chrome.go @@ -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()} +} + +func (req *chromeRequest) formFiles() map[string]string { + files := make(map[string]string) + files["header.html"] = req.headerFilePath + files["footer.html"] = req.footerFilePath + return files +} + +// 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) +} diff --git a/client.go b/client.go index 0fc4ba9..2b63c3b 100644 --- a/client.go +++ b/client.go @@ -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 @@ -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 @@ -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 } @@ -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 } @@ -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) @@ -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 @@ -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) @@ -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) } diff --git a/go.mod b/go.mod index a21b429..7bc632b 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4f89841..de5e395 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/html.go b/html.go index 39686ab..d7b20b3 100644 --- a/html.go +++ b/html.go @@ -3,17 +3,15 @@ package gotenberg import ( "fmt" "path/filepath" - "strconv" ) // HTMLRequest facilitates HTML conversion // with the Gotenberg API. type HTMLRequest struct { indexFilePath string - headerFilePath string - footerFilePath string assetFilePaths []string - values map[string]string + + *chromeRequest } // NewHTMLRequest create HTMLRequest. @@ -21,82 +19,30 @@ func NewHTMLRequest(indexFilePath string) (*HTMLRequest, error) { if !fileExists(indexFilePath) { return nil, fmt.Errorf("%s: index file does not exist", indexFilePath) } - return &HTMLRequest{indexFilePath: indexFilePath, values: make(map[string]string)}, nil -} - -// SetWebhookURL sets webhookURL form field. -func (html *HTMLRequest) SetWebhookURL(webhookURL string) { - html.values[webhookURL] = webhookURL -} - -// SetHeader sets header form file. -func (html *HTMLRequest) SetHeader(fpath string) error { - if !fileExists(fpath) { - return fmt.Errorf("%s: header file does not exist", fpath) - } - html.headerFilePath = fpath - return nil -} - -// SetFooter sets footer form file. -func (html *HTMLRequest) SetFooter(fpath string) error { - if !fileExists(fpath) { - return fmt.Errorf("%s: footer file does not exist", fpath) - } - html.footerFilePath = fpath - return nil + return &HTMLRequest{indexFilePath, nil, newChromeRequest()}, nil } -// SetAssets sets assets form files. -func (html *HTMLRequest) SetAssets(fpaths ...string) error { +// Assets sets assets form files. +func (req *HTMLRequest) Assets(fpaths ...string) error { for _, fpath := range fpaths { if !fileExists(fpath) { return fmt.Errorf("%s: file does not exist", fpath) } } - html.assetFilePaths = fpaths + req.assetFilePaths = fpaths return nil } -// SetPaperSize sets paperWidth and paperHeight form fields. -func (html *HTMLRequest) SetPaperSize(size [2]float64) { - html.values[paperWidth] = fmt.Sprintf("%f", size[0]) - html.values[paperHeight] = fmt.Sprintf("%f", size[1]) -} - -// SetMargins sets marginTop, marginBottom, -// marginLeft and marginRight form fields. -func (html *HTMLRequest) SetMargins(margins [4]float64) { - html.values[marginTop] = fmt.Sprintf("%f", margins[0]) - html.values[marginBottom] = fmt.Sprintf("%f", margins[1]) - html.values[marginLeft] = fmt.Sprintf("%f", margins[2]) - html.values[marginRight] = fmt.Sprintf("%f", margins[3]) -} - -// SetLandscape sets landscape form field. -func (html *HTMLRequest) SetLandscape(isLandscape bool) { - html.values[landscape] = strconv.FormatBool(isLandscape) -} - -// SetWebFontsTimeout sets webFontsTimeout form field. -func (html *HTMLRequest) SetWebFontsTimeout(timeout int64) { - html.values[webFontsTimeout] = strconv.FormatInt(timeout, 10) -} - -func (html *HTMLRequest) getPostURL() string { +func (req *HTMLRequest) postURL() string { return "/convert/html" } -func (html *HTMLRequest) getFormValues() map[string]string { - return html.values -} - -func (html *HTMLRequest) getFormFiles() map[string]string { +func (req *HTMLRequest) formFiles() map[string]string { files := make(map[string]string) - files["index.html"] = html.indexFilePath - files["header.html"] = html.headerFilePath - files["footer.html"] = html.footerFilePath - for _, fpath := range html.assetFilePaths { + files["index.html"] = req.indexFilePath + files["header.html"] = req.headerFilePath + files["footer.html"] = req.footerFilePath + for _, fpath := range req.assetFilePaths { files[filepath.Base(fpath)] = fpath } return files @@ -105,5 +51,4 @@ func (html *HTMLRequest) getFormFiles() map[string]string { // Compile-time checks to ensure type implements desired interfaces. var ( _ = Request(new(HTMLRequest)) - _ = ChromeRequest(new(HTMLRequest)) ) diff --git a/html_test.go b/html_test.go index 12993e7..5c3ea0d 100644 --- a/html_test.go +++ b/html_test.go @@ -14,19 +14,22 @@ func TestHTML(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} req, err := NewHTMLRequest(test.HTMLTestFilePath(t, "index.html")) require.Nil(t, err) - err = req.SetHeader(test.HTMLTestFilePath(t, "header.html")) + req.ResultFilename("foo.pdf") + req.WaitTimeout(5) + req.WaitDelay(1) + err = req.Header(test.HTMLTestFilePath(t, "header.html")) require.Nil(t, err) - err = req.SetFooter(test.HTMLTestFilePath(t, "footer.html")) + err = req.Footer(test.HTMLTestFilePath(t, "footer.html")) require.Nil(t, err) - err = req.SetAssets( + err = req.Assets( test.HTMLTestFilePath(t, "font.woff"), test.HTMLTestFilePath(t, "img.gif"), test.HTMLTestFilePath(t, "style.css"), ) require.Nil(t, err) - req.SetPaperSize(A4) - req.SetMargins(NormalMargins) - req.SetLandscape(false) + req.PaperSize(A4) + req.Margins(NormalMargins) + req.Landscape(false) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -41,9 +44,9 @@ func TestHTMLWithoutHeaderFooter(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} req, err := NewHTMLRequest(test.HTMLTestFilePath(t, "index.html")) require.Nil(t, err) - req.SetPaperSize(A4) - req.SetMargins(NormalMargins) - req.SetLandscape(false) + req.PaperSize(A4) + req.Margins(NormalMargins) + req.Landscape(false) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -53,11 +56,3 @@ func TestHTMLWithoutHeaderFooter(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } - -func TestConcurrentHTML(t *testing.T) { - for i := 0; i < 10; i++ { - go func() { - TestHTML(t) - }() - } -} diff --git a/markdown.go b/markdown.go index 3ef045d..9af5c88 100644 --- a/markdown.go +++ b/markdown.go @@ -3,7 +3,6 @@ package gotenberg import ( "fmt" "path/filepath" - "strconv" ) // MarkdownRequest facilitates Markdown conversion @@ -11,10 +10,9 @@ import ( type MarkdownRequest struct { indexFilePath string markdownFilePaths []string - headerFilePath string - footerFilePath string assetFilePaths []string - values map[string]string + + *chromeRequest } // NewMarkdownRequest create MarkdownRequest. @@ -27,89 +25,33 @@ func NewMarkdownRequest(indexFilePath string, markdownFilePaths ...string) (*Mar return nil, fmt.Errorf("%s: markdown file does not exist", fpath) } } - return &MarkdownRequest{ - indexFilePath: indexFilePath, - markdownFilePaths: markdownFilePaths, - values: make(map[string]string), - }, nil -} - -// SetWebhookURL sets webhookURL form field. -func (markdown *MarkdownRequest) SetWebhookURL(webhookURL string) { - markdown.values[webhookURL] = webhookURL -} - -// SetHeader sets header form file. -func (markdown *MarkdownRequest) SetHeader(fpath string) error { - if !fileExists(fpath) { - return fmt.Errorf("%s: header file does not exist", fpath) - } - markdown.headerFilePath = fpath - return nil -} - -// SetFooter sets footer form file. -func (markdown *MarkdownRequest) SetFooter(fpath string) error { - if !fileExists(fpath) { - return fmt.Errorf("%s: footer file does not exist", fpath) - } - markdown.footerFilePath = fpath - return nil + return &MarkdownRequest{indexFilePath, markdownFilePaths, nil, newChromeRequest()}, nil } -// SetAssets sets assets form files. -func (markdown *MarkdownRequest) SetAssets(fpaths ...string) error { +// Assets sets assets form files. +func (req *MarkdownRequest) Assets(fpaths ...string) error { for _, fpath := range fpaths { if !fileExists(fpath) { return fmt.Errorf("%s: file does not exist", fpath) } } - markdown.assetFilePaths = fpaths + req.assetFilePaths = fpaths return nil } -// SetPaperSize sets paperWidth and paperHeight form fields. -func (markdown *MarkdownRequest) SetPaperSize(size [2]float64) { - markdown.values[paperWidth] = fmt.Sprintf("%f", size[0]) - markdown.values[paperHeight] = fmt.Sprintf("%f", size[1]) -} - -// SetMargins sets marginTop, marginBottom, -// marginLeft and marginRight form fields. -func (markdown *MarkdownRequest) SetMargins(margins [4]float64) { - markdown.values[marginTop] = fmt.Sprintf("%f", margins[0]) - markdown.values[marginBottom] = fmt.Sprintf("%f", margins[1]) - markdown.values[marginLeft] = fmt.Sprintf("%f", margins[2]) - markdown.values[marginRight] = fmt.Sprintf("%f", margins[3]) -} - -// SetLandscape sets landscape form field. -func (markdown *MarkdownRequest) SetLandscape(isLandscape bool) { - markdown.values[landscape] = strconv.FormatBool(isLandscape) -} - -// SetWebFontsTimeout sets webFontsTimeout form field. -func (markdown *MarkdownRequest) SetWebFontsTimeout(timeout int64) { - markdown.values[webFontsTimeout] = strconv.FormatInt(timeout, 10) -} - -func (markdown *MarkdownRequest) getPostURL() string { +func (req *MarkdownRequest) postURL() string { return "/convert/markdown" } -func (markdown *MarkdownRequest) getFormValues() map[string]string { - return markdown.values -} - -func (markdown *MarkdownRequest) getFormFiles() map[string]string { +func (req *MarkdownRequest) formFiles() map[string]string { files := make(map[string]string) - files["index.html"] = markdown.indexFilePath - files["header.html"] = markdown.headerFilePath - files["footer.html"] = markdown.footerFilePath - for _, fpath := range markdown.markdownFilePaths { + files["index.html"] = req.indexFilePath + files["header.html"] = req.headerFilePath + files["footer.html"] = req.footerFilePath + for _, fpath := range req.markdownFilePaths { files[filepath.Base(fpath)] = fpath } - for _, fpath := range markdown.assetFilePaths { + for _, fpath := range req.assetFilePaths { files[filepath.Base(fpath)] = fpath } return files @@ -118,5 +60,4 @@ func (markdown *MarkdownRequest) getFormFiles() map[string]string { // Compile-time checks to ensure type implements desired interfaces. var ( _ = Request(new(MarkdownRequest)) - _ = ChromeRequest(new(MarkdownRequest)) ) diff --git a/markdown_test.go b/markdown_test.go index cd6f146..704bd40 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -19,19 +19,22 @@ func TestMarkdown(t *testing.T) { test.MarkdownTestFilePath(t, "paragraph3.md"), ) require.Nil(t, err) - err = req.SetHeader(test.MarkdownTestFilePath(t, "header.html")) + req.ResultFilename("foo.pdf") + req.WaitTimeout(5) + req.WaitDelay(1) + err = req.Header(test.MarkdownTestFilePath(t, "header.html")) require.Nil(t, err) - err = req.SetFooter(test.MarkdownTestFilePath(t, "footer.html")) + err = req.Footer(test.MarkdownTestFilePath(t, "footer.html")) require.Nil(t, err) - err = req.SetAssets( + err = req.Assets( test.MarkdownTestFilePath(t, "font.woff"), test.MarkdownTestFilePath(t, "img.gif"), test.MarkdownTestFilePath(t, "style.css"), ) require.Nil(t, err) - req.SetPaperSize(A4) - req.SetMargins(NormalMargins) - req.SetLandscape(false) + req.PaperSize(A4) + req.Margins(NormalMargins) + req.Landscape(false) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -51,9 +54,9 @@ func TestMarkdownWithoutHeaderFooter(t *testing.T) { test.MarkdownTestFilePath(t, "paragraph3.md"), ) require.Nil(t, err) - req.SetPaperSize(A4) - req.SetMargins(NormalMargins) - req.SetLandscape(false) + req.PaperSize(A4) + req.Margins(NormalMargins) + req.Landscape(false) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -63,11 +66,3 @@ func TestMarkdownWithoutHeaderFooter(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } - -func TestConcurrentMarkdown(t *testing.T) { - for i := 0; i < 10; i++ { - go func() { - TestMarkdown(t) - }() - } -} diff --git a/merge.go b/merge.go index 9b5d854..844f86a 100644 --- a/merge.go +++ b/merge.go @@ -9,7 +9,8 @@ import ( // with the Gotenberg API. type MergeRequest struct { filePaths []string - values map[string]string + + *request } // NewMergeRequest create MergeRequest. @@ -19,25 +20,16 @@ func NewMergeRequest(fpaths ...string) (*MergeRequest, error) { return nil, fmt.Errorf("%s: file does not exist", fpath) } } - return &MergeRequest{filePaths: fpaths, values: make(map[string]string)}, nil -} - -// SetWebhookURL sets webhookURL form field. -func (merge *MergeRequest) SetWebhookURL(webhookURL string) { - merge.values[webhookURL] = webhookURL -} - -func (merge *MergeRequest) getPostURL() string { - return "/merge" + return &MergeRequest{fpaths, newRequest()}, nil } -func (merge *MergeRequest) getFormValues() map[string]string { - return merge.values +func (req *MergeRequest) postURL() string { + return "/convert/merge" } -func (merge *MergeRequest) getFormFiles() map[string]string { +func (req *MergeRequest) formFiles() map[string]string { files := make(map[string]string) - for _, fpath := range merge.filePaths { + for _, fpath := range req.filePaths { files[filepath.Base(fpath)] = fpath } return files diff --git a/merge_test.go b/merge_test.go index 2600d06..d66926d 100644 --- a/merge_test.go +++ b/merge_test.go @@ -17,6 +17,8 @@ func TestMerge(t *testing.T) { test.PDFTestFilePath(t, "gotenberg.pdf"), ) require.Nil(t, err) + req.ResultFilename("foo.pdf") + req.WaitTimeout(5) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -26,11 +28,3 @@ func TestMerge(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } - -func TestConcurrentMerge(t *testing.T) { - for i := 0; i < 10; i++ { - go func() { - TestMerge(t) - }() - } -} diff --git a/office.go b/office.go index 14e6b24..b519baf 100644 --- a/office.go +++ b/office.go @@ -6,11 +6,14 @@ import ( "strconv" ) +const landscapeOffice string = "landscape" + // OfficeRequest facilitates Office documents // conversion with the Gotenberg API. type OfficeRequest struct { filePaths []string - values map[string]string + + *request } // NewOfficeRequest create OfficeRequest. @@ -20,30 +23,21 @@ func NewOfficeRequest(fpaths ...string) (*OfficeRequest, error) { return nil, fmt.Errorf("%s: file does not exist", fpath) } } - return &OfficeRequest{filePaths: fpaths, values: make(map[string]string)}, nil -} - -// SetWebhookURL sets webhookURL form field. -func (office *OfficeRequest) SetWebhookURL(webhookURL string) { - office.values[webhookURL] = webhookURL + return &OfficeRequest{fpaths, newRequest()}, nil } -// SetLandscape sets landscape form field. -func (office *OfficeRequest) SetLandscape(isLandscape bool) { - office.values[landscape] = strconv.FormatBool(isLandscape) +// Landscape sets landscape form field. +func (req *OfficeRequest) Landscape(isLandscape bool) { + req.values[landscapeOffice] = strconv.FormatBool(isLandscape) } -func (office *OfficeRequest) getPostURL() string { +func (req *OfficeRequest) postURL() string { return "/convert/office" } -func (office *OfficeRequest) getFormValues() map[string]string { - return office.values -} - -func (office *OfficeRequest) getFormFiles() map[string]string { +func (req *OfficeRequest) formFiles() map[string]string { files := make(map[string]string) - for _, fpath := range office.filePaths { + for _, fpath := range req.filePaths { files[filepath.Base(fpath)] = fpath } return files @@ -52,5 +46,4 @@ func (office *OfficeRequest) getFormFiles() map[string]string { // Compile-time checks to ensure type implements desired interfaces. var ( _ = Request(new(OfficeRequest)) - _ = UnoconvRequest(new(OfficeRequest)) ) diff --git a/office_test.go b/office_test.go index 96e7763..95bbebf 100644 --- a/office_test.go +++ b/office_test.go @@ -14,7 +14,9 @@ func TestOffice(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} req, err := NewOfficeRequest(test.OfficeTestFilePath(t, "document.docx")) require.Nil(t, err) - req.SetLandscape(false) + req.ResultFilename("foo.pdf") + req.WaitTimeout(5) + req.Landscape(false) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -24,11 +26,3 @@ func TestOffice(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } - -func TestConcurrentOffice(t *testing.T) { - for i := 0; i < 10; i++ { - go func() { - TestOffice(t) - }() - } -} diff --git a/url.go b/url.go index 9c2a0c3..ef52bbc 100644 --- a/url.go +++ b/url.go @@ -1,90 +1,25 @@ package gotenberg -import ( - "fmt" - "strconv" -) +const remoteURL string = "remoteURL" // URLRequest facilitates remote URL conversion // with the Gotenberg API. type URLRequest struct { - headerFilePath string - footerFilePath string - values map[string]string + *chromeRequest } // NewURLRequest create URLRequest. func NewURLRequest(url string) *URLRequest { - req := &URLRequest{values: make(map[string]string)} + req := &URLRequest{newChromeRequest()} req.values[remoteURL] = url return req } -// SetWebhookURL sets webhookURL form field. -func (url *URLRequest) SetWebhookURL(webhookURL string) { - url.values[webhookURL] = webhookURL -} - -// SetHeader sets header form file. -func (url *URLRequest) SetHeader(fpath string) error { - if !fileExists(fpath) { - return fmt.Errorf("%s: header file does not exist", fpath) - } - url.headerFilePath = fpath - return nil -} - -// SetFooter sets footer form file. -func (url *URLRequest) SetFooter(fpath string) error { - if !fileExists(fpath) { - return fmt.Errorf("%s: footer file does not exist", fpath) - } - url.footerFilePath = fpath - return nil -} - -// SetPaperSize sets paperWidth and paperHeight form fields. -func (url *URLRequest) SetPaperSize(size [2]float64) { - url.values[paperWidth] = fmt.Sprintf("%f", size[0]) - url.values[paperHeight] = fmt.Sprintf("%f", size[1]) -} - -// SetMargins sets marginTop, marginBottom, -// marginLeft and marginRight form fields. -func (url *URLRequest) SetMargins(margins [4]float64) { - url.values[marginTop] = fmt.Sprintf("%f", margins[0]) - url.values[marginBottom] = fmt.Sprintf("%f", margins[1]) - url.values[marginLeft] = fmt.Sprintf("%f", margins[2]) - url.values[marginRight] = fmt.Sprintf("%f", margins[3]) -} - -// SetLandscape sets landscape form field. -func (url *URLRequest) SetLandscape(isLandscape bool) { - url.values[landscape] = strconv.FormatBool(isLandscape) -} - -// SetWebFontsTimeout sets webFontsTimeout form field. -func (url *URLRequest) SetWebFontsTimeout(timeout int64) { - url.values[webFontsTimeout] = strconv.FormatInt(timeout, 10) -} - -func (url *URLRequest) getPostURL() string { +func (url *URLRequest) postURL() string { return "/convert/url" } -func (url *URLRequest) getFormValues() map[string]string { - return url.values -} - -func (url *URLRequest) getFormFiles() map[string]string { - files := make(map[string]string) - files["header.html"] = url.headerFilePath - files["footer.html"] = url.footerFilePath - return files -} - // Compile-time checks to ensure type implements desired interfaces. var ( _ = Request(new(URLRequest)) - _ = ChromeRequest(new(URLRequest)) ) diff --git a/url_test.go b/url_test.go index 9f0e8ca..17af188 100644 --- a/url_test.go +++ b/url_test.go @@ -13,13 +13,16 @@ import ( func TestURL(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} req := NewURLRequest("http://google.com") - err := req.SetHeader(test.URLTestFilePath(t, "header.html")) + req.ResultFilename("foo.pdf") + req.WaitTimeout(5) + req.WaitDelay(1) + err := req.Header(test.URLTestFilePath(t, "header.html")) require.Nil(t, err) - err = req.SetFooter(test.URLTestFilePath(t, "footer.html")) + err = req.Footer(test.URLTestFilePath(t, "footer.html")) require.Nil(t, err) - req.SetPaperSize(A4) - req.SetMargins(NormalMargins) - req.SetLandscape(false) + req.PaperSize(A4) + req.Margins(NormalMargins) + req.Landscape(false) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -33,9 +36,9 @@ func TestURL(t *testing.T) { func TestURLWithoutHeaderFooter(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} req := NewURLRequest("http://google.com") - req.SetPaperSize(A4) - req.SetMargins(NormalMargins) - req.SetLandscape(false) + req.PaperSize(A4) + req.Margins(NormalMargins) + req.Landscape(false) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -45,11 +48,3 @@ func TestURLWithoutHeaderFooter(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } - -func TestConcurrentURL(t *testing.T) { - for i := 0; i < 10; i++ { - go func() { - TestURL(t) - }() - } -} From e1bf4ab32e0567400cfc0cecf8abebeac12f7ea0 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Sun, 14 Apr 2019 17:29:59 +0200 Subject: [PATCH 2/2] fixing issues --- Makefile | 2 +- chrome.go | 14 +++++++------- html_test.go | 2 +- markdown_test.go | 2 +- merge_test.go | 2 +- office_test.go | 2 +- url_test.go | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 4169a7d..09f06a7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ GOLANG_VERSION=1.12 -GOTENBERG_VERSION=snapshot +GOTENBERG_VERSION=5.0.0 VERSION=snapshot # gofmt and goimports all go files. diff --git a/chrome.go b/chrome.go index 09e4381..afe97c1 100644 --- a/chrome.go +++ b/chrome.go @@ -55,13 +55,6 @@ func newChromeRequest() *chromeRequest { return &chromeRequest{"", "", newRequest()} } -func (req *chromeRequest) formFiles() map[string]string { - files := make(map[string]string) - files["header.html"] = req.headerFilePath - files["footer.html"] = req.footerFilePath - return files -} - // WaitDelay sets waitDelay form field. func (req *chromeRequest) WaitDelay(delay float64) { req.values[waitDelay] = strconv.FormatFloat(delay, 'f', 2, 64) @@ -104,3 +97,10 @@ func (req *chromeRequest) Margins(margins [4]float64) { 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 +} diff --git a/html_test.go b/html_test.go index 5c3ea0d..22477b8 100644 --- a/html_test.go +++ b/html_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v4/test" + "github.com/thecodingmachine/gotenberg-go-client/v5/test" ) func TestHTML(t *testing.T) { diff --git a/markdown_test.go b/markdown_test.go index 704bd40..c376e0e 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v4/test" + "github.com/thecodingmachine/gotenberg-go-client/v5/test" ) func TestMarkdown(t *testing.T) { diff --git a/merge_test.go b/merge_test.go index d66926d..5823b1d 100644 --- a/merge_test.go +++ b/merge_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v4/test" + "github.com/thecodingmachine/gotenberg-go-client/v5/test" ) func TestMerge(t *testing.T) { diff --git a/office_test.go b/office_test.go index 95bbebf..1af7b22 100644 --- a/office_test.go +++ b/office_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v4/test" + "github.com/thecodingmachine/gotenberg-go-client/v5/test" ) func TestOffice(t *testing.T) { diff --git a/url_test.go b/url_test.go index 17af188..4304a86 100644 --- a/url_test.go +++ b/url_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v4/test" + "github.com/thecodingmachine/gotenberg-go-client/v5/test" ) func TestURL(t *testing.T) {