diff options
author | Paul van Brouwershaven <vanbroup@users.noreply.github.com> | 2021-11-30 13:49:51 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-30 13:49:51 +0300 |
commit | 8aa7257f652ebea70dfbb819c301800f5c25b567 (patch) | |
tree | 188863b9a6e35c0826ab765b499cb411747a2358 /tpl/resources/resources.go | |
parent | 75a823a36a75781c0c5d89fe9f328e3b9322d95f (diff) |
Add remote support to resources.Get
Closes #5255
Supports #9044
Diffstat (limited to 'tpl/resources/resources.go')
-rw-r--r-- | tpl/resources/resources.go | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go index 850def00e..a3f3aaa3e 100644 --- a/tpl/resources/resources.go +++ b/tpl/resources/resources.go @@ -16,6 +16,7 @@ package resources import ( "fmt" + "net/url" "path/filepath" "sync" @@ -107,14 +108,32 @@ func (ns *Namespace) getscssClientDartSass() (*dartsass.Client, error) { return ns.scssClientDartSass, err } -// Get locates the filename given in Hugo's assets filesystem -// and creates a Resource object that can be used for further transformations. -func (ns *Namespace) Get(filename interface{}) (resource.Resource, error) { - filenamestr, err := cast.ToStringE(filename) +// Get locates the filename given in Hugo's assets filesystem or downloads +// a file from an URL and creates a Resource object that can be used for +// further transformations. +// +// For URLs an additional argument with options can be provided. +func (ns *Namespace) Get(args ...interface{}) (resource.Resource, error) { + if len(args) != 1 && len(args) != 2 { + return nil, errors.New("must provide a filename or URL") + } + + filenamestr, err := cast.ToStringE(args[0]) if err != nil { return nil, err } + if u, err := url.Parse(filenamestr); err == nil && u.Scheme != "" { + if len(args) == 2 { + options, err := maps.ToStringMapE(args[1]) + if err != nil { + return nil, err + } + return ns.createClient.FromRemote(filenamestr, options) + } + return ns.createClient.FromRemote(filenamestr, nil) + } + filenamestr = filepath.Clean(filenamestr) return ns.createClient.Get(filenamestr) |