aboutsummaryrefslogtreecommitdiff
path: root/themes/default/layouts/shortcodes/attach.html
blob: 55b5744236f6bd5939cd7045a9a24852ab429e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{{- $url     := default "/canory/media/2city12p.pdf"                          (.Get "url"     | default (.Get 0)) -}}
{{- $title   := default "Attachment"                                          (.Get "title"   | default (.Get 1)) -}}
{{- $caption := default $title                                                (.Get "caption" | default (.Get 2)) -}}
{{- $remote  := default ""                                                    (.Get "remote"  | default (.Get 3)) -}}

{{- $data       := "" -}}
{{- $cache      := "" -}}
{{- $author     := .Page.Section -}}
{{- $extension  := path.Ext $url -}}
{{- $immutable  := print $url | anchorize -}}
{{- $localFile  := path.Join "public/" $url -}}
{{- $type       := "application/octet-stream" -}}
{{- $storage    := print $author "/media/" $immutable $extension -}}
{{- $cached     := fileExists (path.Join "public/" $storage) -}}

{{- if not $cached -}}
  {{- with $remote := cond (default true $.Page.Site.Params.site.offline) false (resources.GetRemote $url) -}}
    {{- with .Err -}}
      {{- if not (fileExists $localFile) -}}
        {{- $caption = "No local attachment data found for source" -}}
        {{- warnf "Attachment local fetch error: %s" $url -}}
      {{- end -}}
    {{- else -}}
      {{- $type = $remote.MediaType -}}
      {{- $cache = $remote | resources.Copy $storage -}}
      {{- $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/" $storage) -}}
    {{- $type = .MediaType -}}
    {{- $cache = print ("" | absURL) (strings.TrimLeft "public" $cache) -}}
  {{- end -}}
{{- end -}}

{{- $data := or $cache $url -}}

{{- if $remote -}}
  {{- $data = $url -}}
{{- 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.
    </p>
  </object>
  <footer>
    {{ $caption | markdownify }}
    <br />
    Index: {{ with $cache }} {{ print "[Cache](" $cache ")" | markdownify }} &middot; {{ end }}
    {{ print "[Source](" $url ")" | markdownify }}
  </footer>
</document-attachment>