This commit is contained in:
parent
0983eccacc
commit
e4509312ab
47
sos/sos
47
sos/sos
@ -1,13 +1,6 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# sos upload <server> <label:tag> <file>
|
||||
|
||||
# example:
|
||||
# sos upload tools.dropshell.app file:latest ./file.txt
|
||||
|
||||
# this will upload the file to the server, and return the download URL
|
||||
|
||||
|
||||
# get dropshell
|
||||
TEMP_DIR=$(mktemp -d)
|
||||
@ -74,7 +67,7 @@ function upload() {
|
||||
|
||||
|
||||
# upload the file
|
||||
TARGET_URL="https://$server/upload"
|
||||
TARGET_SERVER="https://$server"
|
||||
echo "Uploading $file to $TARGET_URL"
|
||||
|
||||
DATETIME=$(datetime)
|
||||
@ -84,6 +77,11 @@ function upload() {
|
||||
LABELTAGS_JSON=$(printf '"%s",' "${LABELTAGS[@]}")
|
||||
LABELTAGS_JSON="[${LABELTAGS_JSON%,}]"
|
||||
|
||||
# trip whitespace from the file path
|
||||
LOCALHASH=$("${DROPSHELL}" hash "${file}" | tr -d '[:space:]')
|
||||
echo "Local hash: $LOCALHASH"
|
||||
|
||||
|
||||
METADATA_JSON=$(cat <<EOF
|
||||
{
|
||||
"labeltags": $LABELTAGS_JSON,
|
||||
@ -102,27 +100,28 @@ EOF
|
||||
WRITE_TOKEN=$(cat "$TOKENPATH")
|
||||
fi
|
||||
|
||||
# trip whitespace from the file path
|
||||
LOCALHASH=$("${DROPSHELL}" hash "${file}" | tr -d '[:space:]')
|
||||
echo "Local hash: $LOCALHASH"
|
||||
|
||||
FILELINE="-F \"file=@${file}\""
|
||||
EXISTSJSON=$(eval "curl -s \"https://$server/exists/$LOCALHASH\"")
|
||||
EXISTSJSON=$(eval "curl -s \"https://$server/exists/$LOCALHASH\"") || die "Failed to check if file exists"
|
||||
DOESEXIT=$(echo "$EXISTSJSON" | jq -r '.exists')
|
||||
if [ "$DOESEXIT" == "true" ]; then
|
||||
echo "File already exists, skipping upload"
|
||||
FILELINE=""
|
||||
fi
|
||||
|
||||
HASH=""
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
curl --progress-bar -X PUT \
|
||||
-H "Authorization: Bearer ${WRITE_TOKEN}" \
|
||||
$FILELINE \
|
||||
-F "metadata=${METADATA_JSON}" \
|
||||
"$TARGET_URL" \
|
||||
|| die "Failed to upload $file to $TARGET_URL"
|
||||
if [ "$DOESEXIT" == "true" ]; then
|
||||
echo "File already exists, skipping upload"
|
||||
# UPDATE the metadata
|
||||
curl -X PUT -H "Authorization: Bearer ${WRITE_TOKEN}" \
|
||||
-F "metadata=${METADATA_JSON}" \
|
||||
-F "hash=${LOCALHASH}" \
|
||||
"$TARGET_SERVER/update" \
|
||||
|| die "Failed to upload $file to $TARGET_URL"
|
||||
else
|
||||
# UPLOAD the file + metadata
|
||||
curl -X PUT -H "Authorization: Bearer ${WRITE_TOKEN}" \
|
||||
-F "metadata=${METADATA_JSON}" \
|
||||
-F "file=@${file}" \
|
||||
"$TARGET_SERVER/upload" \
|
||||
|| die "Failed to upload $file to $TARGET_URL"
|
||||
fi
|
||||
|
||||
echo " "
|
||||
echo " "
|
||||
|
Loading…
x
Reference in New Issue
Block a user