github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/download.go (about) 1 package html 2 3 import ( 4 "context" 5 "io" 6 "net/http" 7 8 "github.com/MontFerret/ferret/pkg/runtime/core" 9 "github.com/MontFerret/ferret/pkg/runtime/values" 10 "github.com/MontFerret/ferret/pkg/runtime/values/types" 11 ) 12 13 // DOWNLOAD downloads a resource from the given GetURL. 14 // @param {String} url - URL to download. 15 // @return {Binary} - A base64 encoded string in binary format. 16 func Download(_ context.Context, args ...core.Value) (core.Value, error) { 17 err := core.ValidateArgs(args, 1, 1) 18 19 if err != nil { 20 return values.None, err 21 } 22 23 arg1 := args[0] 24 err = core.ValidateType(arg1, types.String) 25 26 if err != nil { 27 return values.None, err 28 } 29 30 resp, err := http.Get(arg1.String()) 31 32 if err != nil { 33 return values.None, err 34 } 35 36 defer resp.Body.Close() 37 38 data, err := io.ReadAll(resp.Body) 39 40 if err != nil { 41 return values.None, err 42 } 43 44 return values.NewBinary(data), nil 45 }