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