1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-05 15:16:07 +02:00

Implement DOWNLOAD function (#132)

This commit is contained in:
Paul 2018-10-17 14:19:44 +02:00 committed by Tim Voronov
parent eb4fba135d
commit dd13878f80
2 changed files with 28 additions and 0 deletions

View File

@ -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
}

View File

@ -34,5 +34,6 @@ func NewLib() map[string]core.Function {
"INNER_TEXT_ALL": InnerTextAll,
"SCREENSHOT": Screenshot,
"PDF": PDF,
"DOWNLOAD": Download,
}
}