Remove original resource (image) after copying?

Sounds like Suggestion: Ability to rename page resources intended for download - #4

Unfortunately, no solution to the actual question was given there (IMO).

My use case:

  • I have a blog with travel reports and photos
  • For the sake of simplicity, I put original photos (hi-res, 2-5 Mb each) directly next to my post content. Similarly to the topicstarter of the linked question, the filenames in my filesystem are for for myself only. Example:
|posts
-|2022-my-mega-post
--|index.md
--|2022-01-01 21.22.35.jpg
--|2022-01-02 11.22.35.jpg
  • In my post I had my img shortcode which is capable of renaming the images to something more human-readable/seo-friendly/whatever, like {{< img "2024-08-15 16.21.04.jpg" "vennbahn-map" "Vennbahn Map" >}} where the second param is the intented filename (vennbahn-map.jpg) and the third is caption

And now goes the problem - basically when I do Resource Copy and copy the image to the new name… It obviously does copy, so it leaves me with basically 2x copies of the files in the resulted public Hugo folder.

Is there a way to remove the original resource from public after copying?

Use build options. You can do this by page, by section, or by site. For example, to not publish page resources for the entire site, do this in your site configuration:

[cascade.build]
publishResources = false

With this setting, you must explicitly call a page resource’s Permalink, RelPermalink, or Publish method to publish the resource.

I assume your shortcode looks something like this:

{{ with .Page.Resources.Get (.Get 0) }}
  {{ with . | resources.Copy (urls.JoinPath $.Page.RelPermalink ($.Get 1)) }}
    {{ .Publish }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

The .Publish call above is a workaround for #10584, applicable if you’re publishing the original page resource (which you are not, but I’d leave it in there anyway).

1 Like