-
Notifications
You must be signed in to change notification settings - Fork 14
/
shareLinkCreator
executable file
·244 lines (216 loc) · 7.54 KB
/
shareLinkCreator
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
#!/bin/bash
#
# Public Link Creator Version 1.0
#
# (c) Copyright 2013 Bjoern Schiessle <bjoern@schiessle.org>
#
# This program is free software released under the MIT License, for more details
# see LICENSE.txt or http://opensource.org/licenses/MIT
#
# Description:
#
# The program was developed for the Thunar file manager but it should also
# works with other file managers which provide similar possibilities to
# integrate shell scripts. For example I got some feedback that it also works
# nicely with Dolphin and Nautilus.
#
# This script can be integrated in the Thunar file manager as a "custom
# action". If you configure the "custom action" in Thunar, make sure to pass
# the paths of all selected files to the program using the "%F" parameter. The
# program expects the absolute path to the files. Once the custom action is
# configured you can execute the program from the right-click context menu. The
# program works for all file types and also for directories. Once the script
# gets executed it will first upload the files/directories to your Nextcloud and
# afterwards it will generate a public link to access them. The link will be
# copied directly to your clipboard and a dialog will inform you about the
# URL. If you uploaded a single file or directory than the file/directory will
# be created directly below your "uploadTarget" as defined below. If you
# selected multiple files, than the programm will group them together in a
# directory named with the current timestamp.
#
# Before you can use the program you need to adjust at least the "baseURL",
# "username" and "password" config parameter below. If you keep "username"
# and/or "password" empty a dialog will show up and ask for the credentials.
#
# Requirements:
#
# - curl
# - xclip
# - zenity
# load config parameters
. "$(dirname $0)/config"
# constants
TRUE=0
FALSE=1
webdavURL="$baseURL/remote.php/webdav"
url=$(echo "$webdavURL/$uploadTarget" | sed 's/\ /%20/g')
shareAPI="$baseURL/ocs/v1.php/apps/files_sharing/api/v1/shares"
curlOpts=""
if [ -n "$cacert" ]; then
curlOpts="$curlOpts --cacert $cacert"
fi
# check if base dir for file upload exists
baseDirExists() {
if curl -u "$username":"$password" --output /dev/null $curlOpts --silent --head --fail "$url"; then
return $FALSE
fi
return $TRUE
}
checkCredentials() {
curl -u "$username":"$password" $curlOpts --output /dev/null --silent --fail "$webdavURL"
if [ $? != 0 ]; then
msg="Username or password does not match"
if [ $usezenity -eq 1 ]; then
zenity --error --title="Nextcloud Public Link Creator" --text="$msg"
exit 1
else
echo "$msg"
exit 1
fi
fi
}
# upload files, first parameter will be the upload target from the second
# parameter on we have the list of files
uploadFiles() {
for filePath in "${@:2}"
do
basename=$(basename "$filePath")
basename=$(echo "$basename" | sed 's/\ /%20/g')
if [ -f "$filePath" ]; then
curl -u "$username":"$password" $curlOpts -T "$filePath" "$1/$basename"
count=$(($count+1))
if [ $usezenity -eq 1 ]; then
echo $(($count*100/$numOfFiles)) >&3;
fi
else
curl -u "$username":"$password" $curlOpts -X MKCOL "$1/$basename"
uploadDirectory "$1/$basename" "$filePath"
fi
done
return $TRUE
}
# upload a directory recursively, first parameter contains the upload target
# and the second parameter contains the path to the local directory
uploadDirectory() {
while read filePath; do
filePath=$(basename "$filePath")
urlencodedFilePath=$(echo "$filePath" | sed 's/\ /%20/g')
if [ -d "$2/$filePath" ]; then
curl -u "$username":"$password" $curlOpts -X MKCOL "$1/$urlencodedFilePath"
uploadDirectory "$1/$urlencodedFilePath" "$2/$filePath"
else
curl -u "$username":"$password" $curlOpts -T "$2/$filePath" "$1/$urlencodedFilePath"
count=$(($count+1))
if [ $usezenity -eq 1 ]; then
echo $(($count*100/$numOfFiles)) >&3;
fi
fi
done < <(find "$2" -mindepth 1 -maxdepth 1)
}
# create public link share, first parameter contains the path of the shared file/folder
createShare() {
result=$(curl -u "$username":"$password" $curlOpts --silent "$shareAPI" -d path="$1" -d shareType=3 -H "OCS-APIRequest: true")
shareLink=$(echo $result | sed -e 's/.*<url>\(.*\)<\/url>.*/\1/')
shareLink=$(echo $shareLink | sed 's/\&/\&/')
echo -n $shareLink | xclip -sel clip
return $TRUE
}
# if no password is set in the script we ask the user to enter them
askForPassword() {
ENTRY=`zenity --password --title="Nextcloud Public Link Creator"`
case $? in
0)
password=`echo $ENTRY | cut -d'|' -f1`
;;
1)
exit 0;;
-1)
exit 1;;
esac
}
# if no username/password is set in the script we ask the user to enter them
askForUserPassword() {
ENTRY=`zenity --password --username --title="Nextcloud Public Link Creator"`
case $? in
0)
username=`echo $ENTRY | cut -d'|' -f1`
password=`echo $ENTRY | cut -d'|' -f2`
;;
1)
exit 0;;
-1)
exit 1;;
esac
}
askForUserPasswordNonZenity() {
read -p "Username: " username
read -s -p "Password: " password
echo
if [ -z $password ] || [ -z $username ]; then
exit 1
fi
}
askForPasswordNonZenity() {
read -s -p "Password for $username: " password
echo
if [ -z $password ] || [ -z $username ]; then
exit 1
fi
}
if [ $# -lt 1 ]; then
msg="no file was selected!"
if [ $usezenity -eq 1 ]; then zenity --error --title="Nextcloud Public Link Creator" --text="$msg"
else echo "$msg"
fi
exit 1
fi
if [ -z "$password" ] && [ -z "$username" ]; then
# ask for both password and username
if [ $usezenity -eq 1 ]; then askForUserPassword
else askForUserPasswordNonZenity
fi
elif [ -z "$password" ]; then
# ask for password only
if [ $usezenity -eq 1 ]; then askForPassword
else askForPasswordNonZenity
fi
fi
checkCredentials
msg="Uploading files and generating a public link"
if [ $usenotify -eq 1 ]; then
notify-send -c transfer "Nextcloud Public Link Creator" "$msg"
elif [ $usezenity -eq 1 ]; then
exec 3> >(zenity --progress --title="Nextcloud Public Link Creator" --text=$msg --auto-kill --auto-close --percentage=0 --width=400)
else
echo $msg
fi
numOfFiles=$(find "$@" -type f | wc -l)
count=0
if baseDirExists; then
curl -u "$username":"$password" $curlOpts -X MKCOL "$url"
fi
# if we have more than one file selected we create a folder with
# the current timestamp
if [ $# -gt 1 ]; then
share=$(date +%s)
url="$url/$share"
curl -u "$username":"$password" $curlOpts -X MKCOL "$url"
elif [ $# -eq 1 ]; then
share=$(basename "$1")
else
# Errors for this are already shown above.
exit 1
fi
if uploadFiles $url "$@"; then
createShare "/$uploadTarget/$share"
fi
output="File uploaded successfully. Following public link was generated and copied to your clipboard: $shareLink"
if [ $usenotify -eq 1 ]; then
# Lazy hack for xfce4-notifyd cutting off long text
output="File uploaded successfully. Following public link was generated\nand copied to your clipboard:\n<a href='$shareLink'>$shareLink</a>"
notify-send -c transfer.complete "Nextcloud Public Link Creator" "$output"
elif [ $usezenity -eq 1 ]; then
zenity --info --title="Nextcloud Public Link Creator" --text="$output" --no-markup
else
echo $output
fi