-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.sh
executable file
·48 lines (40 loc) · 1.63 KB
/
upload.sh
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
#!/bin/sh
while getopts ":f:p:t:" opt
do
case "$opt" in
f ) localFile="$OPTARG" ;;
p ) targetPath="$OPTARG" ;;
t ) apiToken="$OPTARG" ;;
esac
done
# Print helpFunction in case parameters are empty
if [ $localFile ] && [ $targetPath ] && [ $apiToken ]
then
sessionId=$(curl -X POST https://content.dropboxapi.com/2/files/upload_session/start \
--header "Authorization: Bearer ${apiToken}" \
--header "Dropbox-API-Arg: {\"close\": false}" \
--header "Content-Type: application/octet-stream" | jq -r '.session_id')
totalFileSize=$(wc -c ${localFile} | cut -d' ' -f1)
chunkSize=150000000
chunkDir="chunks"
if [ ! -d "${chunkDir}" ]; then
mkdir ${chunkDir}
fi
split -db ${chunkSize} ${localFile} ./${chunkDir}/
cd ${chunkDir}
offset=0
for file in `ls -tU *`
do
curl -X POST https://content.dropboxapi.com/2/files/upload_session/append_v2 \
--header "Authorization: Bearer ${apiToken}" \
--header "Dropbox-API-Arg: {\"cursor\": {\"session_id\": \"${sessionId}\",\"offset\": ${offset}},\"close\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @$file
offset=$((offset+chunkSize))
done
curl -X POST https://content.dropboxapi.com/2/files/upload_session/finish \
--header "Authorization: Bearer ${apiToken}" \
--header "Dropbox-API-Arg: {\"cursor\": {\"session_id\": \"${sessionId}\",\"offset\": ${totalFileSize}},\"commit\": {\"path\": \"${targetPath}/${localFile}\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}}" \
--header "Content-Type: application/octet-stream"
rm -rf ${chunkDir}
fi