-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudapp
executable file
·208 lines (164 loc) · 5.13 KB
/
cloudapp
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
#!/bin/bash
source ${XDG_CONFIG_HOME:-~/.config}/cloudapp-bash/config
# Requests to upload to CloudApp.
# Returns S3 params and a URL to POST to.
request_upload() {
curl -X GET --silent --digest --user "$username":"$password" --header "Accept: application/json" https://my.cl.ly/items/new
}
# Gets the extension of the file to upload.
get_file_extension() {
# Get the file's details
local filename
filename="$(basename -- "$1")"
local extension
extension="${filename##*.}"
echo "$extension"
}
# Move file to tmp so we can modify it and not touch the original.
move_to_tmp() {
local new_filepath
new_filepath="/tmp/$(date +%Y_%m_%d_%H%M%S).$2"
cp "$1" "$new_filepath"
echo "$new_filepath"
}
# Attempts to do basic conversion of video file to mp4
convert_video_file() {
local new_filepath
new_filepath="$1.mp4"
ffmpeg -loglevel panic -i "$1" "$new_filepath"
echo "$new_filepath"
}
# Uploads the file to S3
upload_file() {
local filename
filename="$(basename -- "$1")"
# Request to upload
local response
response=$(request_upload)
# Get the URL from the JSON, removing quotes
local url
url="$(echo "$response" | jq '.url' | sed 's/\"//g')"
# Get the params from the response, replacing ${filename} with the filename to upload
local params
params="$(echo "$response" | jq '.params' | sed 's|${filename}|'"$filename"'|g')"
# Use jq to convert the JSON array into CURL params (-F key=value)
local form_values
form_values="$(echo "$params" | jq -r '. | to_entries | .[] | "-F " + .key + "=" + .value + " "' | tr -d '\n')"
# Append the file to upload
form_values="${form_values}-F file=@${1}"
# Send to S3
local post_response
post_response="$(curl --silent -i -X POST $form_values $url)"
# Get the location of the file upload
local location
location="$(echo "$post_response" | grep -oP 'Location: \K.*' | tr -d '\r')"
echo "$location"
}
# Get the content URL from the uploaded file.
get_content_url() {
# Ping it, and get the url, removing quotes
local content_url
content_url="$(curl --silent -X GET --digest --user "$username":"$password" --header "Accept: application/json" "$location" | jq '.url' | sed 's/\"//g')"
echo $content_url
}
# Uploads the file to the S3 Bucket.
# It will perform checks on the filename and do different actions
# depending upon settings and the file type.
handle_file() {
# Get the extension
local extension
extension="$(get_file_extension "$1")"
# Move to tmp and rename
local new_filepath
new_filepath="$(move_to_tmp "${1}" "${extension}")"
# Check to convert video
if [ "$convert_video" == "1" ] ; then
# Check if this is a video
local test_file
test_file="$(file -i "$new_filepath" | grep video)"
if [ "$test_file" ] ; then
if [[ ! $extension =~ ^(MP4|mp4)$ ]] ; then
new_filepath="$(convert_video_file "$new_filepath")"
fi
fi
fi
# Upload the file and get the file URL
local location
location="$(upload_file "$new_filepath")"
local content_url
content_url="$(get_content_url "$location")"
# Copy to clipboard for use
echo "$content_url" | xclip -selection clipboard
# Check if we should notify
if [ "$notify" == "1" ] ; then
notify-send "CloudApp File Ready!" "It has been copied to your clipboard."
fi;
}
# Watches the folder specified for writes or moves
watch_directory() {
inotifywait --quiet --event close_write,moved_to -m "$watch_dir" |
while read -r directory events filename; do
handle_file "$watch_dir/$filename"
done
}
# Gets the most recent uploads to CloudApp
get_recent() {
curl -X GET --silent --digest --user "$username":"$password" --header "Accept: application/json" https://my.cl.ly/items
}
# Creates a table from the CSV data
create_recent_table() {
local csv
csv="Title,URL,Created\n${1}"
# Use columns
echo -e "${csv}" | column -s ',' -t
}
# Processes the recent results to a table
process_recent() {
# Get recent uploads
local recent
recent=$(get_recent)
# Process the JSON into CSV format
local process
process="$(echo "${recent}" | jq -r '.[] | "\n\"\(.name)\",\(.url),\(.created_at)"')"
echo "${process}"
}
# Displays the recent entries as a table
display_recent_table() {
# Process the recent entries into CSV format
local process
process=$(process_recent)
# Generate a table from the CSV data
local table
table="$(create_recent_table "${process}")"
echo "${table}"
}
# Copies the most recent upload to clipboard
copy_most_recent() {
# Get recent uploads
local recent
recent=$(get_recent)
# Process the JSON into CSV format
local process
process="$(echo "${recent}" | jq -r '.[0] | .url')"
# Copy to clipboard for use
echo "$process" | xclip -selection clipboard
echo "Copied: $process"
}
# Start!
case "$1" in
watch)
watch_directory
;;
recent)
display_recent_table
;;
copy-recent)
copy_most_recent
;;
*)
echo $"Usage: $0 {watch|recent|copy-recent}"
echo " watch - Watches files in the configured directory to upload"
echo " recent - Lists your most recent uploads in table format"
echo " copy-recent - Copy the most recent upload to the clipboard"
;;
esac