From 8c0e8f92c174534b2b81e9c427d8e19b407e982e Mon Sep 17 00:00:00 2001 From: Alexander Pikeev Date: Sat, 26 Oct 2024 20:06:58 +0300 Subject: [PATCH] feat: add downloadFrom method (#18) Co-authored-by: Alexander Pikeev --- README.md | 11 ++++++++--- baserequest.go | 28 ++++++++++++++++++++++++++++ chromium.go | 1 + fields.go | 5 +++-- test/testfunc.go | 2 +- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 23d1947..98ec3d3 100644 --- a/README.md +++ b/README.md @@ -74,12 +74,17 @@ func main() { // Creates the Gotenberg documents from a files paths. index, err := document.FromPath("index.html", "/path/to/file") - style, err := document.FromPath("style.css", "/path/to/file") - img, err := document.FromPath("img.png", "/path/to/file") // Create the HTML request. req := gotenberg.NewHTMLRequest(index) + // Loading style and image from the specified urls. + downloads := make(map[string]map[string]string) + downloads["http://my.style.css"] = nil + downloads["http://my.img.gif"] = map[string]string{"X-Header": "Foo"} + + req.DownloadFrom(downloads) + // Setting up basic auth (if needed). req.UseBasicAuth("username", "password") @@ -210,4 +215,4 @@ func main() { --- -**For more complete usages, head to the [documentation](https://gotenberg.dev/).** +**For more complete usages, head to the [documentation](https://gotenberg.dev/).** \ No newline at end of file diff --git a/baserequest.go b/baserequest.go index a98ca04..5eb5b21 100644 --- a/baserequest.go +++ b/baserequest.go @@ -2,6 +2,7 @@ package gotenberg import ( "encoding/base64" + "encoding/json" "net/http" "github.com/dcaraxes/gotenberg-go-client/document" @@ -84,4 +85,31 @@ func ensureWebhookMethod(method string) string { } else { return http.MethodGet } +} + +// DownloadFrom sets the URLs to download files from. +// This method accepts a JSON string e.g., [{"url":"http://localhost:80/","extraHttpHeaders":{"X-Foo":"Bar"}}]. For Go, +// this is equivalent to map[string]map[string]string, which this method accepts. +// URLs MUST return a Content-Disposition header with a filename parameter. +func (br *baseRequest) DownloadFrom(downloads map[string]map[string]string) { + dfs := make([]downloadFrom, 0, len(downloads)) + + for url, headers := range downloads { + dfs = append(dfs, downloadFrom{ + URL: url, + ExtraHTTPHeaders: headers, + }) + } + + marshaled, err := json.Marshal(dfs) + if err != nil { + return + } + + br.fields[fieldDownloadFrom] = string(marshaled) +} + +type downloadFrom struct { + URL string `json:"url"` + ExtraHTTPHeaders map[string]string `json:"extraHttpHeaders"` } \ No newline at end of file diff --git a/chromium.go b/chromium.go index 2c3725e..bbecd8b 100644 --- a/chromium.go +++ b/chromium.go @@ -69,6 +69,7 @@ func (req *chromiumRequest) FailOnConsoleExceptions() { } // SkipNetworkIdleEvent specifies whether Chromium have to wait or not for its network to be idle. +// Enabled by default in Gotenberg >= 8.11.0. func (req *chromiumRequest) SkipNetworkIdleEvent() { req.fields[fieldChromiumSkipNetworkIdleEvent] = strconv.FormatBool(true) } diff --git a/fields.go b/fields.go index f4eaf42..1fe5f12 100644 --- a/fields.go +++ b/fields.go @@ -4,7 +4,8 @@ type formField string // Common property. const ( - fieldMetadata formField = "metadata" + fieldMetadata formField = "metadata" + fieldDownloadFrom formField = "downloadFrom" ) // URL request property. @@ -85,4 +86,4 @@ const ( const ( fieldMergePdfA formField = "pdfa" fieldMergePdfUA formField = "pdfua" -) +) \ No newline at end of file diff --git a/test/testfunc.go b/test/testfunc.go index ca1f3dc..c82ccfc 100644 --- a/test/testfunc.go +++ b/test/testfunc.go @@ -127,4 +127,4 @@ func IsPDFUA(filePath string) (bool, error) { } return false, nil -} +} \ No newline at end of file