]> git.ekhem.eu.org Git - gdrive_proxy.git/commitdiff
Wrap `upload` to return HTTP responses.
authorJakub Czajka <jakub@ekhem.eu.org>
Tue, 21 Nov 2023 23:30:49 +0000 (00:30 +0100)
committerJakub Czajka <jakub@ekhem.eu.org>
Mon, 4 Dec 2023 02:17:06 +0000 (03:17 +0100)
upload.sh [new file with mode: 0755]

diff --git a/upload.sh b/upload.sh
new file mode 100755 (executable)
index 0000000..b676799
--- /dev/null
+++ b/upload.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+# Copyright (c) 2023 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+. /etc/environment
+
+# POST request has the following format.
+#
+# ------<boundary>
+# Content-Disposition: [...]
+# Content-Type: [...]
+#
+# <file contents>
+# ------<boundary>
+# Content-Type: [...]
+#
+# <name>
+# ------<boundary>--
+
+# For each line, exchange the contents of the pattern space and the hold space.
+# First line is exchanged with an empty hold space. Lines [2;N] are exchanged
+# with the preceeding line. Therefore, last line is exchanged with the second to
+# last line. This is the final pattern space, which is printed. However, sed
+# adds \r\n which needs to be removed.
+NAME_ON_DRIVE="$(sed --silent 'x;$p' ${PATH_TO_FILE} | tr --delete '\r\n')"
+
+# First, delete lines 1-4. Then, for lines 4-5 append the next line to the
+# pattern space. This results in a pattern space with lines 5-9. Finally, from
+# line 10, append the next line to the pattern space and then print and delete
+# the first line of the pattern space. This creates a 5-lines long sliding
+# window which are yet to be printed. At the end of the file we are left with 5
+# unprinted lines.
+sed --in-place --silent --expression='1,4d' \
+    --expression=':a;N;4,9ba;P;D' ${PATH_TO_FILE}
+
+echo "HTTP/1.1 200 OK"
+echo "Content-Type: text/html"
+echo "
+<!DOCTYPE html>
+<html>
+<head>
+  <style>
+    body {
+      white-space: pre-wrap;
+    }
+  </style>
+</head>
+<body>"
+echo "Uploading ${NAME_ON_DRIVE}"
+python3 -u -m gdrive_knife 2>&1 upload --token ${gdrive_auth_token} \
+    --key ${gdrive_encryption_key} ${PATH_TO_FILE} ${NAME_ON_DRIVE}
+echo "\
+</body>
+</html>"