aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortdro <tdro@noreply.example.com>2025-01-11 00:00:00 +0000
committertdro <tdro@noreply.example.com>2025-01-11 00:00:00 +0000
commita479b91703a02e160f7935d172e53334f14e758a (patch)
tree8fdfa862a863b2e508e079963e6495401b6ca44d
parent8afabf38116e255d4b7ab9ef35ac48baf9f54efd (diff)
downloadcanory-a479b91703a02e160f7935d172e53334f14e758a.tar.gz
canory-a479b91703a02e160f7935d172e53334f14e758a.tar.bz2
canory-a479b91703a02e160f7935d172e53334f14e758a.zip
themes/default/layouts/shortcodes/attach: Simplify logic more
-rw-r--r--themes/default/layouts/shortcodes/attach.html56
1 files changed, 35 insertions, 21 deletions
diff --git a/themes/default/layouts/shortcodes/attach.html b/themes/default/layouts/shortcodes/attach.html
index 451f8dd..a3506b7 100644
--- a/themes/default/layouts/shortcodes/attach.html
+++ b/themes/default/layouts/shortcodes/attach.html
@@ -5,63 +5,77 @@
{{- $data := "" -}}
{{- $cache := "" -}}
+{{- $canonical := "" -}}
{{- $author := .Page.Section -}}
{{- $extension := path.Ext $url -}}
{{- $immutable := print $url | anchorize -}}
-{{- $localFile := path.Join "public/" $url -}}
+{{- $public := path.Join "public/" $url -}}
{{- $type := "application/octet-stream" -}}
{{- $storage := print $author "/media/" $immutable $extension -}}
{{- $cached := fileExists (path.Join "public/" $storage) -}}
+{{- $target := urls.Parse $url -}}
+{{- $self := urls.Parse .Page.Site.BaseURL -}}
+{{- $prurl := eq (findRE `^/\w` $url | len) 0 -}}
+{{- $local := or (eq $target.Host $self.Host) (and (strings.HasPrefix $url "/") (not $prurl)) -}}
{{- if not $cached -}}
- {{- $remote := false -}}
+ {{- $fetch := false -}}
{{- if and (isset site.Params.site "offline") (eq $.Page.Site.Params.site.offline false) -}}
- {{- $remote = resources.GetRemote $url -}}
+ {{- $fetch = resources.GetRemote $url -}}
{{- end -}}
- {{- with $remote -}}
+ {{- with $fetch -}}
{{- with .Err -}}
- {{- if not (fileExists $localFile) -}}
- {{- $caption = "No local attachment data found for source" -}}
- {{- warnf "Attachment local fetch error: %s" $url -}}
+ {{- if not $local -}}
+ {{- $caption = "Unable to load remote source data" -}}
+ {{- warnf "Attachment remote fetch error: %s" $url -}}
{{- end -}}
{{- else -}}
- {{- $type = $remote.MediaType -}}
- {{- $cache = $remote | resources.Copy $storage -}}
+ {{- $type = $fetch.MediaType -}}
+ {{- $cache = $fetch | resources.Copy $storage -}}
{{- $cache = $cache.Permalink -}}
{{- end -}}
{{- else -}}
- {{- $caption = "Unable to load remote source data" -}}
- {{- warnf "Attachment remote fetch error: %s" $url -}}
+ {{- if not $local -}}
+ {{- $caption = "Offline unable to load remote source data" -}}
+ {{- warnf "Offline attachment remote fetch error: %s" $url -}}
+ {{- end -}}
{{- end -}}
+
{{- else -}}
+
{{- with $cache = resources.Get (path.Join "public/" $storage) -}}
{{- $type = $cache.MediaType -}}
{{- $cache = (strings.TrimPrefix "public/" $cache) -}}
{{- end -}}
-{{- end -}}
-
-{{- $data = strings.TrimPrefix "/" $url | relURL -}}
-{{- if $cache -}}
- {{- $data = $cache | relURL -}}
{{- end -}}
-{{- if $remote -}}
- {{- $data = $url -}}
+{{- if and $local (not (fileExists $public)) -}}
+ {{- $caption = "No local attachment data found for source" -}}
+ {{- warnf "Attachment local fetch error: %s" $url -}}
{{- end -}}
-{{- if (in $data "pdf") -}}
- {{- $type = "application/pdf" -}}
+{{- if $local -}}
+ {{- $data = strings.TrimPrefix "/" $url | relURL -}}
+ {{- $canonical = $data -}}
{{- end -}}
+{{- if not $local -}} {{- $canonical = $url -}} {{- end -}}
+
+{{- if $cache -}} {{- $data = $cache | relURL -}} {{- end -}}
+
+{{- if $remote -}} {{- $data = $url -}} {{- end -}}
+
+{{- if (in $data "pdf") -}} {{- $type = "application/pdf" -}} {{- end -}}
+
<document-attachment title="{{ $title }}">
<object type="{{ $type }}" data="{{ $data }}">
<p>
Your browser does not support the <code>{{ $type }}</code> plugin. Here's a
- <a rel="canonical" href="{{ $data }}">link to the file</a> instead.
+ <a rel="canonical" href="{{ $canonical }}">link to the file</a> instead.
</p>
</object>
<footer>