]> git.plutz.net Git - shellwiki/commitdiff
macro for listing attachments
authorPaul Hänsch <paul@plutz.net>
Fri, 18 Mar 2022 11:52:56 +0000 (12:52 +0100)
committerPaul Hänsch <paul@plutz.net>
Fri, 18 Mar 2022 11:52:56 +0000 (12:52 +0100)
macros/attachments [new file with mode: 0755]

diff --git a/macros/attachments b/macros/attachments
new file mode 100755 (executable)
index 0000000..7acc806
--- /dev/null
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+. "$_EXEC/cgilite/cgilite.sh"
+
+page="$1"
+
+if [ "${page#/}" = "$page" ]; then
+  page="$(PATH "${PATH_INFO}/$page")"
+fi
+
+printf %s\\n '<ul class="attachment list">'
+
+for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
+  [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
+        -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
+  stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
+  size="${stat% *}" date="${stat#* }"
+
+  if [ $size -gt $((1024 * 1024 * 1024)) ]; then
+    size=$((size / 1024 / 1024 / 1024 * 10 + size / 1024 / 1024 % 1024 / 100))
+    size="$((size / 10)).$((size % 10)) GB"
+
+  elif [ $size -gt $((1024 * 1024)) ]; then
+    size=$((size / 1024 / 1024 * 10 + size / 1024 % 1024 / 100))
+    size="$((size / 10)).$((size % 10)) MB"
+
+  elif [ $size -gt $((1024)) ]; then
+    size=$((size / 1024 * 10 + size % 1024 / 100))
+    size="$((size / 10)).$((size % 10)) KB"
+
+  else
+    size="${size} B"
+  fi
+
+  printf '<li><span class=name>%s</span>
+          <span class=size>%s</span><span class=date>%s</span></li>' \
+    "$(HTML "${file##*/}")" "$size" "$(date -d @"$date" +"%F %T")"
+done
+
+printf %s\\n '</ul>'