From a2273bc98492f6dc292f01ab53e6d22c9bd285ed Mon Sep 17 00:00:00 2001 From: Jakub Czajka Date: Wed, 22 Nov 2023 00:30:49 +0100 Subject: [PATCH] Wrap `upload` to return HTTP responses. --- upload.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 upload.sh diff --git a/upload.sh b/upload.sh new file mode 100755 index 0000000..b676799 --- /dev/null +++ b/upload.sh @@ -0,0 +1,54 @@ +#!/bin/sh +# Copyright (c) 2023 Jakub Czajka +# License: GPL-3.0 or later. + +. /etc/environment + +# POST request has the following format. +# +# ------ +# Content-Disposition: [...] +# Content-Type: [...] +# +# +# ------ +# Content-Type: [...] +# +# +# -------- + +# 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 " + + + + + +" +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 "\ + +" -- 2.39.5