From 6069820a482201d390b8a9a1f1828a47ae724037 Mon Sep 17 00:00:00 2001 From: tdro Date: Wed, 27 Sep 2023 19:20:15 -0400 Subject: themes/default/layouts/shortcodes/attach: Clear up logic a bit Avoid writing file again after being saved --- themes/default/layouts/shortcodes/attach.html | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'themes/default/layouts/shortcodes') diff --git a/themes/default/layouts/shortcodes/attach.html b/themes/default/layouts/shortcodes/attach.html index baddb92..dfd472f 100644 --- a/themes/default/layouts/shortcodes/attach.html +++ b/themes/default/layouts/shortcodes/attach.html @@ -5,33 +5,34 @@ {{- $data := "" -}} {{- $cache := "" -}} -{{- $type := "application/octet-stream" -}} {{- $author := .Page.Section -}} -{{- $immutable := print $url | anchorize -}} {{- $extension := path.Ext $url -}} +{{- $immutable := print $url | anchorize -}} +{{- $localFile := path.Join "public/" $url -}} +{{- $type := "application/octet-stream" -}} {{- $fileCache := print $author "/media/" $immutable $extension -}} -{{- $notCached := not (fileExists (path.Join "public/" $fileCache)) -}} +{{- $cached := fileExists (path.Join "public/" $fileCache) -}} -{{- if $notCached -}} +{{- if not $cached -}} {{- with $remote := cond (default true $.Page.Site.Params.site.offline) false (resources.GetRemote $url) -}} {{- with .Err -}} - {{- warnf "Attachment fetch %s" . -}} - {{- if not (fileExists $fileCache) -}} + {{- if not (fileExists $localFile) -}} {{- $caption = "No local attachment data found for source" -}} + {{- warnf "Attachment local fetch error: %s" $url -}} {{- end -}} {{- else -}} - {{- $type = .MediaType -}} - {{- $cache = . | resources.Copy $fileCache -}} + {{- $type = $remote.MediaType -}} + {{- $cache = $remote | resources.Copy $fileCache -}} {{- $cache = $cache.Permalink -}} {{- end -}} {{- else -}} {{- $caption = "Unable to load remote source data" -}} + {{- warnf "Attachment remote fetch error: %s" $url -}} {{- end -}} {{- else -}} {{- with $cache = resources.Get (path.Join "public/" $fileCache) -}} {{- $type = .MediaType -}} - {{- $cache = $cache.Content | resources.FromString $fileCache -}} - {{- $cache = $cache.Permalink -}} + {{- $cache = print ("" | absURL) (strings.TrimLeft "public" $cache) -}} {{- end -}} {{- end -}} -- cgit v1.2.3