-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
79 lines (67 loc) · 2.24 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
var S3_BUCKET = 'zen-tests';
var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var crypto = require('crypto');
var moment = require('moment');
var AWS = require('aws-sdk');
var s3 = new AWS.S3({ params: { Bucket: S3_BUCKET }});
var zencoder = require('zencoder')();
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(express.static(path.join(__dirname, 'public')));
app.post('/process', function(req, res) {
// Build up the S3 URL based on the specified S3 Bucket and filename included
// in the POST request body.
var input = 'https://'+S3_BUCKET+'.s3.amazonaws.com/'+req.body.filename;
createJob(input, req.body.email, function(err, data) {
if (err) { return res.send(500, err); }
res.send(200, data);
});
});
app.post('/upload', function(req, res) {
var cors = createS3Policy();
res.send(201, { url: 'https://'+S3_BUCKET+'.s3.amazonaws.com/', cors: cors });
});
function createS3Policy() {
var policy = {
"expiration": moment().utc().add('days', 1).toISOString(),
"conditions": [
{ "bucket": S3_BUCKET },
{ "acl":"private" },
[ "starts-with", "$key", "" ],
[ "starts-with", "$Content-Type", "" ],
[ "content-length-range", 0, 5368709120 ]
]
};
var base64Policy = new Buffer(JSON.stringify(policy)).toString('base64');
var signature = crypto.createHmac('sha1', AWS.config.credentials.secretAccessKey).update(base64Policy).digest('base64');
return {
key: AWS.config.credentials.accessKeyId,
policy: base64Policy,
signature: signature
};
}
function createJob(input, email, cb) {
var watermark = {
url: 'https://s3.amazonaws.com/zencoder-demo/blog-posts/videobooth.png',
x: '-0',
y: '-0',
width: '30%'
};
zencoder.Job.create({
input: input,
notifications: [ email ],
outputs: [
{ format: 'mp4', watermarks: [watermark] },
{ format: 'webm', watermarks: [watermark] }
]
}, cb);
}
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});