mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Add fill HTTP Response info into .Data in resources.GetRemote
See #10604
This commit is contained in:
parent
f13531e608
commit
6a579ebac3
5 changed files with 30 additions and 4 deletions
|
@ -50,6 +50,20 @@ With `resources.GetRemote`, the first argument is a remote URL:
|
||||||
|
|
||||||
`resources.Get` and `resources.GetRemote` return `nil` if the resource is not found.
|
`resources.Get` and `resources.GetRemote` return `nil` if the resource is not found.
|
||||||
|
|
||||||
|
{{< new-in "0.110.0" >}} You can get information about the HTTP Response using `.Data` in the returned `Resource`. This is especially useful for HEAD request without any body. The Data object contains:
|
||||||
|
|
||||||
|
StatusCode
|
||||||
|
: The HTTP status code, e.g. 200
|
||||||
|
Status
|
||||||
|
: The HTTP status text, e.g. "200 OK"
|
||||||
|
TransferEncoding
|
||||||
|
: The transfer encoding, e.g. "chunked"
|
||||||
|
ContentLength
|
||||||
|
: The content length, e.g. 1234
|
||||||
|
ContentType
|
||||||
|
: The content type, e.g. "text/html"
|
||||||
|
|
||||||
|
|
||||||
## Copy a Resource
|
## Copy a Resource
|
||||||
|
|
||||||
{{< new-in "0.100.0" >}}
|
{{< new-in "0.100.0" >}}
|
||||||
|
|
|
@ -70,6 +70,8 @@ type ResourceSourceDescriptor struct {
|
||||||
|
|
||||||
Fs afero.Fs
|
Fs afero.Fs
|
||||||
|
|
||||||
|
Data map[string]any
|
||||||
|
|
||||||
// Set when its known up front, else it's resolved from the target filename.
|
// Set when its known up front, else it's resolved from the target filename.
|
||||||
MediaType media.Type
|
MediaType media.Type
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ func TestGetResourceHead(t *testing.T) {
|
||||||
{{ with .Err }}
|
{{ with .Err }}
|
||||||
{{ errorf "Unable to get remote resource: %s" . }}
|
{{ errorf "Unable to get remote resource: %s" . }}
|
||||||
{{ else }}
|
{{ else }}
|
||||||
Head Content: {{ .Content }}.
|
Head Content: {{ .Content }}. Head Data: {{ .Data }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ else }}
|
{{ else }}
|
||||||
{{ errorf "Unable to get remote resource: %s" $url }}
|
{{ errorf "Unable to get remote resource: %s" $url }}
|
||||||
|
@ -51,6 +51,9 @@ func TestGetResourceHead(t *testing.T) {
|
||||||
|
|
||||||
b.Build()
|
b.Build()
|
||||||
|
|
||||||
b.AssertFileContent("public/index.html", "Head Content: .")
|
b.AssertFileContent("public/index.html",
|
||||||
|
"Head Content: .",
|
||||||
|
"Head Data: map[ContentLength:18210 ContentType:image/png Status:200 OK StatusCode:200 TransferEncoding:[]]",
|
||||||
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -207,10 +207,12 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceID = filename[:len(filename)-len(path.Ext(filename))] + "_" + resourceID + mediaType.FirstSuffix.FullSuffix
|
resourceID = filename[:len(filename)-len(path.Ext(filename))] + "_" + resourceID + mediaType.FirstSuffix.FullSuffix
|
||||||
|
data := responseToData(res, false)
|
||||||
|
|
||||||
return c.rs.New(
|
return c.rs.New(
|
||||||
resources.ResourceSourceDescriptor{
|
resources.ResourceSourceDescriptor{
|
||||||
MediaType: mediaType,
|
MediaType: mediaType,
|
||||||
|
Data: data,
|
||||||
LazyPublish: true,
|
LazyPublish: true,
|
||||||
OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) {
|
OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) {
|
||||||
return hugio.NewReadSeekerNoOpCloser(bytes.NewReader(body)), nil
|
return hugio.NewReadSeekerNoOpCloser(bytes.NewReader(body)), nil
|
||||||
|
|
|
@ -192,6 +192,7 @@ func (r *Spec) newGenericResource(sourceFs afero.Fs,
|
||||||
sourceFilename,
|
sourceFilename,
|
||||||
baseFilename,
|
baseFilename,
|
||||||
mediaType,
|
mediaType,
|
||||||
|
nil,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,7 +204,9 @@ func (r *Spec) newGenericResourceWithBase(
|
||||||
osFileInfo os.FileInfo,
|
osFileInfo os.FileInfo,
|
||||||
sourceFilename,
|
sourceFilename,
|
||||||
baseFilename string,
|
baseFilename string,
|
||||||
mediaType media.Type) *genericResource {
|
mediaType media.Type,
|
||||||
|
data map[string]any,
|
||||||
|
) *genericResource {
|
||||||
if osFileInfo != nil && osFileInfo.IsDir() {
|
if osFileInfo != nil && osFileInfo.IsDir() {
|
||||||
panic(fmt.Sprintf("dirs not supported resource types: %v", osFileInfo))
|
panic(fmt.Sprintf("dirs not supported resource types: %v", osFileInfo))
|
||||||
}
|
}
|
||||||
|
@ -244,6 +247,7 @@ func (r *Spec) newGenericResourceWithBase(
|
||||||
name: baseFilename,
|
name: baseFilename,
|
||||||
title: baseFilename,
|
title: baseFilename,
|
||||||
resourceContent: &resourceContent{},
|
resourceContent: &resourceContent{},
|
||||||
|
data: data,
|
||||||
}
|
}
|
||||||
|
|
||||||
return g
|
return g
|
||||||
|
@ -305,7 +309,8 @@ func (r *Spec) newResource(sourceFs afero.Fs, fd ResourceSourceDescriptor) (reso
|
||||||
fi,
|
fi,
|
||||||
sourceFilename,
|
sourceFilename,
|
||||||
fd.RelTargetFilename,
|
fd.RelTargetFilename,
|
||||||
mimeType)
|
mimeType,
|
||||||
|
fd.Data)
|
||||||
|
|
||||||
if mimeType.MainType == "image" {
|
if mimeType.MainType == "image" {
|
||||||
imgFormat, ok := images.ImageFormatFromMediaSubType(mimeType.SubType)
|
imgFormat, ok := images.ImageFormatFromMediaSubType(mimeType.SubType)
|
||||||
|
|
Loading…
Reference in a new issue