forked from willwebberley/NodeDirectUploader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
88 lines (73 loc) · 2.9 KB
/
app.js
File metadata and controls
88 lines (73 loc) · 2.9 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
/*
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
/*
* Import required packages.
* Packages should be installed with "npm install".
*/
var express = require('express');
var crypto = require('crypto');
var http = require('http');
var path = require('path');
/*
* Set-up the Express app.
*/
var app = express();
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'public')));
/*
* Load the S3 information from the environment variables.
*/
var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
var S3_BUCKET = process.env.S3_BUCKET
/*
* Respond to GET requests to /account.
* Upon request, render the 'account.html' web page in views/ directory.
*/
app.get('/account', function(req, res){
res.render('account.html');
});
/*
* Respond to GET requests to /sign_s3.
* Upon request, return JSON containing the temporarily-signed S3 request and the
* anticipated URL of the image.
*/
app.get('/sign_s3', function(req, res){
var object_name = req.query.s3_object_name;
var mime_type = req.query.s3_object_type;
var now = new Date();
var expires = Math.ceil((now.getTime() + 10000)/1000); // 10 seconds from now
var amz_headers = "x-amz-acl:public-read";
var put_request = "PUT\n\n"+mime_type+"\n"+expires+"\n"+amz_headers+"\n/"+S3_BUCKET+"/"+object_name;
var signature = crypto.createHmac('sha1', AWS_SECRET_KEY).update(put_request).digest('base64');
signature = encodeURIComponent(signature.trim());
signature = signature.replace('%2B','+');
var url = 'https://'+S3_BUCKET+'.s3.amazonaws.com/'+object_name;
var credentials = {
signed_request: url+"?AWSAccessKeyId="+AWS_ACCESS_KEY+"&Expires="+expires+"&Signature="+signature,
url: url
};
res.write(JSON.stringify(credentials));
res.end();
});
/*
* Respond to POST requests to /submit_form.
* This function needs to be completed to handle the information in
* a way that suits your application.
*/
app.post('/submit_form', function(req, res){
username = req.body.username;
full_name = req.body.full_name;
avatar_url = req.body.avatar_url;
update_account(username, full_name, avatar_url); // TODO: create this function
// TODO: Return something useful or redirect
});
/*
* Start the server to handle incoming requests.
*/
app.listen(app.get('port'));