From cd3fcf7bb2c77ab627e0bb28eb2348c3401b600b Mon Sep 17 00:00:00 2001 From: tdro Date: Mon, 27 Jun 2022 22:24:02 -0400 Subject: themes/default/layouts/_default/_markup/render-image: Try not to fetch again Messy but let's try to cache remote images and not fetch again unless necessary. Use webp to check for existence. TODO: will have clean this up later. --- .../layouts/_default/_markup/render-image.html | 67 ++++++++++++++-------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/themes/default/layouts/_default/_markup/render-image.html b/themes/default/layouts/_default/_markup/render-image.html index b588842..b3328e0 100644 --- a/themes/default/layouts/_default/_markup/render-image.html +++ b/themes/default/layouts/_default/_markup/render-image.html @@ -1,32 +1,40 @@ {{- $source := $.Destination | safeURL -}} {{- $public := print "public/" $source -}} -{{- $remote := resources.GetRemote $source -}} {{- $local := "" -}} +{{- $cache := "" -}} +{{- $exists := "" -}} {{- $width := "" -}} {{- $height := "" -}} -{{- $cache := "" -}} {{- $dimensions := "600x360" -}} -{{- $immutable := print (.Page.RelPermalink | humanize | urlize) "-" ($source | sha256 | truncate 8 "") -}} +{{- $author := .Page.Section -}} +{{- $relURL := strings.TrimPrefix .Page.Site.BaseURL .Page.Permalink -}} +{{- $immutable := print ($relURL | humanize | urlize) "-" ($source | sha256 | truncate 8 "") -}} +{{- $fileCache := print (partial "function-paths-media.html") "/" $author "/" $immutable -}} -{{- with $remote -}} - {{- with .Err -}} - {{- if fileExists $public -}} - {{- $local = resources.Get $public -}} - {{- with $local -}} - {{- $local = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $.Page.Section "/" (path.Base .)) -}} +{{- if not (fileExists (print "public/" $fileCache (path.Ext $source))) -}} + {{- with $remote := resources.GetRemote $source -}} + {{- with .Err -}} + {{- if fileExists $public -}} + {{- $local = resources.Get $public -}} + {{- with $local -}} + {{- $local = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base .)) -}} + {{- end -}} + {{- $source = print ("" | absURL) $source -}} + {{- else -}} + {{- $404image := resources.Get "data/media/404.png" -}} + {{- with $404image -}} + {{- $width = .Width -}} + {{- $height = .Height -}} + {{- end -}} + {{- $source = $404image.RelPermalink -}} {{- end -}} - {{ $source = print ("" | absURL) $source }} {{- else -}} - {{- $404image := resources.Get "data/media/404.png" -}} - {{- with $404image -}} - {{- $width = .Width -}} - {{- $height = .Height -}} - {{- end -}} - {{ $source = $404image.RelPermalink }} + {{- $cache = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base .)) -}} {{- end -}} - {{- else -}} - {{- $cache = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $.Page.Section "/" (path.Base .)) -}} - {{ end }} + {{- end -}} +{{- else -}} + {{- $exists = resources.Get (print "public/" $fileCache (path.Ext $source)) -}} + {{- $exists = $exists.Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base $exists)) -}} {{ end }} {{- /* This comment removes trailing newlines and white spaces. */ -}}
@@ -34,24 +42,35 @@ {{- with $local }} {{- $local = .Fit (print $dimensions " webp") }} - {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $.Page.Section "/" $immutable ".webp") }} + {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable ".webp") }} {{- $local = .Fit $dimensions }} - {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $.Page.Section "/" $immutable (path.Ext $local)) }} + {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable (path.Ext $local)) }} {{- $width = $local.Width -}} {{- $height = $local.Height -}} {{- $writeToFile := $local.Permalink }} {{- end }} {{- with $cache }} {{- $cache = .Fit (print $dimensions " webp") }} - {{- $cache = $cache | resources.Copy (print $.Page.Section "/media/" $immutable ".webp") }} + {{- $cache = $cache | resources.Copy (print $author "/media/" $immutable ".webp") }} {{- $cache = .Fit $dimensions }} - {{- $cache = $cache | resources.Copy (print (partial "function-paths-media.html") "/" $.Page.Section "/" $immutable (path.Ext $cache)) }} + {{- $cache = $cache | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable (path.Ext $cache)) }} {{- $width = $cache.Width -}} {{- $height = $cache.Height -}} {{- $cache = $cache.RelPermalink }} - {{- end }} + {{- end -}} + {{- with $exists }} + {{- if fileExists (print "public/" $fileCache ".webp") -}} + + {{- end -}} + {{- if fileExists (print "public/" $author "/media/" $immutable ".webp") -}} + + {{- end -}} + {{- $width = .Width -}} + {{- $height = .Height -}} + {{- $cache = .RelPermalink }} + {{- end -}}