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