From: Jakub Czajka Date: Tue, 21 Nov 2023 23:30:49 +0000 (+0100) Subject: Wrap `upload` to return HTTP responses. X-Git-Url: https://git.ekhem.eu.org/?a=commitdiff_plain;h=a2273bc98492f6dc292f01ab53e6d22c9bd285ed;p=gdrive_proxy.git Wrap `upload` to return HTTP responses. --- 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 "\ + +"