]> git.plutz.net Git - shellwiki/blob - handlers/40_attachment.sh
Merge commit 'ff5af5cdcdc909682fede7999751bce20694bc81'
[shellwiki] / handlers / 40_attachment.sh
1 #!/bin/sh
2
3 . "$_EXEC/cgilite/file.sh"
4
5 # REV_ATTACHMENTS="${REV_ATTACHMENTS:-false}"
6
7 attachment_convert(){
8   local attpath="$1"
9   local cachepath="${attpath%/#attachments/*}/#cache/${attpath#*/#attachments/}"
10   local res junk
11
12   case $attpath in
13     *.webm|*.mp4|*.mkv|*.avi)
14       cachepath="${cachepath}.webm"
15       ;;
16   esac
17
18   if [ -s "$cachepath" ]; then
19     printf %s "$cachepath"
20     return 0
21   elif [ -f "$cachepath" ]; then
22     printf %s "$attpath"
23     return 0
24   elif ! mkdir -p -- "${cachepath%/*}" && touch "$cachepath"; then
25     printf %s "$attpath"
26     return 0
27   fi
28
29   case $attpath in
30     *.jpg|*.jpeg|*.png)
31       read junk junk res junk <<-EOF
32         $(identify "$attpath")
33         EOF
34       if [ "${res%x*}" -gt 2048 ]; then
35         convert "$attpath" -resize 1920x-2 -quality 85 "$cachepath"
36       else
37         convert "$attpath" -quality 85 "$cachepath"
38       fi
39       printf %s "$cachepath"
40       return 0
41     ;;
42     *.webm|*.mp4|*.mkv|*.avi)
43       res=$(ffprobe -show_entries stream=width "$attpath" 2>&-)
44       res="${res#*width=}" res="${res%%${BR}*}"
45       if [ "$res" -gt 1280 ]; then
46         ( exec >&- 2>&1;
47           ffmpeg -y -nostdin -i "$attpath" \
48           -c:v libvpx -vf scale=1280:-2 -crf 28 -b:v 0 \
49           -c:a libvorbis -q:a 6 \
50           "${cachepath%.*}.tmp.webm" \
51           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
52         & ) &
53        
54       else
55         ( exec >&- 2>&1;
56           ffmpeg -y -nostdin -i "$attpath" \
57           -c:v libvpx -crf 28 -b:v 0 \
58           -c:a libvorbis -q:a 6 \
59           "${cachepath%.*}.tmp.webm" \
60           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
61         & ) &
62       fi
63       printf %s "$attpath"
64       return 0
65     ;;
66     *) printf "$attpath";;
67   esac
68 }
69
70 case ${PATH_INFO} in
71   */\[attachment\]/)
72     # no trailing slash
73     REDIRECT "${_BASE}${PATH_INFO%/}"
74     ;;
75   */*/)
76     # attached files never end on /
77     return 1
78     ;;
79   */\[attachment\])
80     # show attachment page
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" ]; then
87       # pass uploads to next handler
88       return 1
89     elif [ "$(POST action)" ]; then
90       # pass edits to next handler
91       return 1
92     elif ! acl_read "${page}"; then
93       theme_error 403
94       return 0
95     else
96       theme_attachments "${page}"
97       return 0
98     fi
99     ;;
100
101   */\[attachment\]/*)
102     attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
103
104     if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
105       return 1
106     elif ! acl_read "${PATH_INFO%/\[attachment\]/*}"; then
107       theme_error 403
108       return 0
109     elif [ -f "$_DATA/pages/$attpath" ]; then
110       FILE "$_DATA/pages/$attpath"
111       return 0
112     elif [ -f "$_EXEC/pages/$attpath" ]; then
113       FILE "$_EXEC/pages/$attpath"
114       return 0
115     fi
116     ;;
117   */*)
118     attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
119
120     if [ ! -f "$_DATA/pages/$attpath" -a ! -f "$_EXEC/pages/$attpath" ]; then
121       return 1
122     elif ! acl_read "${PATH_INFO%/*}/"; then
123       theme_error 403
124       return 0
125     elif [ -f "$_DATA/pages/$attpath" ]; then
126       FILE "$(attachment_convert "$_DATA/pages/$attpath")"
127       return 0
128     elif [ -f "$_EXEC/pages/$attpath" ]; then
129       FILE "$(attachment_convert "$_EXEC/pages/$attpath")"
130       return 0
131     fi
132     ;;
133 esac
134
135 return 1