-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropbox
executable file
·69 lines (58 loc) · 2.16 KB
/
dropbox
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
# -=- Simple Dropbox Uploader -=-
# Created because my lecturer force me to use Dropbox for some reason
# Requirements: jq
# Load config
#
# --- Example Config File (/home/user/.config/zi_dropbox) ---
#
# TOKEN="INSERT YOUR DROPBOX TOKEN HERE"
# DIRECTORY="dropboxApp"
# TEMPLATE="Shared file: %s" -> Where %s is the shared link
# # DIRECTORY -> "/Dropbox/Apps/<dropbox_app_name>/$DIRECTORY/"
CONF_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/zi_dropbox"
[ -e $CONF_FILE ] && . "$CONF_FILE" || exit 1
# Error stuff (and instruction on how to use this script)
err()
{
>&2 echo "Usage: dropbox [OPTION...] [FILE]
Options:
-h show this message
-n change upload name
-s share file" && exit 1;
}
# Get flags (options)
while getopts "hn:s" opt; do
case "${opt}" in
h) err ;;
n) DB_FILENAME="${OPTARG}";;
s) SHARE=1 ;;
*) ;;
esac
done
shift $((OPTIND - 1))
# Get file from argument
[ -z "$@" ] && err
LOCAL="$@"
[ ! -e "$LOCAL" ] && >&2 echo "File called $LOCAL not Found!" && exit 1
FILENAME="${LOCAL##*/}"
# Log
# LOG_DIR="${XDG_DATA_HOME:-$HOME/local/share}/zi_dropbox"
# [ ! -d $LOG_DIR ] && mkdir $LOG_DIR
# Upload to dropbox using cURL
# DISCLAIMER: Dropbox token is required!
RESULT=$(curl -X POST https://content.dropboxapi.com/2/files/upload \
--silent \
--header "Authorization: Bearer $TOKEN" \
--header "Dropbox-API-Arg: {\"path\": \"/$DIRECTORY/${DB_FILENAME:-$(date "+%Y-%m-%d")-$FILENAME}\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @"$LOCAL")
[ "$(echo $RESULT | jq .error)" = "null" ] && echo "Succesfully uploaded $FILENAME" || { >&2 echo "An error occurred!"; exit 1; }
# Share file then print the url
[ ${SHARE:-0} -lt 1 ] && exit 0
SHARED=$(curl -X POST https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings \
--silent \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"path\": $(echo $RESULT | jq .path_display),\"settings\": {\"requested_visibility\": \"public\",\"audience\": \"public\",\"access\": \"viewer\"}}" | jq -Mr .url)
printf "${TEMPLATE:-Shared link: %s}\n" $SHARED