Skip to content

Commit

Permalink
feat: add downloadFrom method
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Pikeev committed Oct 26, 2024
1 parent aac8963 commit bd75049
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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/).**
28 changes: 28 additions & 0 deletions baserequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gotenberg

import (
"encoding/base64"
"encoding/json"
"net/http"

"github.com/dcaraxes/gotenberg-go-client/document"
Expand Down Expand Up @@ -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"`
}
1 change: 1 addition & 0 deletions chromium.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ type formField string

// Common property.
const (
fieldMetadata formField = "metadata"
fieldMetadata formField = "metadata"
fieldDownloadFrom formField = "downloadFrom"
)

// URL request property.
Expand Down Expand Up @@ -85,4 +86,4 @@ const (
const (
fieldMergePdfA formField = "pdfa"
fieldMergePdfUA formField = "pdfua"
)
)
2 changes: 1 addition & 1 deletion test/testfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ func IsPDFUA(filePath string) (bool, error) {
}

return false, nil
}
}

0 comments on commit bd75049

Please sign in to comment.