-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (73 loc) · 2.01 KB
/
app.js
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
const express = require('express');
const aws = require('aws-sdk');
const path = require('path');
const stormpath = require('express-stormpath');
const app = express();
const port = process.env.PORT || 3000;
const S3_BUCKET = process.env.S3_BUCKET;
app.use(express.static(path.join(__dirname, '/public')));
app.use(stormpath.init(app, { website: true }));
app.set('views', './public/views');
app.set('view engine', 'ejs');
app.on('stormpath.ready', () => {
app.listen(port);
console.log('listening on port', port);
})
const s3 = new aws.S3();
app.get('/', stormpath.loginRequired, stormpath.getUser, (req, res) => {
console.log(req.user);
res.send();
})
app.get('/sign-s3', stormpath.loginRequired, (req, res) => {
const fileName = req.query['file-name'];
const fileType = req.query['file-type'];
const s3Params = {
Bucket: S3_BUCKET,
Key: fileName,
Expires: 600,
ContentType: fileType,
ACL: 'public-read'
};
s3.getSignedUrl('putObject', s3Params, (err, data) => {
if (err) {
console.log('error getting signed url: ', err);
return res.end();
}
const returnData = {
signedRequest: data,
url: `https://${S3_BUCKET}.s3.amazonaws.com/${fileName}`
};
res.send(returnData);
});
});
app.get('/batchList', stormpath.loginRequired, (req, res) => {
const batchListParams = {
Bucket: S3_BUCKET,
Delimiter: '/'
};
s3.listObjects(batchListParams, (err, data) => {
if (err) {
console.log('error getting batch list:', err);
return res.end();
}
res.send(data);
});
});
app.get('/batch/:name', stormpath.loginRequired, (req, res) => {
const batchName = req.params.name;
const batchListParams = {
Bucket: S3_BUCKET,
Prefix: batchName
}
s3.listObjects(batchListParams, (err, data) => {
if (err) {
console.log('error retrieving batch:', err);
return res.end();
}
res.render('batch', {
data: data,
urlPath: `https://${S3_BUCKET}.s3.amazonaws.com/`,
title: batchName
});
})
});