gbhat
April 27, 2025, 7:22am
1
When I fetch a remote resource (Example: resources.GetRemote "https://example.com/my-song.mp3"
) the generated .RelPermalink
does not have an extension. This causes the rather dumb default rewrite rule in AWS Amplify to assume that the link points to a folder (adding a mime type does not help) and generates a 404 when an index.html
is not found in this imaginary folder. I could modify the rewrite rules of course, but with lots of remote mp3s this gets complicated.
While, I solved this issue with a resources.Copy
to a new file with the correct extension, I wonder if it might be possible for Hugo to attach an extension to the fetched resource, either from the url.Path
or configured via opts
to GetRemote
To be sure, this is not an issue with Hugo but an opportunity to accommodate dumb hosting solutions in a way that is better than my silly hack.
Thank you.
irkode
April 27, 2025, 8:29am
2
in general Hugo does not remove file extensions from remote resources. so my guess is that the root cause is within fetching the mp3
maybe there’s something special in
server setup of the one where you fetch the resource from
your config fe allowlist
…
can you share a link to one source mp3?
maybe use wget to inspect the responses.
GetRemote also has Data.Headers field to inspect since 0.143. see resources.GetRemote
gbhat
April 27, 2025, 8:35am
3
Thanks - Sure, here you go: Link to my Audio
My shortcode (with the Copy)
{{- $caption := .Get "caption" }}
{{- with $path := trim (.Get "path") "<>" }}
{{- $r := "" }}
{{ $url := urls.Parse $path }}
{{- if $url.IsAbs }}
{{ $fName := printf "audio%s" $url.Path }}
{{- with try (resources.GetRemote $path) }}
{{- with .Err }}
{{- errorf "The %q shortcode was unable to get %s: %s: see %s" $.Name $path . $.Position }}
{{- else with .Value }}
{{- $r = . | resources.Copy $fName }}
{{- else }}
{{- errorf "The %q shortcode was unable to get %s: see %s" $.Name $path $.Position }}
{{- end }}
{{- end }}
{{- else }}
{{- with or ($.Page.Resources.Get $path) (resources.Get $path) }}
{{- $r = . }}
{{- else }}
{{- errorf "The %q shortcode was unable to get %s: see %s" $.Name $path $.Position }}
{{- end }}
{{- end }}
{{- with $r }}
<figure style="width: 100%">
{{- with $caption }}
<figcaption>{{ . }}</figcaption>
{{- end }}
{{- $id := printf "audio-%s" ($path | md5) }}
<audio controls id="{{ $id }}" preload="auto">
<source
src="{{ .RelPermalink -}}"
type="{{ .MediaType.Type }}" />
</audio>
</figure>
{{- end }}
{{- else }}
{{- errorf "The %q shortcode requires a path argument: see %s" .Name .Position }}
{{- end }}
When I remove the Copy hack, the fetched resource has no extension
irkode
April 27, 2025, 10:09am
4
offline, so can’t directly play with getRemote, but.
on mobile android
clicking the link will open a player
download will save an mp3 file with extension
so I suppose you could add an accept header field in the options to GetRemote specifying the mp3 content type. maybe user-agent…
or the.Data trick for wrong content types desribed in the docs
gbhat
April 27, 2025, 10:28am
5
wget
reports the correct MIME type (audio/mpeg
). So let me read the docs on the Data headers. Thanks for helping with this.
When retrieving an mp3 file, in its default configuration Hugo throws this error:
error calling GetRemote: failed to resolve media type for remote resource
The error is thrown because audio/mpeg
is not one of the default (built-in) media types:
https://gohugo.io/configuration/media-types/#default-configuration
There are two ways to address this:
1) Bypass the resources.GetRemote
security check for this media type:
[security.http]
mediaTypes = ['^audio/mpeg$']
The resulting resource with have a permalink without an extension. For example:
https://example.org/Jewel_Thief_Bhawana_Somaaya_5193727227576830780
2) Create a new media type as well:
[security.http]
mediaTypes = ['^audio/mpeg$']
[mediaTypes.'audio/mpeg']
suffixes = ['mp3']
The resulting resource with have a permalink with an extension. For example:
https://example.org/Jewel_Thief_Bhawana_Somaaya_5193727227576830780.mp3
2 Likes
gbhat
April 27, 2025, 2:30pm
7
Thanks a ton!
I had already set security.http
to the correct MIME type (media types) but now adding a new media type with the right suffix works like a charm. I also now see that creating new media types is documented .
Thank you once again
1 Like
system
Closed
April 29, 2025, 2:31pm
8
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.