Expose resource path

It would be useful to access the file path of a page resource. One use case would be to pass the resource path into os.Stat to check the file size, and return a resized image if the original resource is an image greater than a certain file size.

Unless I’m mistaken, the only workaround for this is to have permalinks match the structure of the content directory and pass $resource.RelPermalink. This only works if the permalink matches the relative path of the file in the content directory ( pretty hacky ).

Would anyone else find this useful?

Untested, but if you can get the .File.Path of a resource, then you can pass content/ + the file path to os.Stat

I don’t believe that .File is a field on Resource (in fact I just tested it). Is there an undocumented way to capture the file info (that includes the path) of a resource?

From the source it looks like Resource has what I need: https://github.com/gohugoio/hugo/blob/158e7ec204e5149d77893d353cac9f55946d3e9a/resources/resource.go#L276

but calling .TargetPath on a resource gives me a build error. :thinking:

{{ len $resource.Content }}

Will give you the size in bytes.

2 Likes

Much more elegant solution. Thanks!