This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnight-uploader
executable file
·386 lines (328 loc) · 11.3 KB
/
night-uploader
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Night Updater
# https://github.com/ZacharyDuBois/Night-Uploader
version="v0.4.2"
##
# Variables
##
# All variables ending with Loc are full paths to directories or files. The also must contain a trailing slash if they are a direcotry.
loc=""
s3bucket=""
endLoc=""
templateLoc=""
temporaryDirLoc=""
failTime=3600
# The following is for AWS Elastic Transcoder. If you do not plan to use it, make sure the workflow is not loading the transcodeMedia function.
etcMP4PresetID=""
etcMP3PresetID=""
etcPipelineID=""
# If you don't want to use Pushover, leave pushoverAppKey and/or pushoverUserKey set to "".
pushoverAppKey=""
pushoverUserKey=""
pushoverTitle="Night Uploader"
pushoverURL=""
pushoverURLTitle=""
# File extentions and formats below.
formatVideo=".*\.(mp4|mov|mkv|m4v|ogv|avi)$"
formatAudio=".*\.(mp3|acc|ogg|wav|m4a|ogg|oga|opus)$"
formatCode=".*\.(html|htm|xml|py|c|http|log|rb|cpp|sh|bash)$"
formatPDF=".*\.(pdf)$"
formatImage=".*\.(png|jpg|jpeg|tiff|gif|svg|ico|pxm)$"
formatArchive=".*\.(zip|tar|gz)$"
# The next 7 are not recommend to edit but you may if you'd like.
indexFile="index.html"
indexFileLoc="$temporaryDirLoc""$indexFile"
listingFile="tmp"
listingFileLoc="$temporaryDirLoc""$listingFile"
newUploadsFile="New-Uploads.txt"
newUploadsFileLoc="$temporaryDirLoc""$newUploadsFile"
curTime=$(date '+%F %T')
# Do not edit past this line
runType=$1
##
# Message Types
##
fail="[$(tput setaf 1) FAIL $(tput sgr0)]"
ok="[$(tput setaf 2) OK $(tput sgr0)]"
running="[$(tput setaf 3) ** $(tput sgr0)]"
notice="[$(tput setaf 3)NOTICE$(tput sgr0)]"
warn="[$(tput setaf 3) WARN $(tput sgr0)]"
info="[$(tput setaf 6) INFO $(tput sgr0)]"
finish="[$(tput setaf 4) DONE $(tput sgr0)]"
##
# Startup Checks
##
# Check for root
if [[ "$(id -u)" == 0 ]]
then
echo "$fail Do not run this script as root."
exit 1
fi
# Check for AWS CLI
which aws > /dev/null
checkForAWS=$?
if [[ "$checkForAWS" != 0 ]]
then
echo "$fail You need AWS CLI to run this script."
exit 1
fi
# Check for variables set correctly
if [[ "$loc" == "" ]] || [[ "$s3bucket" == "" ]] || [[ "$endLoc" == "" ]] || [[ "$templateLoc" == "" ]] || [[ "$temporaryDirLoc" == "" ]] || [[ "$indexFile" == "" ]] || [[ "$indexFileLoc" == "" ]] || [[ "$listingFile" == "" ]] || [[ "$listingFileLoc" == "" ]] || [[ "$newUploadsFile" == "" ]] || [[ "$newUploadsFileLoc" == "" ]] || [[ "$curTime" == "" ]]
then
echo "$fail You have not set all the required variables in the variables block. Please edit them with the correct details."
exit 1
fi
# Check if locs and files exist.
if ! [[ -d "$loc" ]] || ! [[ -d "$endLoc" ]] || ! [[ -d "$templateLoc" ]] || ! [[ -d "$temporaryDirLoc" ]] || ! [[ -r "$templateLoc""index/""aftercontent.html" ]] || ! [[ -r "$templateLoc""index/""footer.html" ]] || ! [[ -r "$templateLoc""index/""head.html" ]]
then
echo "$fail One or more of the folders and/or files does not exist."
exit 1
fi
##
# Start
##
pushoverSend() {
message=$1
if [[ "$pushoverAppKey" != "" ]] || [[ "$pushoverUserKey" != "" ]]
then
curl -s --form-string "token=$pushoverAppKey" --form-string "user=$pushoverUserKey" --form-string "url=$pushoverURL" --form-string "url_title=$pushoverURLTitle" --form-string "priority=0" --form-string "title=$pushoverTitle" --form-string "message=$message" https://api.pushover.net/1/messages.json > /dev/null
pushoverStatus=$?
if [[ "$pushoverStatus" == 0 ]]
then
echo "$info Pushover message sent successfully: $message"
else
echo "$warn Pushover failed to send message: $message"
fi
else
echo "$info No Pushover key set to send message: $message"
fi
}
makeNewFilesList() {
# Make a new files list
echo "$running Generating new files list."
ls $loc > $newUploadsFileLoc
echo "Last Updated: $curTime" >> $newUploadsFileLoc
}
uploadFiles() {
# Upload
echo "$running Starting upload to s3://"$s3bucket"/"
pushoverSend "Starting to upload files to s3://"$s3bucket"/."
aws s3 cp $loc s3://"$s3bucket"/ --recursive --exclude ".*" --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers
}
makeIndex() {
# Generate Index
echo "$running Generating index file."
pushoverSend "Generating index file."
numberOfFiles=0
aws s3 ls s3://"$s3bucket"/ | grep -v PRE | awk -F' ' '{ print $4 }' > $listingFileLoc
sort -f $listingFileLoc -o $listingFileLoc
cat "$templateLoc""index/""head.html" > $indexFileLoc
echo " <ul>" >> $indexFileLoc
while read listItem
do
numberOfFiles=$(($numberOfFiles+1))
# Set the icon.
shopt -s nocasematch
if [[ "$listItem" =~ $formatVideo ]]
then
itemIcon="fa-file-video-o"
elif [[ "$listItem" =~ $formatAudio ]]
then
itemIcon="fa-file-audio-o"
elif [[ "$listItem" =~ $formatCode ]]
then
itemIcon="fa-file-code-o"
elif [[ "$listItem" =~ $formatPDF ]]
then
itemIcon="fa-file-pdf-o"
elif [[ "$listItem" =~ $formatImage ]]
then
itemIcon="fa-file-image-o"
elif [[ "$listItem" =~ $formatArchive ]]
then
itemIcon="fa-file-archive-o"
else
itemIcon="fa-file-o"
fi
echo " <li><a href=\"/"$listItem"\"><i class=\"fa "$itemIcon"\"></i> "$listItem"</a></li>" >> $indexFileLoc
done < $listingFileLoc
echo " </ul>" >> $indexFileLoc
cat "$templateLoc""index/""aftercontent.html" >> $indexFileLoc
echo " <p><code>"$numberOfFiles" Items</code></p>" >> $indexFileLoc
echo " <p>Index Last Updated: <code>"$curTime"</code></p>" >> $indexFileLoc
echo " <p>Powered by <a href=\"https://we.destroy.tokyo/Night-Uploader\">Night Uploader</a> <code>"$version"</code></p>" >> $indexFileLoc
cat "$templateLoc""index/""footer.html" >> $indexFileLoc
# Upload Index
echo "$running Uploading index."
aws s3 cp $indexFileLoc s3://"$s3bucket"/"$indexFile" --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers
}
cleanUp() {
echo "$running Cleaning up."
if [[ "$temporaryDirLoc" != "" ]]
then
rm "$temporaryDirLoc"*
fi
}
moveFiles() {
# Move them to the done loc.
echo "$running Moving files to $endLoc..."
if [[ "$loc" != "" ]]
then
mv "$loc"* $endLoc
fi
}
transcodeMedia() {
# Transcode media on the CDN that does not have a copy in mp4 and mp3.
if [[ "$etcPipelineID" == "" ]] || [[ "$etcMP3PresetID" == "" ]] || [[ "$etcMP4PresetID" == "" ]]
then
echo "$fail The ETC variables are not setup corrctly."
exit 1
fi
echo "$running Finding media that needs to be transcoded"
pushoverSend "Transcoding required items."
aws s3 ls s3://"$s3bucket"/ | grep -v PRE | awk -F' ' '{ print $4 }' > $listingFileLoc
filesList=$(cat $listingFileLoc)
etcQueued=0
mp3=".mp3"
mp4=".mp4"
while read mediaItem
do
# Remove the extention - TODO: Fix the fact that you cannot have files with dots
noExtItem=$(echo $mediaItem | awk -F'.' '{ print $1 }')
shopt -s nocasematch
if [[ "$mediaItem" =~ $formatAudio ]]
then
# Find all audio files
if [[ $(echo $filesList | grep $noExtItem$mp3) == "" ]]
then
# If there is no matches for the mp3 version, make a conversion.
outputName=$noExtItem$mp3
aws elastictranscoder create-job --pipeline-id $etcPipelineID --input \{\"Key\":\"$mediaItem\"\} --outputs \{\"Key\":\"$outputName\",\"PresetId\":\"$etcMP3PresetID\"\} > /dev/null
etcQueued=$(($etcQueued+1))
echo "$ok Created MP3 job for $mediaItem to $outputName"
fi
fi
if [[ "$mediaItem" =~ $formatVideo ]]
then
# Find all the video files.
if [[ $(echo $filesList | grep $noExtItem$mp4) == "" ]]
then
# If there is no matches for the mp4 version, make a conversion.
outputName=$noExtItem$mp4
aws elastictranscoder create-job --pipeline-id $etcPipelineID --input \{\"Key\":\"$mediaItem\"\} --outputs \{\"Key\":\"$outputName\",\"PresetId\":\"$etcMP4PresetID\"\} > /dev/null
etcQueued=$(($etcQueued+1))
echo "$ok Created MP4 job for $mediaItem to $outputName"
fi
if [[ $(echo $filesList | grep $noExtItem$mp3) == "" ]]
then
# If there is no matches for the mp3 version, make a conversion.
outputName=$noExtItem$mp3
aws elastictranscoder create-job --pipeline-id $etcPipelineID --input \{\"Key\":\"$mediaItem\"\} --outputs \{\"Key\":\"$outputName\",\"PresetId\":\"$etcMP3PresetID\"\} > /dev/null
etcQueued=$(($etcQueued+1))
echo "$ok Created MP3 job for $mediaItem to $outputName"
fi
fi
done < $listingFileLoc
echo "$info Queued $etcQueued jobs."
pushoverSend "Queued $etcQueued transcoding jobs."
# Wait for all of them to compleate.
etcWaitCount=0
while aws elastictranscoder list-jobs-by-status --status Submitted | grep $etcPipelineID > /dev/null
do
if [[ "$etcWaitCount" == 0 ]]
then
echo "$running Waiting for all jobs to be progressing."
fi
etcWaitCount=$(($etcWaitCount+1))
sleep 5
done
echo "$info Wait looped $etcWaitCount times."
etcWaitCount=0
while aws elastictranscoder list-jobs-by-status --status Progressing | grep $etcPipelineID > /dev/null
do
if [[ "$etcWaitCount" == 0 ]]
then
echo "$running Waiting for all jobs to be compleate."
fi
etcWaitCount=$(($etcWaitCount+1))
sleep 5
done
echo "$info Wait looped $etcWaitCount times."
echo "$ok Transcoding done."
pushoverSend "Finished transcoding files."
}
##
# AWS Check
##
# Check if AWS is running already
retryCount=0
while ps aux | grep -v grep | grep "aws" > /dev/null
do
if [[ "$retryCount" == 0 ]]
then
echo "$warn AWS is currently running. Waiting for AWS to finish to avoid issues."
pushoverSend "There is a process matching AWS already running on your system. To avoid conflict, this script will wait for it to finish."
fi
if [[ "$retryCount" == $failTime ]]
then
echo "$fail Night Uploader failed to run due to an active AWS process taking longer than $failTime seconds."
pushoverSend "Night Uploader is canceling because the current running AWS process took longer than $failTime seconds."
exit 1
fi
echo -n "."
retryCount=$((retryCount+1))
sleep 1
done
if [[ "$retryCount" != 0 ]]
then
echo
fi
##
# Run Types and Workflows
##
echo "$notice Starting workflow ID $runType."
if [[ "$runType" == "" ]] || [[ "$runType" == "1" ]]
then
runType=1
if [[ "$(ls $loc)" == "" ]]
then
# Standard run w/o changes. transcode media, make media pages, index.
# No files to upload, reindex, see if anything needs transcoding, or needs media pages. Then, finish.
echo "$ok No files found for upload."
pushoverSend "No files found for upload. Reindexing."
transcodeMedia
makeIndex
cleanUp
else
# New files found. Make listing of new, upload, move, transcode, media player pages, and index.
echo "$ok Files found, continuing with workflow."
pushoverSend "New files are being prepared for upload."
makeNewFilesList
uploadFiles
cleanUp
moveFiles
transcodeMedia
makeIndex
cleanUp
fi
elif [[ "$runType" == "2" ]]
then
runType=2
# Index run transcode media, make media pages, index.
echo "$ok Running index workflow."
pushoverSend "Running index workflow."
transcodeMedia
makeIndex
cleanUp
else
echo "$fail Unknown workflow ID $runType."
exit 1
fi
echo "$ok Finished workflow ID $runType."
pushoverSend "Finished workflow ID $runType."
##
# Done
##
echo "$finish Done"
exit 0