From ad80bc2c5d655b67abea18b7b6c107896e7b3150 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Fri, 18 Mar 2022 12:52:29 +0100 Subject: [PATCH] attachment conversion and access --- attachment.sh | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.cgi | 11 ++----- 2 files changed, 88 insertions(+), 8 deletions(-) create mode 100755 attachment.sh diff --git a/attachment.sh b/attachment.sh new file mode 100755 index 0000000..68e03b9 --- /dev/null +++ b/attachment.sh @@ -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 diff --git a/index.cgi b/index.cgi index 6f71a4b..ea78af1 100755 --- 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" -- 2.39.2