]> git.plutz.net Git - shellwiki/blob - handlers/40_attachment.sh
fc0b0f983ff00aee3a0787c1e38761315d973697
[shellwiki] / handlers / 40_attachment.sh
1 #!/bin/sh
2
3 # Copyright 2022 - 2023 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 . "$_EXEC/cgilite/file.sh"
18
19 # REV_ATTACHMENTS="${REV_ATTACHMENTS:-false}"
20
21 attachment_convert(){
22   local attpath="$1"
23   local cachepath="${attpath%/#attachments/*}/#cache/${attpath#*/#attachments/}"
24   local res junk
25
26   case $attpath in
27     *.webm|*.mp4|*.mkv|*.avi)
28       cachepath="${cachepath}.webm"
29       ;;
30   esac
31
32   if [ -s "$cachepath" ]; then
33     printf %s "$cachepath"
34     return 0
35   elif [ -f "$cachepath" ]; then
36     printf %s "$attpath"
37     return 0
38   elif ! mkdir -p -- "${cachepath%/*}" && touch -- "$cachepath"; then
39     printf %s "$attpath"
40     return 0
41   fi
42
43   case $attpath in
44     *.[jJ][pP][gG]|*.[jJ][pP][eE][gG]|*.[pP][nN][gG])
45       read junk junk res junk <<-EOF
46         $(identify -- "$attpath")
47         EOF
48       if [ "${res%x*}" -gt 2048 ]; then
49         convert "$attpath" -resize 1920x-2 -quality 85 "$cachepath"
50       else
51         convert "$attpath" -quality 85 "$cachepath"
52       fi
53       printf %s "$cachepath"
54       return 0
55     ;;
56     *.[wW][eE][bB][mM]|*.[mM][pP]4|*.[mM][kK][vV]|*.[aA][vV][iI])
57       res=$(ffprobe -show_entries stream=width "$attpath" 2>&-)
58       res="${res#*width=}" res="${res%%${BR}*}"
59       if [ "$res" -gt 1280 ]; then
60         ( exec >&- 2>&1;
61           ffmpeg -y -nostdin -i "$attpath" \
62           -c:v libvpx -vf scale=1280:-2 -crf 28 -b:v 0 \
63           -c:a libvorbis -q:a 6 \
64           "${cachepath%.*}.tmp.webm" \
65           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
66         & ) &
67        
68       else
69         ( exec >&- 2>&1;
70           ffmpeg -y -nostdin -i "$attpath" \
71           -c:v libvpx -crf 28 -b:v 0 \
72           -c:a libvorbis -q:a 6 \
73           "${cachepath%.*}.tmp.webm" \
74           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
75         & ) &
76       fi
77       printf %s "$attpath"
78       return 0
79     ;;
80     *) printf %s "$attpath";;
81   esac
82 }
83
84 case ${PATH_INFO} in
85   */\[attachment\]/)
86     # no trailing slash
87     REDIRECT "${_BASE}${PATH_INFO%/}"
88     ;;
89   */*/)
90     # attached files never end on /
91     return 1
92     ;;
93   */\[attachment\])
94     # show attachment page
95     page="${PATH_INFO%\[attachment\]}"
96
97     if [ ! -d "$_DATA/pages${page}" -a ! -d "$_DATA/pages${page}" ]; then
98       # base page does not exist
99       return 1
100     elif [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ]; then
101       # pass uploads to next handler
102       return 1
103     elif [ "$(POST action)" ]; then
104       # pass edits to next handler
105       return 1
106     elif ! acl_read "${page}"; then
107       theme_error 403
108       return 0
109     else
110       theme_attachments "${page}"
111       return 0
112     fi
113     ;;
114
115   */\[attachment\]/*)
116     attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
117
118     if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
119       return 1
120     elif ! acl_read "${PATH_INFO%/\[attachment\]/*}"; then
121       theme_error 403
122       return 0
123     elif [ -f "$_DATA/pages/$attpath" ]; then
124       FILE "$_DATA/pages/$attpath"
125       return 0
126     elif [ -f "$_EXEC/pages/$attpath" ]; then
127       FILE "$_EXEC/pages/$attpath"
128       return 0
129     fi
130     ;;
131   */*)
132     attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
133
134     if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
135       return 1
136     elif ! acl_read "${PATH_INFO%/*}/"; then
137       theme_error 403
138       return 0
139     elif [ -f "$_DATA/pages/$attpath" ]; then
140       FILE "$(attachment_convert "$_DATA/pages/$attpath")"
141       return 0
142     elif [ -f "$_EXEC/pages/$attpath" ]; then
143       FILE "$(attachment_convert "$_EXEC/pages/$attpath")"
144       return 0
145     fi
146     ;;
147 esac
148
149 return 1