Skip to content

Commit

Permalink
Implement DOWNLOAD function (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pangoraw authored and ziflex committed Oct 17, 2018
1 parent eb4fba1 commit dd13878
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/stdlib/html/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package html
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"regexp"

"github.com/mafredri/cdp/protocol/page"
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/stdlib/html/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ func NewLib() map[string]core.Function {
"INNER_TEXT_ALL": InnerTextAll,
"SCREENSHOT": Screenshot,
"PDF": PDF,
"DOWNLOAD": Download,
}
}

0 comments on commit dd13878

Please sign in to comment.