-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·92 lines (80 loc) · 2.28 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
85
86
87
88
89
90
91
92
var express = require('express');
var pug = require('pug');
var AWS = require('aws-sdk');
var basicAuth = require('basic-auth-connect');
var config = require('./config');
var port = process.env.PORT || 3000;
var app = express();
app.set('view engine', 'pug');
app.use(basicAuth(config.access.username, config.access.password));
app.use('/img', express.static('img'));
app.use('/styles', express.static('styles'));
app.use('/scripts', express.static('scripts'));
AWS.config.update(config.s3);
var s3 = new AWS.S3();
String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
}
app.get('/', function (req, res) {
var params = {
Bucket: config.s3.bucket,
Delimiter: '/'
};
s3.listObjects(params, function (err, data) {
if (err) {
res.send(err);
} else {
var albums = [];
for (var i in data.CommonPrefixes) {
var prefix = data.CommonPrefixes[i].Prefix;
var name = prefix.substr(0, prefix.length - 1).capitalize();
var album = {
'prefix': prefix,
'name': name
}
albums.push(album);
}
res.render('index', {
'albums': albums
});
}
});
});
app.get('/:album', function (req, res) {
var album = req.params.album;
if (album.substr(album.length - 1, 1) !== '/') {
album = album + '/';
}
var params = {
Bucket: config.s3.bucket,
Prefix: album,
Delimiter: '/'
};
s3.listObjects(params, function (err, data) {
if (err) {
res.send(err);
} else {
var images = [];
for (i in data.Contents) {
var image = data.Contents[i];
if (image.Key.endsWith(config.suffix.small)) {
var imageProps = {
url: image.Key,
full_suffix: config.suffix.full,
rotation: (Math.random() * 20 - 10)
};
images.push(imageProps);
}
}
res.render('album', {
'name': album.substr(0, album.length - 1).capitalize(),
'images': images
});
}
});
});
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('app listening at http://%s:%s', host, port);
});