mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Add resources.GetRemote
In Hugo 0.89 we added remote support to `resources.Get`. In hindsight that was not a great idea, as a poll from many Hugo users showed. See Issue #9285 for more details. After this commit `resources.Get` only supports local resource lookups. If you want to support both, you need to use a construct similar to: Also improve some option case handling. ``` {{ resource := "" }} {{ if (urls.Parse $url).IsAbs }} {{ $resource = resources.GetRemote $url }} {{ else }} {{ $resource = resources.Get $url }} {{ end }} ``` Fixes #9285 Fixes #9296
This commit is contained in:
@@ -16,15 +16,7 @@
|
||||
package create
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -36,13 +28,8 @@ import (
|
||||
|
||||
"github.com/gohugoio/hugo/cache/filecache"
|
||||
"github.com/gohugoio/hugo/common/hugio"
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
"github.com/gohugoio/hugo/common/types"
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
"github.com/gohugoio/hugo/resources"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Client contains methods to create Resource objects.
|
||||
@@ -150,203 +137,3 @@ func (c *Client) FromString(targetPath, content string) (resource.Resource, erro
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// FromRemote expects one or n-parts of a URL to a resource
|
||||
// If you provide multiple parts they will be joined together to the final URL.
|
||||
func (c *Client) FromRemote(uri string, options map[string]interface{}) (resource.Resource, error) {
|
||||
if err := c.validateFromRemoteArgs(uri, options); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rURL, err := url.Parse(uri)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to parse URL for resource %s", uri)
|
||||
}
|
||||
|
||||
resourceID := helpers.HashString(uri, options)
|
||||
|
||||
_, httpResponse, err := c.cacheGetResource.GetOrCreate(resourceID, func() (io.ReadCloser, error) {
|
||||
method, reqBody, err := getMethodAndBody(options)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to get method or body for resource %s", uri)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, uri, reqBody)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to create request for resource %s", uri)
|
||||
}
|
||||
addDefaultHeaders(req)
|
||||
|
||||
if _, ok := options["headers"]; ok {
|
||||
headers, err := maps.ToStringMapE(options["headers"])
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to parse request headers for resource %s", uri)
|
||||
}
|
||||
addUserProvidedHeaders(headers, req)
|
||||
}
|
||||
|
||||
res, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusNotFound {
|
||||
if res.StatusCode < 200 || res.StatusCode > 299 {
|
||||
return nil, errors.Errorf("failed to retrieve remote resource: %s", http.StatusText(res.StatusCode))
|
||||
}
|
||||
}
|
||||
|
||||
httpResponse, err := httputil.DumpResponse(res, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return hugio.ToReadCloser(bytes.NewReader(httpResponse)), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer httpResponse.Close()
|
||||
|
||||
res, err := http.ReadResponse(bufio.NewReader(httpResponse), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res.StatusCode == http.StatusNotFound {
|
||||
// Not found. This matches how looksup for local resources work.
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to read remote resource %s", uri)
|
||||
}
|
||||
|
||||
filename := path.Base(rURL.Path)
|
||||
if _, params, _ := mime.ParseMediaType(res.Header.Get("Content-Disposition")); params != nil {
|
||||
if _, ok := params["filename"]; ok {
|
||||
filename = params["filename"]
|
||||
}
|
||||
}
|
||||
|
||||
var extension string
|
||||
if arr, _ := mime.ExtensionsByType(res.Header.Get("Content-Type")); len(arr) == 1 {
|
||||
extension = arr[0]
|
||||
}
|
||||
|
||||
// If extension was not determined by header, look for a file extention
|
||||
if extension == "" {
|
||||
if ext := path.Ext(filename); ext != "" {
|
||||
extension = ext
|
||||
}
|
||||
}
|
||||
|
||||
// If extension was not determined by header or file extention, try using content itself
|
||||
if extension == "" {
|
||||
if ct := http.DetectContentType(body); ct != "application/octet-stream" {
|
||||
if ct == "image/jpeg" {
|
||||
extension = ".jpg"
|
||||
} else if arr, _ := mime.ExtensionsByType(ct); arr != nil {
|
||||
extension = arr[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resourceID = filename[:len(filename)-len(path.Ext(filename))] + "_" + resourceID + extension
|
||||
|
||||
return c.rs.New(
|
||||
resources.ResourceSourceDescriptor{
|
||||
LazyPublish: true,
|
||||
OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) {
|
||||
return hugio.NewReadSeekerNoOpCloser(bytes.NewReader(body)), nil
|
||||
},
|
||||
RelTargetFilename: filepath.Clean(resourceID),
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (c *Client) validateFromRemoteArgs(uri string, options map[string]interface{}) error {
|
||||
if err := c.rs.ExecHelper.Sec().CheckAllowedHTTPURL(uri); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if method, ok := options["method"].(string); ok {
|
||||
if err := c.rs.ExecHelper.Sec().CheckAllowedHTTPMethod(method); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func addDefaultHeaders(req *http.Request, accepts ...string) {
|
||||
for _, accept := range accepts {
|
||||
if !hasHeaderValue(req.Header, "Accept", accept) {
|
||||
req.Header.Add("Accept", accept)
|
||||
}
|
||||
}
|
||||
if !hasHeaderKey(req.Header, "User-Agent") {
|
||||
req.Header.Add("User-Agent", "Hugo Static Site Generator")
|
||||
}
|
||||
}
|
||||
|
||||
func addUserProvidedHeaders(headers map[string]interface{}, req *http.Request) {
|
||||
if headers == nil {
|
||||
return
|
||||
}
|
||||
for key, val := range headers {
|
||||
vals := types.ToStringSlicePreserveString(val)
|
||||
for _, s := range vals {
|
||||
req.Header.Add(key, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func hasHeaderValue(m http.Header, key, value string) bool {
|
||||
var s []string
|
||||
var ok bool
|
||||
|
||||
if s, ok = m[key]; !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, v := range s {
|
||||
if v == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func hasHeaderKey(m http.Header, key string) bool {
|
||||
_, ok := m[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
func getMethodAndBody(options map[string]interface{}) (string, io.Reader, error) {
|
||||
if options == nil {
|
||||
return "GET", nil, nil
|
||||
}
|
||||
|
||||
if method, ok := options["method"].(string); ok {
|
||||
method = strings.ToUpper(method)
|
||||
switch method {
|
||||
case "GET", "DELETE", "HEAD", "OPTIONS":
|
||||
return method, nil, nil
|
||||
case "POST", "PUT", "PATCH":
|
||||
var body []byte
|
||||
if _, ok := options["body"]; ok {
|
||||
switch b := options["body"].(type) {
|
||||
case string:
|
||||
body = []byte(b)
|
||||
case []byte:
|
||||
body = b
|
||||
}
|
||||
}
|
||||
return method, bytes.NewBuffer(body), nil
|
||||
}
|
||||
|
||||
return "", nil, fmt.Errorf("invalid HTTP method %q", method)
|
||||
}
|
||||
|
||||
return "GET", nil, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user