@@ -10,45 +10,13 @@ import (
1010 "os"
1111 "path/filepath"
1212 "runtime"
13+ "strconv"
1314)
1415
1516const (
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"
5220)
5321
5422// Client facilitates interacting with
@@ -61,29 +29,38 @@ type Client struct {
6129// form values and form files to
6230// the Gotenberg API.
6331type Request interface {
64- SetWebhookURL (webhookURL string )
65- getPostURL () string
66- getFormValues () map [string ]string
67- getFormFiles () map [string ]string
32+ postURL () string
33+ formValues () map [string ]string
34+ formFiles () map [string ]string
35+ }
36+
37+ type request struct {
38+ values map [string ]string
39+ }
40+
41+ func newRequest () * request {
42+ return & request {
43+ values : make (map [string ]string ),
44+ }
45+ }
46+
47+ // ResultFilename sets resultFilename form field.
48+ func (req * request ) ResultFilename (filename string ) {
49+ req .values [resultFilename ] = filename
50+ }
51+
52+ // WaitTiemout sets waitTimeout form field.
53+ func (req * request ) WaitTimeout (timeout float64 ) {
54+ req .values [waitTimeout ] = strconv .FormatFloat (timeout , 'f' , 2 , 64 )
6855}
6956
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 )
57+ // WebhookURL sets webhookURL form field.
58+ func (req * request ) WebhookURL (url string ) {
59+ req .values [webhookURL ] = url
8060}
8161
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 )
62+ func (req * request ) formValues () map [string ]string {
63+ return req .values
8764}
8865
8966// Post sends a request to the Gotenberg API
@@ -93,8 +70,8 @@ func (c *Client) Post(req Request) (*http.Response, error) {
9370 if err != nil {
9471 return nil , err
9572 }
96- URL := fmt .Sprintf ("%s%s" , c .Hostname , req .getPostURL ())
97- resp , err := http .Post (URL , contentType , body )
73+ URL := fmt .Sprintf ("%s%s" , c .Hostname , req .postURL ())
74+ resp , err := http .Post (URL , contentType , body ) /* #nosec */
9875 if err != nil {
9976 return nil , err
10077 }
@@ -114,7 +91,7 @@ func (c *Client) Store(req Request, dest string) error {
11491}
11592
11693func hasWebhook (req Request ) bool {
117- webhookURL , ok := req .getFormValues ()[webhookURL ]
94+ webhookURL , ok := req .formValues ()[webhookURL ]
11895 if ! ok {
11996 return false
12097 }
@@ -129,7 +106,7 @@ func writeNewFile(fpath string, in io.Reader) error {
129106 if err != nil {
130107 return fmt .Errorf ("%s: creating new file: %v" , fpath , err )
131108 }
132- defer out .Close ()
109+ defer out .Close () // nolint: errcheck
133110 err = out .Chmod (0644 )
134111 if err != nil && runtime .GOOS != "windows" {
135112 return fmt .Errorf ("%s: changing file mode: %v" , fpath , err )
@@ -149,8 +126,8 @@ func fileExists(name string) bool {
149126func multipartForm (req Request ) (* bytes.Buffer , string , error ) {
150127 body := & bytes.Buffer {}
151128 writer := multipart .NewWriter (body )
152- defer writer .Close ()
153- for filename , fpath := range req .getFormFiles () {
129+ defer writer .Close () // nolint: errcheck
130+ for filename , fpath := range req .formFiles () {
154131 // https://github.com/thecodingmachine/gotenberg-go-client/issues/3
155132 if fpath == "" {
156133 continue
@@ -159,6 +136,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
159136 if err != nil {
160137 return nil , "" , fmt .Errorf ("%s: opening file: %v" , filename , err )
161138 }
139+ defer in .Close () // nolint: errcheck
162140 part , err := writer .CreateFormFile ("files" , filename )
163141 if err != nil {
164142 return nil , "" , fmt .Errorf ("%s: creating form file: %v" , filename , err )
@@ -168,7 +146,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
168146 return nil , "" , fmt .Errorf ("%s: copying file: %v" , filename , err )
169147 }
170148 }
171- for name , value := range req .getFormValues () {
149+ for name , value := range req .formValues () {
172150 if err := writer .WriteField (name , value ); err != nil {
173151 return nil , "" , fmt .Errorf ("%s: writing form field: %v" , name , err )
174152 }
0 commit comments