]> git.plutz.net Git - rawnet/blob - upload.sh
63704071050e2683ef0374581c616408c786b855
[rawnet] / upload.sh
1 #!/bin/sh
2
3 UPLOAD(){
4   local file="$1"
5   local boundary line length=0
6
7   [ ! "${CONTENT_TYPE}" -o "${CONTENT_TYPE##multipart/form-data;*}" ] && return 1
8
9   boundary="${CONTENT_TYPE#*; boundary=}"
10   boundary="${boundary%%;*}"
11
12   while read -r line; do
13     length="$(( length + ${#line} + 1))"
14     [ "${line%${CR}}" = "--$boundary" ] && break
15   done
16   while read -r line; do
17     length="$(( length + ${#line} + 1))"
18     [ ! "${line%${CR}}" ] && break \
19     || debug "$line"
20   done
21
22   printf "%i\n" "$(( CONTENT_LENGTH - length ))" >"${file}.upload"
23   head -c "$(( CONTENT_LENGTH - length ))" \
24   | sed -nE '
25     # print lines until boundary ( = actual file upload)
26     :FILE; p; n;
27     /^--'"${boundary}"'(--)?\r?$/!bFILE;
28     # discard remaining lines
29     :END; $q; n; bEND;
30   ' >"$file"
31   truncate -s $(( $(stat -c %s -- "$file") -2 )) -- "$file"
32   rm -- "${file}.upload"
33 }
34
35 frame_uploadprogress() {
36   printf '<!DOCTYPE HTML>
37   <html><head>
38     <title>Upload Progress</title>
39     <link rel="stylesheet" type="text/css" href="%s/rawnet.css" />
40   </head><body id=uploadprogress>
41   ' "$_BASE"
42   printf '<div class=progress><div class=bar style="width: 0%%;"></div><div class=count>%i / %i</div></div>\n' 0 0
43   while [  ! -f "$_DATA/$channel/$video.upload.mp4" \
44         -a ! -f "$_DATA/$channel/$video.upload.mp4.upload" ]; do sleep 1; done
45   read size <"$_DATA/$channel/$video.upload.mp4.upload"
46   while [ -f "$_DATA/$channel/$video.upload.mp4.upload" ]; do
47     stat="$(stat -c %s "$_DATA/$channel/$video.upload.mp4" 2>&-)"
48     printf '<div class=progress><div class=bar style="width:%i%%;"></div><div class=count>%iMB / %iMB</div></div>\n' \
49       "$(( stat * 100 / size ))" "$((stat / 1048576))" "$((size / 1048576))"
50     sleep 1
51   done
52   printf '<span class=progress><div class=bar style="width:100%%;"></div><div class=count>Ready!</div></span>\n'
53   printf '</body></html>'
54 }