From 63944ba313ecffcd90b284f557bad2a50815cf76 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sun, 15 Jul 2018 03:54:48 +0200 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20du=20Linux=20Auto=20Uploader=20?= =?UTF-8?q?(LAU):=20script=20permettant=20d'uploader=20sur=20ma=20dropbox?= =?UTF-8?q?=20depuis=20la=20ligne=20de=20commande=20et=20d'obtenir=20le=20?= =?UTF-8?q?lien=20de=20t=C3=A9l=C3=A9chargement.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: -Process d'OAuth2 complet -Gestion des sous-dossiers pour le fichier uploadé (Deuxième argument optionnel? +regex?) --- Perso/LAU.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 Perso/LAU.sh diff --git a/Perso/LAU.sh b/Perso/LAU.sh new file mode 100755 index 0000000..9f5e88d --- /dev/null +++ b/Perso/LAU.sh @@ -0,0 +1,60 @@ +#! /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