--- /dev/null
+#!/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