]> git.plutz.net Git - shellwiki/blob - handlers/20_attachment.sh
efc66f1b77f714fa4ed6afcfc25030c2e87b8258
[shellwiki] / handlers / 20_attachment.sh
1 #!/bin/sh
2
3 . "$_EXEC/cgilite/file.sh"
4
5 attachment_convert(){
6   local attpath="$1"
7   local cachepath="${attpath%/#attachments/*}/#cache/${attpath#*/#attachments/}"
8   local res junk
9
10   case $attpath in
11     *.webm|*.mp4|*.mkv|*.avi)
12       cachepath="${cachepath}.webm"
13       ;;
14   esac
15
16   if [ -s "$cachepath" ]; then
17     printf %s "$cachepath"
18     return 0
19   elif [ -f "$cachepath" ]; then
20     printf %s "$attpath"
21     return 0
22   elif ! mkdir -p -- "${cachepath%/*}" && touch "$cachepath"; then
23     printf %s "$attpath"
24     return 0
25   fi
26
27   case $attpath in
28     *.jpg|*.jpeg|*.png)
29       read junk junk res junk <<-EOF
30         $(identify "$attpath")
31         EOF
32       if [ "${res%x*}" -gt 2048 ]; then
33         convert "$attpath" -resize 1920x-2 -quality 85 "$cachepath"
34       else
35         convert "$attpath" -quality 85 "$cachepath"
36       fi
37       printf %s "$cachepath"
38       return 0
39     ;;
40     *.webm|*.mp4|*.mkv|*.avi)
41       res=$(ffprobe -show_entries stream=width "$attpath" 2>&-)
42       res="${res#*width=}" res="${res%%${BR}*}"
43       if [ "$res" -gt 1280 ]; then
44         ( exec >&- 2>&1;
45           ffmpeg -y -nostdin -i "$attpath" \
46           -c:v libvpx -vf scale=1280:-2 -crf 28 -b:v 0 \
47           -c:a libvorbis -q:a 6 \
48           "${cachepath%.*}.tmp.webm" \
49           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
50         & ) &
51        
52       else
53         ( exec >&- 2>&1;
54           ffmpeg -y -nostdin -i "$attpath" \
55           -c:v libvpx -crf 28 -b:v 0 \
56           -c:a libvorbis -q:a 6 \
57           "${cachepath%.*}.tmp.webm" \
58           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
59         & ) &
60       fi
61       printf %s "$attpath"
62       return 0
63     ;;
64   esac
65 }
66
67 case ${PATH_INFO} in
68   */\[attachment\]/)
69     REDIRECT "${_BASE}${PATH_INFO%/}"
70     ;;
71   */\[attachment\])
72     tsid="$(POST session_key)"; tsid="${tsid%% *}"
73     attachment_delete="$(POST delete)"
74
75     if [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ] && acl_write "${PATH_INFO%\[attachment\]}"; then
76       . "$_EXEC/multipart.sh"
77       multipart_cache
78
79       # Validate session id from form to prevent CSRF
80       # Only validate if username is present, because no username means
81       # anonymous uploads are allowed via acl and cgilite/session.sh does not
82       # validate anonymous sessions from a multipart/formdata
83       if [ "$USER_NAME" -a "$(multipart session_id)" != "$SESSION_ID" ]; then
84         rm -- "$multipart_cachefile"
85         printf 'Refresh: %i\r\n' 4
86         theme_error 403
87         return 0
88       fi
89
90       mkdir -p "$_DATA/pages/${PATH_INFO%/\[attachment\]}/#attachments/"
91       n=1; while filename=$(multipart_filename "file" "$n"); do
92         filename="$(printf %s "$filename" |tr /\\0 __)"
93         multipart "file" "$n" >"$_DATA/pages/${PATH_INFO%/\[attachment\]}/#attachments/$filename"
94         n=$((n + 1))
95       done
96       rm -- "$multipart_cachefile"
97       REDIRECT "${_BASE}${PATH_INFO}"
98     elif [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ]; then
99       printf 'Refresh: %i\r\n' 4
100       theme_error 403
101       head -c $((CONTENT_LENGTH)) >/dev/null
102       return 0
103     elif [ "$attachment_delete" -a "$SESSION_ID" = "$tsid" ]; then
104       rm -- "$_DATA/pages/${PATH_INFO%/\[attachment\]}/#attachments/$attachment_delete"
105       REDIRECT "${_BASE}${PATH_INFO}"
106     elif [ "$attachment_delete" ]; then
107       printf 'Refresh: %i\r\n' 4
108       theme_error 403
109       return 0
110     elif acl_read "${PATH_INFO%\[attachment\]}"; then
111       theme_attachments "${PATH_INFO%\[attachment\]}"
112       return 0
113     else
114       theme_error 404
115       return 0
116     fi
117     ;;
118
119   */\[attachment\]/*)
120     attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
121
122     if ! acl_read "${PATH_INFO%/\[attachment\]/*}"; then
123       theme_error 403
124       return 0
125     elif [ -f "$_DATA/pages/$attpath" ]; then
126       FILE "$_DATA/pages/$attpath"
127       return 0
128     elif [ -f "$_EXEC/pages/$attpath" ]; then
129       FILE "$_EXEC/pages/$attpath"
130       return 0
131     else
132       theme_error 404
133       return 0
134     fi
135     ;;
136   */*/)
137     return 1
138     ;;
139   */*)
140     attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
141
142     if ! acl_read "${PATH_INFO%/*}/"; then
143       theme_error 403
144       return 0
145     elif [ -f "$_DATA/pages/$attpath" ]; then
146       FILE "$(attachment_convert "$_DATA/pages/$attpath")"
147       return 0
148     elif [ -f "$_EXEC/pages/$attpath" ]; then
149       FILE "$(attachment_convert "$_EXEC/pages/$attpath")"
150       return 0
151     elif [ -d "$_DATA/pages/${PATH_INFO}" -o -d "$_EXEC/pages/${PATH_INFO}" ]; then
152       REDIRECT "${_BASE}${PATH_INFO}/"
153     elif [ "${PATH_INFO%\[*\]}" = "${PATH_INFO}" ]; then
154       theme_error 404
155       return 0
156     fi
157     ;;
158 esac
159
160 return 1