]> git.plutz.net Git - shellwiki/commitdiff
attachment conversion and access
authorPaul Hänsch <paul@plutz.net>
Fri, 18 Mar 2022 11:52:29 +0000 (12:52 +0100)
committerPaul Hänsch <paul@plutz.net>
Fri, 18 Mar 2022 11:52:29 +0000 (12:52 +0100)
attachment.sh [new file with mode: 0755]
index.cgi

diff --git a/attachment.sh b/attachment.sh
new file mode 100755 (executable)
index 0000000..68e03b9
--- /dev/null
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+attachment_convert(){
+  local attpath="$1"
+  local cachepath="${attpath%/#attachments/*}/#cache/${attpath#*/#attachments/}"
+  local res junk
+
+  case $attpath in
+    *.webm|*.mp4|*.mkv|*.avi)
+      cachepath="${cachepath}.webm"
+      ;;
+  esac
+
+  if [ -s "$cachepath" ]; then
+    printf %s "$cachepath"
+    return 0
+  elif [ -f "$cachepath" ]; then
+    printf %s "$attpath"
+    return 0
+  elif ! touch "$cachepath"; then
+    printf %s "$attpath"
+    return 0
+  fi
+
+  case $attpath in
+    *.jpg|*.jpeg|*.png)
+      read junk junk res junk <<-EOF
+       $(identify "$attpath")
+       EOF
+      if [ "${res%x*}" -gt 2048 ]; then
+        convert "$attpath" -resize 1920x-2 -quality 85 "$cachepath"
+      else
+        convert "$attpath" -quality 85 "$cachepath"
+      fi
+      printf %s "$cachepath"
+      return 0
+    ;;
+    *.webm|*.mp4|*.mkv|*.avi)
+      res=$(ffprobe -show_entries stream=width "$attpath" 2>&-)
+      res="${res#*width=}" res="${res%%${BR}*}"
+      if [ "$res" -gt 1280 ]; then
+        ( ffmpeg -y -nostdin -i "$attpath" \
+          -c:v libvpx -vf scale=1280:-2 -crf 28 -b:v 0 \
+          -c:a libvorbis -q:a 6 \
+          "${cachepath%.*}.tmp.webm" \
+          && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
+        & ) &
+       
+      else
+        ( ffmpeg -y -nostdin -i "$attpath" \
+          -c:v libvpx -crf 28 -b:v 0 \
+          -c:a libvorbis -q:a 6 \
+          "${cachepath%.*}.tmp.webm" \
+          && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
+        & ) &
+      fi
+      printf %s "$attpath"
+      return 0
+    ;;
+  esac
+}
+
+if [ "${PATH_INFO%/\[attachment\]/*}" != "${PATH_INFO}" ]; then
+  attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
+  if [ -f "$_DATA/pages/$attpath" ]; then
+    FILE "$_DATA/pages/$attpath"
+  elif [ -f "$_EXEC/pages/$attpath" ]; then
+    FILE "$_EXEC/pages/$attpath"
+  else
+    theme_404
+  fi
+  exit 0;
+  
+elif [ "${PATH_INFO%/}" = "${PATH_INFO}" ]; then
+  attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
+  if [ -f "$_DATA/pages/$attpath" ]; then
+    FILE "$(attachment_convert "$_DATA/pages/$attpath")"
+  elif [ -f "$_EXEC/pages/$attpath" ]; then
+    FILE "$(attachment_convert "$_EXEC/pages/$attpath")"
+  elif [ -d "$_DATA/pages/${PATH_INFO}" -o -d "$_EXEC/pages/${PATH_INFO}" ]; then
+    REDIRECT "${_BASE}${PATH_INFO}/"
+  fi
+  exit 0
+
+fi
index 6f71a4b6aaa54ea0a7538c08c813a65f55c159d7..ea78af168a22a187e80bfa4e1bfa302b7afa67dd 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -78,8 +78,6 @@ attachment() {
   fi
 }
 
-. "$_EXEC/page_edit.sh"
-
 case "${PATH_INFO}" in
   /"[.]"/*)
     FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
@@ -100,13 +98,10 @@ case "${PATH_INFO}" in
   */"[register]")
     theme_register
     ;;
-  */"[attachment]"/*)
-    :  # TODO: Original attachment file
-    ;;
   */"#"*)
     :  # TODO: Invalid page name
     ;;
-  *)
-    attachment "${PATH_INFO}"
-    ;;
 esac
+
+. "$_EXEC/page_edit.sh"
+. "$_EXEC/attachment.sh"