From dd13878f80f340c4727d3ad5a6a70859dd958b92 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 17 Oct 2018 14:19:44 +0200 Subject: [PATCH] Implement DOWNLOAD function (#132) --- pkg/stdlib/html/blob.go | 27 +++++++++++++++++++++++++++ pkg/stdlib/html/lib.go | 1 + 2 files changed, 28 insertions(+) diff --git a/pkg/stdlib/html/blob.go b/pkg/stdlib/html/blob.go index d4c9062c..d54c8be4 100644 --- a/pkg/stdlib/html/blob.go +++ b/pkg/stdlib/html/blob.go @@ -3,6 +3,8 @@ package html import ( "context" "fmt" + "io/ioutil" + "net/http" "regexp" "github.com/mafredri/cdp/protocol/page" @@ -373,3 +375,28 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) { return pdf, nil } + +// Download a ressource from the given URL. +// @param URL (String) - URL to download. +// @returns data (Binary) - Returns a base64 encoded string in binary format. +func Download(_ context.Context, args ...core.Value) (core.Value, error) { + err := core.ValidateArgs(args, 1, 1) + if err != nil { + return values.None, err + } + + arg1 := args[0] + err = core.ValidateType(arg1, core.StringType) + if err != nil { + return values.None, err + } + resp, err := http.Get(arg1.String()) + if err != nil { + return values.None, err + } + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return values.None, err + } + return values.NewBinary(data), nil +} diff --git a/pkg/stdlib/html/lib.go b/pkg/stdlib/html/lib.go index 15831920..dcb62eb7 100644 --- a/pkg/stdlib/html/lib.go +++ b/pkg/stdlib/html/lib.go @@ -34,5 +34,6 @@ func NewLib() map[string]core.Function { "INNER_TEXT_ALL": InnerTextAll, "SCREENSHOT": Screenshot, "PDF": PDF, + "DOWNLOAD": Download, } }