]> git.plutz.net Git - shellwiki/blob - attachment.sh
introduce reflink macro
[shellwiki] / attachment.sh
1 #!/bin/sh
2
3 attachment_convert(){
4   local attpath="$1"
5   local cachepath="${attpath%/#attachments/*}/#cache/${attpath#*/#attachments/}"
6   local res junk
7
8   case $attpath in
9     *.webm|*.mp4|*.mkv|*.avi)
10       cachepath="${cachepath}.webm"
11       ;;
12   esac
13
14   if [ -s "$cachepath" ]; then
15     printf %s "$cachepath"
16     return 0
17   elif [ -f "$cachepath" ]; then
18     printf %s "$attpath"
19     return 0
20   elif ! mkdir -p -- "${cachepath%/*}" && touch "$cachepath"; then
21     printf %s "$attpath"
22     return 0
23   fi
24
25   case $attpath in
26     *.jpg|*.jpeg|*.png)
27       read junk junk res junk <<-EOF
28         $(identify "$attpath")
29         EOF
30       if [ "${res%x*}" -gt 2048 ]; then
31         convert "$attpath" -resize 1920x-2 -quality 85 "$cachepath"
32       else
33         convert "$attpath" -quality 85 "$cachepath"
34       fi
35       printf %s "$cachepath"
36       return 0
37     ;;
38     *.webm|*.mp4|*.mkv|*.avi)
39       res=$(ffprobe -show_entries stream=width "$attpath" 2>&-)
40       res="${res#*width=}" res="${res%%${BR}*}"
41       if [ "$res" -gt 1280 ]; then
42         ( exec >&- 2>&1;
43           ffmpeg -y -nostdin -i "$attpath" \
44           -c:v libvpx -vf scale=1280:-2 -crf 28 -b:v 0 \
45           -c:a libvorbis -q:a 6 \
46           "${cachepath%.*}.tmp.webm" \
47           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
48         & ) &
49        
50       else
51         ( exec >&- 2>&1;
52           ffmpeg -y -nostdin -i "$attpath" \
53           -c:v libvpx -crf 28 -b:v 0 \
54           -c:a libvorbis -q:a 6 \
55           "${cachepath%.*}.tmp.webm" \
56           && mv -- "${cachepatch%.*}.tmp.webm" "${cachepath}" \
57         & ) &
58       fi
59       printf %s "$attpath"
60       return 0
61     ;;
62   esac
63 }
64
65 if [ "${PATH_INFO%/\[attachment\]/}"  != "${PATH_INFO}" ]; then
66   . "$_EXEC/multipart.sh"
67
68   if [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ] && acl_write "${PATH_INFO%\[attachment\]/}"; then
69     multipart_cache
70     mkdir -p "$_DATA/pages/${PATH_INFO%/\[attachment\]/}/#attachments/"
71     n=1; while filename=$(multipart_filename "file" "$n"); do
72       filename="$(printf %s "$filename" |tr /\\0 __)"
73       multipart "file" "$n" >"$_DATA/pages/${PATH_INFO%/\[attachment\]/}/#attachments/$filename"
74       n=$((n + 1))
75     done
76     rm -- "$multipart_cachefile"
77     REDIRECT "${_BASE}${PATH_INFO}"
78   elif [ "${CONTENT_TYPE%%;*}" = "multipart/form-data" ]; then
79     theme_403
80   elif acl_read "${PATH_INFO%\[attachment\]/}"; then
81     theme_attachments "${PATH_INFO%\[attachment\]/}"
82   else
83     theme_404
84   fi
85
86 elif [ "${PATH_INFO%/\[attachment\]/*}" != "${PATH_INFO}" ]; then
87   attpath="${PATH_INFO%/\[attachment\]/*}/#attachments/${PATH_INFO##*/}"
88
89   if ! acl_read "${PATH_INFO%/\[attachment\]/*}"; then
90     theme_403
91   elif [ -f "$_DATA/pages/$attpath" ]; then
92     FILE "$_DATA/pages/$attpath"
93   elif [ -f "$_EXEC/pages/$attpath" ]; then
94     FILE "$_EXEC/pages/$attpath"
95   else
96     theme_404
97   fi
98   exit 0;
99   
100 elif [ "${PATH_INFO%/}" = "${PATH_INFO}" ]; then
101   attpath="${PATH_INFO%/*}/#attachments/${PATH_INFO##*/}"
102
103   if ! acl_read "${PATH_INFO%/*}/"; then
104     theme_403
105   elif [ -f "$_DATA/pages/$attpath" ]; then
106     FILE "$(attachment_convert "$_DATA/pages/$attpath")"
107   elif [ -f "$_EXEC/pages/$attpath" ]; then
108     FILE "$(attachment_convert "$_EXEC/pages/$attpath")"
109   elif [ -d "$_DATA/pages/${PATH_INFO}" -o -d "$_EXEC/pages/${PATH_INFO}" ]; then
110     REDIRECT "${_BASE}${PATH_INFO}/"
111   else
112     theme_404
113   fi
114   exit 0
115
116 fi