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 res="$(identify -- "$attpath")"
46 res="${res% * * * * * *}" res="${res##* }"
47 if [ "${res%x*}" -gt 2048 ]; then
48 convert "$attpath" -resize 1920x-2 -quality 85 "$cachepath"
50 convert "$attpath" -quality 85 "$cachepath"
52 printf %s "$cachepath"
55 *.[wW][eE][bB][mM]|*.[mM][pP]4|*.[mM][kK][vV]|*.[aA][vV][iI])
56 res=$(ffprobe -show_entries stream=width "$attpath" 2>&-)
57 res="${res#*width=}" res="${res%%${BR}*}"
58 if [ "$res" -gt 1280 ]; then
60 ffmpeg -y -nostdin -i "$attpath" \
61 -c:v libvpx -vf scale=1280:-2 -crf 28 -b:v 0 \
62 -c:a libvorbis -q:a 6 \
63 "${cachepath%.*}.tmp.webm" \
64 && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
69 ffmpeg -y -nostdin -i "$attpath" \
70 -c:v libvpx -crf 28 -b:v 0 \
71 -c:a libvorbis -q:a 6 \
72 "${cachepath%.*}.tmp.webm" \
73 && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
79 *) printf %s "$attpath";;
86 REDIRECT "${_BASE}${PATH_INFO%/}"
89 # attached files never end on /
93 # show attachment page
94 page="${PATH_INFO%\[attachment\]}"
96 if [ ! -d "$_DATA/pages${page}" -a ! -d "$_DATA/pages${page}" ]; then
97 # base page does not exist
99 elif [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ]; then
100 # pass uploads to next handler
102 elif [ "$(POST action)" ]; then
103 # pass edits to next handler
105 elif ! acl_read "${page}"; then
109 theme_attachments "${page}"
115 attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
117 if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
119 elif ! acl_read "${PATH_INFO%/\[attachment\]/*}"; then
122 elif [ -f "$_DATA/pages/$attpath" ]; then
123 FILE "$_DATA/pages/$attpath"
125 elif [ -f "$_EXEC/pages/$attpath" ]; then
126 FILE "$_EXEC/pages/$attpath"
131 attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
133 if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
135 elif ! acl_read "${PATH_INFO%/*}/"; then
138 elif [ -f "$_DATA/pages/$attpath" ]; then
139 FILE "$(attachment_convert "$_DATA/pages/$attpath")"
141 elif [ -f "$_EXEC/pages/$attpath" ]; then
142 FILE "$(attachment_convert "$_EXEC/pages/$attpath")"