]> git.plutz.net Git - shellwiki/blob - handlers/20_attachment.sh
bugfix: variable name
[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     # no trailing slash
70     REDIRECT "${_BASE}${PATH_INFO%/}"
71     ;;
72   */*/)
73     # attached files never end on /
74     return 1
75     ;;
76   */\[attachment\])
77     # show attachment page
78     # receive uploads
79     tsid="$(POST session_key)"; tsid="${tsid%% *}"
80     attachment_delete="$(POST delete)"
81     page="${PATH_INFO%\[attachment\]}"
82
83     if [ ! -d "$_DATA/pages${page}" -a ! -d "$_DATA/pages${page}" ]; then
84       # base page does not exist
85       return 1
86     elif [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ] && acl_write "${page}"; then
87       . "$_EXEC/multipart.sh"
88       multipart_cache
89
90       # Validate session id from form to prevent CSRF
91       # Only validate if username is present, because no username means
92       # anonymous uploads are allowed via acl and cgilite/session.sh does not
93       # validate anonymous sessions from a multipart/formdata
94       if [ "$USER_NAME" -a "$(multipart session_id)" != "$SESSION_ID" ]; then
95         rm -- "$multipart_cachefile"
96         printf 'Refresh: %i\r\n' 4
97         theme_error 403
98         return 0
99       fi
100
101       mkdir -p "$_DATA/pages${page}#attachments/"
102       n=1; while filename=$(multipart_filename "file" "$n"); do
103         filename="$(printf %s "$filename" |tr /\\0 __)"
104         multipart "file" "$n" >"$_DATA/pages${page}#attachments/$filename"
105         n=$((n + 1))
106       done
107       rm -- "$multipart_cachefile"
108       REDIRECT "${_BASE}${PATH_INFO}"
109     elif [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ]; then
110       printf 'Refresh: %i\r\n' 4
111       theme_error 403
112       head -c $((CONTENT_LENGTH)) >/dev/null
113       return 0
114     elif [ "$attachment_delete" -a "$SESSION_ID" = "$tsid" ]; then
115       rm -- "$_DATA/pages${page}#attachments/$attachment_delete"
116       REDIRECT "${_BASE}${PATH_INFO}"
117     elif [ "$attachment_delete" ]; then
118       printf 'Refresh: %i\r\n' 4
119       theme_error 403
120       return 0
121     elif ! acl_read "${page}"; then
122       theme_error 403
123       return 0
124     else
125       theme_attachments "${page}"
126       return 0
127     fi
128     ;;
129
130   */\[attachment\]/*)
131     attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
132
133     if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
134       return 1
135     elif ! acl_read "${PATH_INFO%/\[attachment\]/*}"; then
136       theme_error 403
137       return 0
138     elif [ -f "$_DATA/pages/$attpath" ]; then
139       FILE "$_DATA/pages/$attpath"
140       return 0
141     elif [ -f "$_EXEC/pages/$attpath" ]; then
142       FILE "$_EXEC/pages/$attpath"
143       return 0
144     fi
145     ;;
146   */*)
147     attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
148
149     if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
150       return 1
151     elif ! acl_read "${PATH_INFO%/*}/"; then
152       theme_error 403
153       return 0
154     elif [ -f "$_DATA/pages/$attpath" ]; then
155       FILE "$(attachment_convert "$_DATA/pages/$attpath")"
156       return 0
157     elif [ -f "$_EXEC/pages/$attpath" ]; then
158       FILE "$(attachment_convert "$_EXEC/pages/$attpath")"
159       return 0
160     fi
161     ;;
162 esac
163
164 return 1