Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
1033 lines (819 loc) · 31.4 KB
/
Copy pathindex.js
File metadata and controls
1033 lines (819 loc) · 31.4 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const AWS = require('aws-sdk')
const fs = require('fs')
const compression = require('compression')
const Jimp = require('jimp')
const express = require('express')
const fileUpload = require('express-fileupload')
const crypto = require('crypto')
const mustache = require('mustache')
var cookieParser = require('cookie-parser')
const app = express()
const {
Sequelize,
Model,
DataTypes
} = require('sequelize')
// Initialize Sequelize with local database
// You can modify this to work with networked databases
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'database.sqlite',
logging: false
})
let rawdata = fs.readFileSync('s3.json') // Read s3.json for S3 settings
let s3Data = JSON.parse(rawdata)
// The name of the bucket, and other information will be extracted and S3 api will be setup
const BUCKET_NAME = s3Data.bucket // set the Bucket name
var ep = new AWS.Endpoint(s3Data.endpoint) // Setup the endpoint
const s3 = new AWS.S3({ // Using the endpoint, access key, and secret the S3 will be created and initialized
endpoint: ep,
accessKeyId: s3Data.accessKeyId,
secretAccessKey: s3Data.secretAccessKey
})
/*******************************************
* Sequelize database setup
*******************************************/
// Files stores information about each file stored on the server
class files extends Model {}
files.init({
filename: DataTypes.STRING,
fileid: DataTypes.STRING,
location: DataTypes.STRING,
lastedit: DataTypes.TIME,
user: DataTypes.STRING,
preview: DataTypes.BOOLEAN
}, {
sequelize,
modelName: 'files'
})
// This database contains a log of IP addresses that have connected to the server
class ipAddresses extends Model {}
ipAddresses.init({
address: DataTypes.STRING,
cookie: DataTypes.STRING,
currentUser: DataTypes.STRING,
time: DataTypes.TIME,
path: DataTypes.STRING,
}, {
sequelize,
modelName: 'ipAddresses'
})
// Folders stores the folders for each user that may contain files
class folders extends Model {}
folders.init({
foldername: DataTypes.STRING,
path: DataTypes.STRING,
fullpath: DataTypes.STRING,
lastedit: DataTypes.TIME,
user: DataTypes.STRING
}, {
sequelize,
modelName: 'folders'
})
// Each user's information will be stored here. Because of how it is designed,
// each user can only be logged into one browser at a time
class users extends Model {}
users.init({
username: DataTypes.STRING,
cookie: DataTypes.STRING,
password: DataTypes.STRING
}, {
sequelize,
modelName: 'users'
})
sequelize.sync() // Sync the database
/*******************************************
* Useful functions
*******************************************/
function sha512(plaintext) {
/**
* Used to generate Sha512 hashes for passwords and such
*/
let hash = crypto.createHash('sha512')
// Generate hash function
data = hash.update(plaintext, 'utf-8')
// pass plaintext to hash function
return data.digest('hex') // generate the hash and return it
}
function isIdUnique(table, params) {
/**
* isIdUnique: Uses a table and params to see if the parameters are unique to the table
*
* table: sequelize table variable
* params: sequelize parameters used to query a table
*/
return table
.count({ // Run count query
where: params
})
.then(count => { // after the query runs,
if (count > 0) { // If there aren't any entries in the query, return false
return false
}
return true // If there are entries, return true
})
}
function generateID(size) {
/**
* generates a random number id using the size parameter passed to the function
*
* size: byte size of the random id that will be returned
*/
return crypto.randomBytes(size).toString('hex') // generate a hex id with the length of size that was passed to the function
}
function assemblePath(pathArray) {
/**
* assemblePath: Assembles an array with elements of a path into a string
*
* pathArray: An array of elements in a path
*/
let path = '' // Stores the output
for (i in pathArray) { // iterate through the array
path += '/' + pathArray[i]; // append next array element with / in front of it
}
if (path === '') { // if the path is empty, set it to /
path = '/'
}
return path // return the finished path
}
async function update(table, params, value) {
/**
* update: updates an element in a table
*
* table: sequelize table variable
* params: sequelize variable that stores parameters for a WHERE statement
* value: the new value
*/
// Update an element in a table
table.update(value, {
where: params
})
}
function previous(path) {
/**
* previous: generates a path at the previous path, and removes the preamble function
*
* path: string for a path
*/
path = path.split('/') // generates path array
path.shift() // remove preamble
path.pop() // remove end of path
return assemblePath(path) // reassemble path and return
}
function retrieve(table, params) {
/**
* retrieve: retrieves data from a table based on some parameters. keep in mind this only queries the first element it can find
* table: sequelize table variable
* params: WHERE parameters to find some data in the table
*/
return table // Run the query
.findOne({
where: params
})
.then(function (item) {
return item
})
}
function previewFilter(filename) {
/**
* previewFilter: generates preview http path for file preview in Storage Manager
*
* filename: path to file
*/
// extract file extension
let extension = filename.split('.')
extension = extension[extension.length - 1]
// Array of file types and their extensions
let extensionTypes = {
images: ['png', 'jpg', 'jpeg']
}
// Check if preview is possible
if (extensionTypes.images.includes(extension)) {
return filename // Return the original file
} else {
return '/images/document.png' // Return default "document.png"
}
}
app.use(fileUpload()) // Setup fileUpload function when HTTP connection is recieved
app.use(cookieParser()) // Setup cookieParser function when HTTP connection is recieved
app.use(compression()) // Setup compression function when HTTP connection is recieved
app.use(express.json()) // Setup express.json function when HTTP connection is recieved
app.use((req, res, next) => {
let params = { // generate default values in params
address: null,
cookie: null,
time: Date.now(),
path: req.path
};
if (req.ip) {
params.address = req.ip // extract browser ip
}
if (req.cookies.browserid) { // extract browser cookie
params.cookie = req.cookies.browserid;
}
ipAddresses.create(params); // Append ip info to ipAddresses table
next() // run next function
})
app.use(async function (req, res, next) {
/**
* Generates new cookie if the browser doesn't already have one
*/
let cookie = req.cookies.browserid
if (cookie === undefined) { // If the cookie is not yet defined
// Generate new id
let id = generateID(64);
// Get response ready with new cookie
res.cookie('browserid', id, {
maxAge: 2592000,
httpOnly: true
})
console.log('cookie created successfully') // Log that a new cookie was created
res.redirect("/") // redirect the user to the index with their new cookie
} else {
next() // run next function if a cookie wasn't created
}
})
app.get('/', async function (req, res, next) {
/**
* Return index file
*/
// Extract file from index.mustache template
let template = await fs.readFileSync(
__dirname + '/public/mustache/index.mustache', {
encoding: 'utf-8'
}
)
// Generate a file count
let fileCount = (await files.findAll()).length
// Return render from template
let data = mustache.render(template, {
fileCount: fileCount
})
res.send(data) // Send the index page
})
app.post('/upload*', async function (req, res, next) {
/**
* Run when a user sends an upload post
*/
// extract the path to the file
let path = await req.path.split('/')
path.shift()
path.shift()
path = assemblePath(path)
// Change path to / if it's empty
if (path === '') {
path = '/'
}
// Using the cookie, query the user from the user's table
let user = await retrieve(users, {
cookie: req.cookies.browserid
})
if (req.files && req.files !== null) { // Check if file's exist in the post
// Determine whether the file is unique
const uniqueFile = await isIdUnique(files, {
filename: req.files.myfile.name,
location: path,
user: user.dataValues.username
})
// Determine whether the folder and path exists
const uniqueFolder = await isIdUnique(folders, {
fullpath: path,
user: user.dataValues.username
})
// If all is valid, continue to upload the file
if ((uniqueFile && !uniqueFolder) || (['', '/'].includes(path) && uniqueFile)) {
// extract the filename of the file
let filename = req.files.myfile.name
// generate a unique key id for the new file with length of 128 bytes
let key = await generateID(128)
// As long as the id already exists, generate a new one
while ((await isIdUnique(files, {fileid: key, user: user.dataValues.username}))) {
key = await generateID(128)
}
// generate the parameters to upload the file to the S3
let params = {
Bucket: BUCKET_NAME,
Key: key, // File name you want to save as in S3 (this is different than the actual filename)
Body: req.files.myfile.data
}
// Uploading files to the bucket
s3.upload(params, function (err, data) {
// Throw an error if it occured
if (err) {
throw err
}
// Since the file was correctly uploaded, create a new entry in the files table
files.create({
filename: filename,
fileid: key,
location: path,
lastedit: Date.now(),
user: user.dataValues.username,
preview: false
})
// extract the file's extension
let extension = filename.split('.');
extension = extension[extension.length - 1]
// If the file is an image file, a preview can be created to help make it more easily noticed in the Storage Manager
if (['png', 'jpg', 'jpeg'].includes(extension)) {
Jimp.read(req.files.myfile.data).then(image => { // Generate a preview file using Jimp
image // generate a file with the following parameters
// TODO: Change output to WEBP
.scaleToFit(512, 512)
.quality(50)
.getBuffer(Jimp.MIME_JPEG, (err, res) => {
// Given that the file is sucessfully created generate new parameters with "-preview appended to the key"
let params = {
Bucket: BUCKET_NAME,
Key: key + '-preview', // File name you want to save as in S3
Body: res
}
// Uploading files to the bucket
s3.upload(params, async function (err, data) {
if (err) {
throw err
}
console.log('Preview Uploaded')
// retrieve the file from the table
let file = await files.findOne({
where: {
fileid: key
}
})
// Update the file such that preview is true
if (file !== null) {
console.log(file)
file.update({
preview: true
})
}
})
})
})
}
res.redirect(`/download${path}`) // redirect to the original file location
console.log(`File uploaded successfully. ${data.Location}`)
})
} else {
console.log('Error Uploading file')
res.send(`Error uploading file`)
}
} else if (req.body.filedata && req.body.filedata !== null) { // If the filename was not given in the POST but, filedata was in the body of the POST
// Check if the file exists
if (!(await isIdUnique(files, {filename: req.body.filename, location: path}))) { // Make sure it exists
// retrieve the original file
let file = await retrieve(files, {
filename: req.body.filename,
location: path
})
// Generate bucket parameters
let params = {
Bucket: BUCKET_NAME,
Key: file.dataValues.fileid, // File name you want to save as in S3
Body: req.body.filedata
}
// Uploading new data to S3 Bucket
s3.upload(params, function (err, data) {
if (err) {
throw err
}
})
}
} else {
// The last possible POST would be a folder request
// extract the folder from the request
let folder = req.body.folder
if (await isIdUnique(folders, {user: user.dataValues.username, fullpath: path + folder})) {
// Create a new folder entry in the folders table
folders.create({
foldername: folder,
path: path,
fullpath: path + folder,
lastedit: Date.now(),
user: user.dataValues.username
})
}
res.redirect(`/download${path}`)
}
})
app.get('/delete*', async function (req, res, next) {
/**
* Used for delete requests of files
* TODO: Implement folders
*/
// Extract the path from the http path
let path = req.path.split('/delete')
path.shift()
path.pop()
path = assemblePath(path)
// Default path to / if it's at root
if (path === '') {
path = '/'
}
// generate proper filename without spacing representations and split from path
let filename = req.path.split("%20").join(" ").split('/')
filename = filename[filename.length-1] // extract filename from array
// retrieve the user from the browser cookie
let user = await retrieve(users, {
cookie: req.cookies.browserid
})
// retrieve all files that the user owns in the database
let fileData = await files.findAll({
where: {
filename: filename,
location: path,
user: user.dataValues.username
},
// Order by lastedit and descending
order: [
['lastedit', 'DESC']
]
})
.then(function (file) {
return file
})
// If a file exists, delete it
if (fileData.length > 0) {
files.destroy({
where: {
id: fileData[0].id,
}
})
res.redirect("/download" + path)
} else {
res.send('File could not be deleted')
}
})
app.get('/download*', async function (req, res, next) {
/**
* Used to display Storage Manager and to download files
*/
// Retrieve the user from their cookie
let user = await retrieve(users, {
cookie: req.cookies.browserid
})
// If the user exists continue
if (user !== null) {
// retrieve the path from the request
let filename = req.path
// Replace representations with space and split the full path, then extract the filename
filename = filename.split("%20").join(" ").split('/')
filename = filename[filename.length - 1]
// query the file from the files database
let file = await retrieve(files, {
filename: filename
})
// retrieve the path from the request and split it into an array
let fullpath = req.path.split('/')
fullpath.shift() // remove the first two locations
fullpath.shift()
// reassemble the path
fullpath = assemblePath(fullpath)
// query the folder from the database
let folder = await retrieve(folders, {
fullpath: fullpath,
user: user.dataValues.username
})
// If the file exists
if (file !== null) {
console.log('Trying to download file', file.dataValues.filename)
// Setup S3 parameters
let params = {
Bucket: BUCKET_NAME,
Key: file.dataValues.fileid
}
// attach filename to the response
res.attachment(file.dataValues.filename)
// Create filestream from S3 request
let fileStream = await s3.getObject(params).createReadStream()
// stream the file to the response
fileStream.pipe(res)
} else if (folder !== null || ['', '/'].includes(fullpath)) {
// If the file does not exist, generate Storage Manager page
let buttons = '' // stores the buttons for the page
let fileData = await files // retrieve all files in the current path
.findAll({
where: {
location: fullpath,
user: user.dataValues.username
},
order: [
// Order each file by lastedit and descending
['lastedit', 'DESC']
]
})
.then(function (file) {
return file
})
let folderData = await folders // retrieve all folders within the current path
.findAll({
where: {
path: fullpath,
user: user.dataValues.username
},
order: [
// Order each file by lastedit and descending
['lastedit', 'DESC']
]
})
.then(function (folder) {
return folder
})
// if the fullpath is / change path to / otherwise copy full path to path
if (fullpath === '/') {
path = '/'
} else {
path = fullpath
}
// for each folder
for (i in folderData) {
// generate a new button
buttons += `<button id="${folderData[i].dataValues.foldername}" class="folder" onclick="window.location.href='/download${path}${folderData[i].dataValues.foldername}';">
<img class="file-preview lazy" data-src='/images/folder.png'>
${folderData[i].dataValues.foldername}
</button>`
}
// for each file
for (i in fileData) {
// Generate preview for the file
let src = previewFilter(
'/preview' + path + fileData[i].dataValues.filename
)
// generate full file path and filename
let fullfile = path + fileData[i].dataValues.filename
let filename = fileData[i].dataValues.filename
// generate button for the file
buttons += `<button class="file" id="${filename}" onclick="select('${filename}', '${fullfile}');">
<img class="file-preview lazy" data-src='${src}'>
${filename}
</button>`
}
// extract template from storagemanager.mustache
let template = await fs.readFileSync(
__dirname + '/public/mustache/storagemanager.mustache', {
encoding: 'utf-8'
}
)
// generate page from template
let data = mustache.render(template, {
files: buttons,
path: path,
previous: '/download' + previous(fullpath)
})
// send the template
res.send(data)
} else {
// TODO: GENERATE ERROR PAGE
// Send "file not found" if no other options work
res.send('File not found')
}
} else {
// if the user doesn't exist, redirect to /login
res.redirect('/login')
}
})
app.get('/css/*.css', (req, res) => {
/**
* When a css file is requested, /public/css is searched
*/
res.sendFile(__dirname + `/public/css/${req.path.split('/')[2]}`)
console.log(
`${req.connection.remoteAddress}:/public/css/${req.path.split('/')[2]}`
)
})
app.get('/js/*.js', (req, res) => {
/**
* When a js file is requested, /public/js is searched
*/
res.sendFile(__dirname + `/public/js/${req.path.split('/')[2]}`)
console.log(
`${req.connection.remoteAddress}:/public/js/${req.path.split('/')[2]}`
)
})
app.get('/images/*', (req, res) => {
/**
* When an image file is requested, /public/images is searched
*/
let filename = req.path
.split('/')[req.path.split('/').length - 1].replace(/%20/g, ' ')
res.sendFile(__dirname + `/public/images/${filename}`)
console.log(
`${req.connection.remoteAddress}:/public/css/${req.path.split('/')[1]}`
)
})
app.get('/editor*', async function (req, res) {
/**
* used to generate an editor for a file
*/
// Extract the filename of the file that will be edited
let filename = req.path.split('/')[req.path.split('/').length - 1]
// Generate the full path to the file
let fullpath = req.path.split('/')
fullpath.shift()
fullpath.shift()
fullpath = assemblePath(fullpath) // Reassemble the fullpath
// Remove the file from the fullpath and store it in path
let path = previous(fullpath)
// Extract the extension
let extension = filename.split('.')[filename.split('.').length - 1]
// Extract the template from editor.mustache
let template = await fs.readFileSync(
__dirname + '/public/mustache/editor.mustache', {
encoding: 'utf-8'
}
)
// List of filetypes and their valid extensions
let filetypes = {
images: ['jpg', 'jpeg', 'png', 'gif'],
text: ['txt', 'text']
}
let editorData = '' // stores the file's data that will be added to the editor
// If the data does not exist
if (!isIdUnique(files, {filename: filename, location: path})) {
editorData = '<p> Error: File Not Found </p>'
} else if (filetypes.images.includes(extension)) { // if the file is an image, set the editor to an image
editorData = `<img src='${'/download/' + filename}' href='${'/download/' + filename}''>`
} else if (filetypes.text.includes(extension)) { // if the file is a text file
// retrieve the file info from the database
let file = await retrieve(files, {
filename: filename,
location: path
})
// generate S3 request
let params = {
Bucket: BUCKET_NAME,
Key: file.dataValues.fileid
}
// Retrieve the data of the file
let fileData = await s3.getObject(params).promise()
// place the data into the editor
editorData = `<textarea class="textbox" id="textbox">${fileData.Body.toString()}</textarea><button class="submit" id="submit" onclick="savetxt();">Save</button>`
} else {
// Tell the user that this filetype is not yet supported
editorData = '<p>Not Yet Supported</p>'
}
// Generate a response from the template
let data = await mustache.render(template, {
editor: editorData,
file: filename
})
// Send response
res.send(data)
})
app.get('/preview*', async (req, res) => {
/**
* Generates a preview of the file when requested
*/
// if this is a preview page
if (req.path.split('/')[1] === 'preview-page') {
// Extract filename, extension and full path
let filename = req.path.split('/')[req.path.split('/').length - 1]
let extension = filename.split('.')[filename.split('.').length - 1]
let fullpath = req.path.split('/')
fullpath.shift() // Remove HTTP path
fullpath.shift()
fullpath = assemblePath(fullpath) // Reassemble the path
// Extract preview.mustache template
let template = await fs.readFileSync(
__dirname + '/public/mustache/preview.mustache', {
encoding: 'utf-8'
}
)
// If the file is an image display an image
if (['jpg', 'jpeg', 'png', 'webp'].includes(extension)) {
output = `<img href="/download${fullpath}" src="/download${fullpath}">`
} else if (['m4v', 'mp4', 'webm'].includes(extension)) { // If the file is a video, display a video
output = `<video width="320" height="240" controls><source src="/download${fullpath}" type="video/${extension}" /> Your browser does not support the video tag. </video>`
} else { // Otherwise display "Not yet supported"
output = 'Not yet supported'
}
// Render the template
let data = mustache.render(template, {
preview: output
})
// Respond to the user
res.send(data)
} else { // Otherwise this is a file preview that is being requested
// Extract the filename, extension and fullpath
let filename = req.path
.split('/')[req.path.split('/').length - 1].replace(/%20/g, ' ')
let extension = filename.split('.')[filename.split('.').length - 1]
let fullpath = req.path.split('/')
fullpath.shift() // Remove HTTP portion of path
fullpath.shift()
fullpath = assemblePath(fullpath) // reassemble path
let path = previous(fullpath) // Get path without filename
// Query the file from files table
let file = await retrieve(files, {
filename: filename,
location: path
})
// If the file exists and has a preview
if (file && file.preview === true) {
// Generate S3 Parameters
let params = {
Bucket: BUCKET_NAME,
Key: file.fileid + '-preview'
}
// Attach filename to response
res.attachment(file.dataValues.filename)
// Stream preview data to fileStream
let fileStream = await s3.getObject(params).createReadStream()
// pip response to fileStream
fileStream.pipe(res)
} else {
// If the file does not exist, send picture.svg for images
if (['jpg', 'jpeg', 'png'].includes(extension)) {
res.sendFile(__dirname + `/public/images/picture.svg`)
} else {
// send default document.png
res.sendFile(__dirname + `/public/images/document.png`)
}
}
// Tell the browser to cache the file
res.set('Cache-Control', 'public, max-age=3600')
}
})
app.get('/login', async (req, res) => {
// Send login.html
res.sendFile(__dirname + `/public/html/login.html`)
})
app.get('/register', async (req, res) => {
// Send register.html
res.sendFile(__dirname + `/public/html/register.html`)
})
app.post('/register', async (req, res) => {
/**
* Register POST with registration information
*/
let creds = req.body // get the body of the POST
let user = await retrieve(users, { // query the user from the creds
username: creds.user
})
if (user === null) { // If the user doesn't exist, register them
// Create a new user entry in the database
users.create({
username: creds.user,
cookie: req.cookies.browserid,
password: sha512(creds.password)
})
// Redirect to the Storage Manager
res.redirect('/download')
} else {
// Redirect the browser back if the user already exists
res.redirect('/register')
}
})
app.post('/login', async (req, res) => {
/**
* Register POST with login information
*/
let creds = req.body // get the body of the POST
let password = await retrieve(users, { // get the user from the users table
username: creds.user
})
// Check if the user exists, generate hash of the POST's password, and compare to the one in the database
if (password !== null && sha512(creds.pass) === password.dataValues.password) {
console.log(`${password.username}} Logged in`)
// Update the user's cookie
update(
users, {
username: creds.user
}, {
cookie: req.cookies.browserid
}
)
// redirect them to the Storage Manager
res.redirect('/download')
} else {
// Redirect them to /login if login failed
res.redirect('/login')