3 # Copyright 2022 - 2023 Paul Hänsch
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.
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.
17 . "$_EXEC/cgilite/file.sh"
19 # REV_ATTACHMENTS="${REV_ATTACHMENTS:-false}"
23 local cachepath="${attpath%/#attachments/*}/#cache/${attpath#*/#attachments/}"
27 *.webm|*.mp4|*.mkv|*.avi)
28 cachepath="${cachepath}.webm"
32 if [ -s "$cachepath" ]; then
33 printf %s "$cachepath"
35 elif [ -f "$cachepath" ]; then
38 elif ! mkdir -p -- "${cachepath%/*}" && touch -- "$cachepath"; then
44 *.[jJ][pP][gG]|*.[jJ][pP][eE][gG]|*.[pP][nN][gG])
45 read junk junk res junk <<-EOF
46 $(identify -- "$attpath")
48 if [ "${res%x*}" -gt 2048 ]; then
49 convert "$attpath" -resize 1920x-2 -quality 85 "$cachepath"
51 convert "$attpath" -quality 85 "$cachepath"
53 printf %s "$cachepath"
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
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}" \
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}" \
80 *) printf %s "$attpath";;
87 REDIRECT "${_BASE}${PATH_INFO%/}"
90 # attached files never end on /
94 # show attachment page
95 page="${PATH_INFO%\[attachment\]}"
97 if [ ! -d "$_DATA/pages${page}" -a ! -d "$_DATA/pages${page}" ]; then
98 # base page does not exist
100 elif [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ]; then
101 # pass uploads to next handler
103 elif [ "$(POST action)" ]; then
104 # pass edits to next handler
106 elif ! acl_read "${page}"; then
110 theme_attachments "${page}"
116 attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
118 if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
120 elif ! acl_read "${PATH_INFO%/\[attachment\]/*}"; then
123 elif [ -f "$_DATA/pages/$attpath" ]; then
124 FILE "$_DATA/pages/$attpath"
126 elif [ -f "$_EXEC/pages/$attpath" ]; then
127 FILE "$_EXEC/pages/$attpath"
132 attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
134 if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
136 elif ! acl_read "${PATH_INFO%/*}/"; then
139 elif [ -f "$_DATA/pages/$attpath" ]; then
140 FILE "$(attachment_convert "$_DATA/pages/$attpath")"
142 elif [ -f "$_EXEC/pages/$attpath" ]; then
143 FILE "$(attachment_convert "$_EXEC/pages/$attpath")"