aboutsummaryrefslogtreecommitdiff
path: root/norns_upload
blob: 73f34eed263693b341eef7a2d0d40b30e846da45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash

if [[ $# -ne 2 ]]; then
  echo "Usage: $0 project filename"
  exit 1
fi

HOST="${NORNS:-norns.local}"
API="/api/v1"
BASE="/dust/code/"
PROJECT="$1"
FILE="$2"
# make directories
# TODO if there's already a directory with a filename this probably
# doesn't do the right thing, but maybe that's the best we can do...
DIR="$PROJECT/"
if [[ "$(dirname "$FILE")" != "." ]]; then
  DIR+="$(dirname "$FILE")"
fi
DIR_URL="http://$HOST$API$BASE$DIR?kind=directory"
URL="http://$HOST$API$BASE$PROJECT/$FILE"


TEMP="$(mktemp)"
BOUNDARY="----$(head -c70 < <(tr -dc '\101-\132\141-\172' < /dev/urandom))"

function finish {
  rm -f "$TEMP"
}
trap finish EXIT

echo "--$BOUNDARY"$'\r\nContent-Disposition: form-data; name="value"; filename="blob"\r\nContent-Type: text/utf-8\r\n\r' > $TEMP
cat < "$FILE" >> $TEMP
echo $'\r\n'"--$BOUNDARY--"$'\r\n' >> $TEMP


# this is idempotent *and* creates all of the intermediate directories
# thanks monome >:)
curl -XPUT "$DIR_URL" --silent > /dev/null

# all we get is an error message in json if this fails
RESULT="$(curl "$URL" -X PUT -H "Content-Type: multipart/form-data; boundary=$BOUNDARY" --data-binary @$TEMP --compressed --silent)"
if jq -e ".error"<<<"$RESULT" >/dev/null; then
  jq -r ".error" <<<"$RESULT"
  exit 1
fi