1
0
Fork 0
Scripts/Perso/LAU.sh

61 lines
2.2 KiB
Bash
Raw Normal View History

#! /bin/bash
#trotFunky's Linux Auto Uploader (LAU)
#Uses dropbox HTTP API through curl to upload a file and copy shareable link into clipboard
#NEEDS : curl, xclip
if [ -z "$DROPBOX_TOKEN" ]; then
echo "No token provided."
echo "Please set the env variable DROPBOX_TOKEN to the dropbox authorization token"
exit -1
fi
if [ $# -lt 1 ]; then
echo "Too few arguments. Expecting one agument: File name"
exit -1
elif [ $# -gt 1 -a "$2" != "debug" ]; then
echo "Too many arguments. Expecting only one argument: File name"
exit -1
elif [ ! -f "$1" ]; then
echo "File does not exist or is not uploadable"
exit -1
fi
filePath=$1
if [[ $filePath = */* ]]; then #If the string contains a /
fileName="/${filePath##*/}" #Or you know, just remove the largest possible string matching */ and a / at the beginning (Keeping the other for reference)
#fileName="${filePath#"${filePath%/*}"}" #Variable expansion. Innermost bracket reads "Get what does not match the /* anchored at the end".
else #The outermost bracket then removes it from the original string (By doing the same as above but anchored to the start)
fileName="/$filePath"
fi
if [ -n "$2" ]; then
echo $filePath
echo $fileName
fi
#CF curl's and dropbox API's documentations
upload=$(curl --silent https://content.dropboxapi.com/2/files/upload \
-H "Authorization: Bearer $DROPBOX_TOKEN" \
-H 'Dropbox-API-Arg: {"path": "'"$fileName"'", "mode": "add", "autorename": true, "mute": false}' \
-H "Content-type: application/octet-stream" \
--data-binary @"$filePath" )
link=$(curl --silent https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
-H "Authorization: Bearer $DROPBOX_TOKEN" \
-H "Content-type: application/json" \
-d '{"path": "'"$fileName"'"}' )
if [ -n "$2" ]; then
echo $upload
echo $link
fi
tmpShareLink="$(echo -n $link | cut -d, -f2 | cut -d: -f2-3)" #According to the returned data, this is where the URL is
tmpShareLink="${tmpShareLink#*\"}" #Removes everything matching *"
tmpShareLink="${tmpShareLink%\"*}" #Removes everything matching "*
shareLink="${tmpShareLink/https:\/\/www/https:\/\/dl}" #Replaces www by dl for direct link
echo -n "$shareLink" | xclip -selection clipboard