]> git.plutz.net Git - rawnet/blobdiff - upload.sh
upload function to external file, ui change: put video upload before meta edit
[rawnet] / upload.sh
diff --git a/upload.sh b/upload.sh
new file mode 100755 (executable)
index 0000000..dd678fe
--- /dev/null
+++ b/upload.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+UPLOAD(){
+  local file="$1"
+  local boundary line length=0
+
+  [ ! "${CONTENT_TYPE}" -o "${CONTENT_TYPE##multipart/form-data;*}" ] && return 1
+
+  boundary="${CONTENT_TYPE#*; boundary=}"
+  boundary="${boundary%%;*}"
+
+  while read -r line; do
+    length="$(( length + ${#line} + 1))"
+    [ "${line%${CR}}" = "--$boundary" ] && break
+  done
+  while read -r line; do
+    length="$(( length + ${#line} + 1))"
+    [ ! "${line%${CR}}" ] && break \
+    || debug "$line"
+  done
+
+  printf "%i\n" "$(( CONTENT_LENGTH - length ))" >"${file}.upload"
+  head -c "$(( CONTENT_LENGTH - length ))" \
+  | sed -nE '
+    # print lines until boundary ( = actual file upload)
+    :FILE; p; n;
+    /^--'"${boundary}"'(--)?\r?$/!bFILE;
+    # discard remaining lines
+    :END; $q; n; bEND;
+  ' >"$file"
+  truncate -s $(( $(stat -c %s -- "$file") -2 )) -- "$file"
+  rm -- "${file}.upload"
+}
+
+frame_uploadprogress() {
+  printf '<!DOCTYPE HTML>
+  <html><head>
+    <title>Upload Progress</title>
+    <link rel="stylesheet" type="text/css" href="%s/rawnet.css" />
+  </head><body id=uploadprogress>
+  ' "$_BASE"
+  printf '<div class=progress><div class=bar style="width: 0%%;"></div><div class=count>%i / %i</div></div>\n' 0 0
+  while [ ! -f "$_DATA/$channel/$video.mp4" -a \
+          ! -f "$_DATA/$channel/$video.mp4.upload" ]; do sleep 1; done
+  read size <"$_DATA/$channel/$video.mp4.upload"
+  while [ -f "$_DATA/$channel/$video.mp4.upload" ]; do
+    stat="$(stat -c %s "$_DATA/$channel/$video.mp4" 2>&-)"
+    printf '<div class=progress><div class=bar style="width:%i%%;"></div><div class=count>%iMB / %iMB</div></div>\n' \
+      "$(( stat * 100 / size ))" "$((stat / 1048576))" "$((size / 1048576))"
+    sleep 1
+  done
+  printf '<span class=progress><div class=bar style="width:100%%;"></div><div class=count>Ready!</div></span>\n'
+  printf '</body></html>'
+}