-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdropbox
More file actions
executable file
·167 lines (147 loc) · 4.25 KB
/
dropbox
File metadata and controls
executable file
·167 lines (147 loc) · 4.25 KB
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
#!/bin/bash
#The MIT License (MIT)
#
#Copyright (c) 2014 Tommaso Leonardi, tleonardi@gmail.com
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#
# Configure default values
DEFEMAIL="tleonardi@gmail.com"
URL="http://www.ebi.ac.uk/~tl344/dropbox"
FOLDER="/homes/tl344/public_html/dropbox"
usage()
{
cat <<EOF
Usage: $(basename "$0") [-s] [-e <email>] [-h] [-u <url>] [-f <folder>] filename
This script copies a file to an http-accessible folder, provides the URL
pointing to it (and optionally a shortened URL) and optionally sends the
the link via email.
If the filename is not specified, the input is read from stdin and saved
in the html folder
Options:
-s Shorten the URL using Google APIs
-e <email> Email address to send the link to.
-e . sends the email to $DEFEMAIL
-h Print this help screen
-u <URL> Base URL of the http folder
Default: $URL
-f <folder> Save the file in the specified http-accessible folder.
Default: $FOLDER
-o <name> Target file name
Default: source filename or date-time.html if reading from stdin
EOF
}
ARGS=$(getopt -o "se:hu:f:no:" -n "dropbox.sh" -- "$@")
if [ $? -ne 0 ]; then
usage
exit 1;
fi
eval set -- "$ARGS"
# Now go through all the options
while true; do
case "$1" in
-s)
SHORT=1
shift 1
;;
-e)
if [[ -n "$2" ]]; then
EMAIL=$2
shift 2
fi
;;
-u)
if [[ -n "$2" ]]; then
URL=$2
shift 2
fi
;;
-f)
if [[ -n "$2" ]]; then
FOLDER=$2
shift 2
fi
;;
-h)
usage
exit 1
;;
-o)
if [[ -n "$2" ]]; then
OUT=$2
shift 2
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
--) shift ; break;;
esac
done
if [[ -n "$1" ]]; then
# FILE provided
FILE=$1
if [[ ! -f $FILE ]]; then
echo "$FILE: no such file";
exit 1;
fi
#echo "Filename set: $1" >&2
exec < $FILE
if [[ -z "$OUT" ]]; then
OUT=$(basename $FILE)
fi
else
# From STDIN
#echo "Filename not set: stdin" >&2
if [[ -z "$OUT" ]]; then
OUT=$(date +"%m_%d_%Y-%H:%M:%S.html")
fi
fi
# This routine checks if the $OUT file already exists
# If it does, appends -1, -2... to the end of the file
if [[ -f $FOLDER/$OUT ]]; then
EXT="${OUT##*.}"
# If $OUT doesn't have an extension, force EXT to an empty string
if [[ "$OUT" = "$EXT" ]]; then
EXT=""
else
EXT=.$EXT
fi
BASEFILE=$(basename $OUT $EXT)
i=1
while [[ -e ${FOLDER}/${BASEFILE}-${i}${EXT} ]] ; do
let i++
done
OUT=${BASEFILE}-${i}${EXT}
fi
cat <&0 > $FOLDER/$OUT
echo $URL/$OUT
EMAILTEXT="URL: $URL/$OUT"
if [[ -n $SHORT ]]; then
SHORTURL=$(curl --silent https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{"longUrl":'"\"$URL/$OUT\""'}' | grep -Po '"id":.*?[^\\]",' | perl -pe 's/"id"://; s/^[ ]"//; s/",$//' )
echo $SHORTURL
EMAILTEXT="Long URL: $URL/$OUT \nShort URL: $SHORTURL"
fi
if [[ $EMAIL == '.' ]];then
EMAIL=$DEFEMAIL;
fi
if [[ -n $EMAIL ]];then
echo -e "$EMAILTEXT" | mail -s "Dropbox.sh: link to \"$OUT\"" $EMAIL
fi