forked from ox223252/utilsSh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-drive-upload
executable file
·271 lines (230 loc) · 8.46 KB
/
google-drive-upload
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
#help funtion
function usage(){
echo "Source : https://github.com/labbots/google-drive-upload"
echo "To get the folder ID, if it's different of root. Open your drive, open the wanted folder, in the URL copy the last poart and copy it as folder ID"
echo "To get the user ID, open https://console.developers.google.com/api, in id sub section ... juste do it, your client id will be displayed in 'ID clients OAuth 2.0' part"
echo "---"
echo "The script can be used to upload file/directory to google drive."
echo "Usage:"
echo "$0 [options..] <filename> [<filename>]"
echo "Foldername argument is optional. If not provided, the file will be uploaded to preconfigured google drive."
echo "File name argument is optional if create directory option is used."
echo "Options:"
echo "-F | --folder <foldername> - option to create directory. Will provide folder id."
echo "-C | --create-dir <foldername> - option to create directory. Will provide folder id."
echo "-r | --root-dir <google_folderid> - google folder id to which the file/directory to upload."
echo "-v | --verbose - Display detailed message."
echo "-z | --config - Override default config file with custom config file."
echo "-h | --help - Display usage instructions."
exit 0;
}
# Method to extract data from json response
function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}][^://]" '{for(i=1;i<=NF;i++){if($i~/\042'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p | sed -e 's/[}]*$//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[,]*$//'
}
function log() {
if [ "$VERBOSE" = true ]; then
echo -e "${1}"
fi
}
# Method to create directory in google drive. Requires 3 arguments foldername,root directory id and access token.
function createDirectory(){
DIRNAME="$1"
ROOTDIR="$2"
ACCESS_TOKEN="$3"
FOLDER_ID=""
QUERY="mimeType='application/vnd.google-apps.folder' and title='$DIRNAME'"
QUERY=$(echo $QUERY | sed -f ${DIR}/url_escape.sed)
SEARCH_RESPONSE=`/usr/bin/curl \
--silent \
-XGET \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
"https://www.googleapis.com/drive/v2/files/${ROOTDIR}/children?orderBy=title&q=${QUERY}&fields=items%2Fid"`
FOLDER_ID=`echo $SEARCH_RESPONSE | jsonValue id`
if [ -z "$FOLDER_ID" ]
then
CREATE_FOLDER_POST_DATA="{\"mimeType\": \"application/vnd.google-apps.folder\",\"title\": \"$DIRNAME\",\"parents\": [{\"id\": \"$ROOTDIR\"}]}"
CREATE_FOLDER_RESPONSE=`/usr/bin/curl \
--silent \
-X POST \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json; charset=UTF-8" \
-d "$CREATE_FOLDER_POST_DATA" \
"https://www.googleapis.com/drive/v2/files?fields=id"`
FOLDER_ID=`echo $CREATE_FOLDER_RESPONSE | jsonValue id`
fi
echo "$FOLDER_ID"
}
# Method to upload files to google drive. Requires 3 arguments file path, google folder id and access token.
function uploadFile(){
FILE="$1"
FOLDER_ID="$2"
ACCESS_TOKEN="$3"
SLUG=`basename "$FILE"`
FILENAME="${SLUG%.*}"
EXTENSION="${SLUG##*.}"
if [ "$FILENAME" == "$EXTENSION" -o ! "$(command -v mimetype)" ]
then
MIME_TYPE=`file --brief --mime-type "$FILE"`
else
MIME_TYPE=`mimetype --output-format %m "$FILE"`
fi
FILESIZE=$(stat -c%s "$FILE")
# JSON post data to specify the file name and folder under while the file to be created
postData="{\"mimeType\": \"$MIME_TYPE\",\"title\": \"$SLUG\",\"parents\": [{\"id\": \"$FOLDER_ID\"}]}"
postDataSize=$(echo $postData | wc -c)
# Curl command to initiate resumable upload session and grab the location URL
log "Generating upload link for file $FILE ..."
uploadlink=`/usr/bin/curl \
--silent \
-X POST \
-H "Host: www.googleapis.com" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json; charset=UTF-8" \
-H "X-Upload-Content-Type: $MIME_TYPE" \
-H "X-Upload-Content-Length: $FILESIZE" \
-d "$postData" \
"https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable" \
--dump-header - | sed -ne s/"Location: "//pi | tr -d '\r\n'`
# Curl command to push the file to google drive.
# If the file size is large then the content can be split to chunks and uploaded.
# In that case content range needs to be specified.
log "Uploading file $FILE to google drive..."
curl \
-X PUT \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: $MIME_TYPE" \
-H "Content-Length: $FILESIZE" \
-H "Slug: $SLUG" \
--upload-file "$FILE" \
--output /dev/null \
"$uploadlink" \
$curl_args
}
#Configuration variables
ROOT_FOLDER=""
CLIENT_ID=""
CLIENT_SECRET=""
REFRESH_TOKEN=""
SCOPE=${SCOPE:-"https://docs.google.com/feeds"}
#Internal variable
ACCESS_TOKEN=""
FILE=""
FILES=""
FOLDERNAME=""
curl_args=""
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
if [ -e $HOME/.googledrive.conf ]; then
. $HOME/.googledrive.conf
fi
VERBOSE=false
HELP=false
CONFIG=""
ROOTDIR=""
TMP=""
while [ "$1" != "" ]; do
case "$1" in
-F | --folder ) TMP=$1; shift;;
-v | --verbose ) VERBOSE=true;curl_args="--progress"; shift ;;
-h | --help ) usage; shift ;;
-C | --create-dir ) TMP=$1; shift 2 ;;
-r | --root-dir ) ROOTDIR="$2";ROOT_FOLDER="$2"; shift 2 ;;
-z | --config ) CONFIG="$2"; shift 2 ;;
-- ) shift; break ;;
* )
case "$TMP" in
-F | --folder )FOLDERNAME=$1; TMP="";;
-C | --create-dir )FOLDERNAME=$1; TMP="";;
*) FILES="$FILES $1";;
esac
shift
;;
esac
done
if [ "$FILES" = "" ]; then
usage
fi
if [ ! -z "$CONFIG" ]; then
if [ -e "$CONFIG" ]; then
. $CONFIG
fi
if [ ! -z "$ROOTDIR" ]; then
ROOT_FOLDER="$ROOTDIR"
fi
fi
if [ ! -z "$1" ]; then
FILE=$1
fi
old_umask=`umask`
umask 0077
if [ -z "$ROOT_FOLDER" ]; then
read -p "Root Folder ID (Default: root): " ROOT_FOLDER
if [ -z "$ROOT_FOLDER" ] || [ `echo $ROOT_FOLDER | tr [:upper:] [:lower:]` = `echo "root" | tr [:upper:] [:lower:]` ]
then
ROOT_FOLDER="root"
echo "ROOT_FOLDER=$ROOT_FOLDER" >> $HOME/.googledrive.conf
else
if expr "$ROOT_FOLDER" : '^[A-Za-z0-9_]\{28\}$' > /dev/null
then
echo "ROOT_FOLDER=$ROOT_FOLDER" >> $HOME/.googledrive.conf
else
echo "Invalid root folder id"
exit -1
fi
fi
fi
if [ -z "$CLIENT_ID" ]; then
read -p "Client ID: " CLIENT_ID
echo "CLIENT_ID=$CLIENT_ID" >> $HOME/.googledrive.conf
fi
if [ -z "$CLIENT_SECRET" ]; then
read -p "Client Secret: " CLIENT_SECRET
echo "CLIENT_SECRET=$CLIENT_SECRET" >> $HOME/.googledrive.conf
fi
if [ -z "$REFRESH_TOKEN" ]; then
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/device/code" --data "client_id=$CLIENT_ID&scope=$SCOPE"`
DEVICE_CODE=`echo "$RESPONSE" | jsonValue "device_code"`
USER_CODE=`echo "$RESPONSE" | jsonValue "user_code"`
URL=`echo "$RESPONSE" | jsonValue "verification_url"`
echo -n "Go to $URL and enter $USER_CODE to grant access to this application. Hit enter when done..."
read
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&code=$DEVICE_CODE&grant_type=http://oauth.net/grant_type/device/1.0"`
ACCESS_TOKEN=`echo "$RESPONSE" | jsonValue access_token`
REFRESH_TOKEN=`echo "$RESPONSE" | jsonValue refresh_token`
echo "REFRESH_TOKEN=$REFRESH_TOKEN" >> $HOME/.googledrive.conf
fi
if [ -z "$ACCESS_TOKEN" ]; then
# Access token generation
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&refresh_token=$REFRESH_TOKEN&grant_type=refresh_token"`
ACCESS_TOKEN=`echo $RESPONSE | jsonValue access_token`
fi
# Check to find whether the folder exists in google drive. If not then the folder is created in google drive under the configured root folder
if [ -z "$FOLDERNAME" ] || [[ `echo "$FOLDERNAME" | tr [:upper:] [:lower:]` = `echo "root" | tr [:upper:] [:lower:]` ]]; then
if [[ `echo "$FOLDERNAME" | tr [:upper:] [:lower:]` = `echo "root" | tr [:upper:] [:lower:]` ]]; then
ROOT_FOLDER="root"
fi
FOLDER_ID=$ROOT_FOLDER
else
FOLDER_ID=`createDirectory "$FOLDERNAME" "$ROOT_FOLDER" "$ACCESS_TOKEN"`
fi
log "Folder ID for folder name $FOLDERNAME : $FOLDER_ID"
# Check whether the given file argument is valid and check whether the argument is file or directory.
# based on the type, if the argument is directory do a recursive upload.
for FILE in $FILES
do
if [ ! -z "$FILE" ]; then
if [ -f "$FILE" ]; then
uploadFile "$FILE" "$FOLDER_ID" "$ACCESS_TOKEN"
elif [ -d "$FILE" ]; then
FOLDERNAME=$(basename $FILE)
FOLDER_ID=`createDirectory "$FOLDERNAME" "$ROOT_FOLDER" "$ACCESS_TOKEN"`
for file in $(find "$FILE" -type f);
do
uploadFile "$file" "$FOLDER_ID" "$ACCESS_TOKEN"
done
fi
fi
done