]> git.plutz.net Git - shellwiki/blob - attachment.sh
attachment processing, serving and overview
[shellwiki] / attachment.sh
1 #!/bin/sh
2
3 attachment_convert(){
4   local attpath="$1"
5   local cachepath="${attpath%/#attachments/*}/#cache/${attpath#*/#attachments/}"
6   local res junk
7
8   case $attpath in
9     *.webm|*.mp4|*.mkv|*.avi)
10       cachepath="${cachepath}.webm"
11       ;;
12   esac
13
14   if [ -s "$cachepath" ]; then
15     printf %s "$cachepath"
16     return 0
17   elif [ -f "$cachepath" ]; then
18     printf %s "$attpath"
19     return 0
20   elif ! touch "$cachepath"; then
21     printf %s "$attpath"
22     return 0
23   fi
24
25   case $attpath in
26     *.jpg|*.jpeg|*.png)
27       read junk junk res junk <<-EOF
28         $(identify "$attpath")
29         EOF
30       if [ "${res%x*}" -gt 2048 ]; then
31         convert "$attpath" -resize 1920x-2 -quality 85 "$cachepath"
32       else
33         convert "$attpath" -quality 85 "$cachepath"
34       fi
35       printf %s "$cachepath"
36       return 0
37     ;;
38     *.webm|*.mp4|*.mkv|*.avi)
39       res=$(ffprobe -show_entries stream=width "$attpath" 2>&-)
40       res="${res#*width=}" res="${res%%${BR}*}"
41       if [ "$res" -gt 1280 ]; then
42         ( ffmpeg -y -nostdin -i "$attpath" \
43           -c:v libvpx -vf scale=1280:-2 -crf 28 -b:v 0 \
44           -c:a libvorbis -q:a 6 \
45           "${cachepath%.*}.tmp.webm" \
46           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
47         & ) &
48        
49       else
50         ( ffmpeg -y -nostdin -i "$attpath" \
51           -c:v libvpx -crf 28 -b:v 0 \
52           -c:a libvorbis -q:a 6 \
53           "${cachepath%.*}.tmp.webm" \
54           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
55         & ) &
56       fi
57       printf %s "$attpath"
58       return 0
59     ;;
60   esac
61 }
62
63 if [ "${PATH_INFO%/\[attachment\]/}"  != "${PATH_INFO}" ]; then
64   theme_attachments "${PATH_INFO%\[attachment\]/}"
65
66 elif [ "${PATH_INFO%/\[attachment\]/*}" != "${PATH_INFO}" ]; then
67   attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
68   if [ -f "$_DATA/pages/$attpath" ]; then
69     FILE "$_DATA/pages/$attpath"
70   elif [ -f "$_EXEC/pages/$attpath" ]; then
71     FILE "$_EXEC/pages/$attpath"
72   else
73     theme_404
74   fi
75   exit 0;
76   
77 elif [ "${PATH_INFO%/}" = "${PATH_INFO}" ]; then
78   attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
79   if [ -f "$_DATA/pages/$attpath" ]; then
80     FILE "$(attachment_convert "$_DATA/pages/$attpath")"
81   elif [ -f "$_EXEC/pages/$attpath" ]; then
82     FILE "$(attachment_convert "$_EXEC/pages/$attpath")"
83   elif [ -d "$_DATA/pages/${PATH_INFO}" -o -d "$_EXEC/pages/${PATH_INFO}" ]; then
84     REDIRECT "${_BASE}${PATH_INFO}/"
85   fi
86   exit 0
87
88 fi