-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (147 loc) · 5.24 KB
/
index.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
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
'use strict';
const dev = 'development' === process.env.NODE_ENV;
if (!dev) {
process.setgid('www-data');
process.setuid('www-data');
}
require('promise.prototype.finally');
const express = require('express');
const compress = require('compression');
const nunjucks = require('nunjucks');
const multer = require('multer');
const denodeify = require('denodeify');
const crypto = require('crypto');
const fs = require('fs');
const writeFile = denodeify(fs.writeFile);
const upload = multer({ dest: 'uploads/' });
const morgan = require('morgan');
const licenseOptions = [
Object.freeze({key:"CC0", label:"CC0: Creative Commons Zero (recommended)", href:"https://creativecommons.org/publicdomain/zero/1.0/"}),
Object.freeze({key:"Public Domain", label:"Public Domain", href:"https://wiki.creativecommons.org/wiki/Public_domain"}),
Object.freeze({key:"CC BY 3.0", label:"CC BY: Creative Commons Attribution", href:"https://creativecommons.org/licenses/by/3.0/"}),
Object.freeze({key:"CC BY-SA 3.0", label:"CC BY-SA: Creative Commons Attribution-ShareAlike", href:"https://creativecommons.org/licenses/by-sa/3.0/"}),
];
const sources = require('./metadata/sources.json').sources;
const licenseNames = require('./metadata/licenses.json');
const Browser = require('./src/browser');
const browser = new Browser('images.db');
const app = express();
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
app.use(morgan('combined'));
app.use(compress());
nunjucks.configure('views', {
autoescape: true,
express: app,
noCache: dev,
});
app.get('/thanks', (req, res) => {
res.render('thanks.html');
});
app.get('/image/', (req, res) => {
res.redirect('/tags');
});
app.get('/image/:sha1', (req, res, next) => {
browser.getImage(req.params.sha1).then(image => {
const data = image.data;
return browser.getMetrics(data.sha1).then(metrics => {
const download_url = `/${image.sourcePath()}`;
if (data.from == 'pixabay' && data.desc && data.desc.url) {
data.url = data.desc.url;
delete data.desc.url;
}
res.render('image.html', {
image,
metrics,
source: sources[data.from],
license: licenseNames[data.lic],
source_url: `https://github.com/yardstickpics/metadata/blob/master/${data.sha1.substr(0,2)}/${data.sha1.substr(2)}.json`,
download_url: fs.existsSync(`public/${download_url}`) ? download_url : false,
});
});
})
.catch(next);
});
app.get('/sources', (req, res, next) => {
res.render('sources.html', {sources: Object.keys(sources).map(id => ({id, source:sources[id]}))});
});
app.get('/tags', (req, res, next) => {
browser.getAllTags().then(tags => {
const img_tags = [];
const content_tags = [];
tags.forEach(t => {
if (/^img:/.test(t.name)) img_tags.push(t); else content_tags.push(t);
});
res.render('tags.html', {img_tags, content_tags});
})
.catch(next);
});
app.get('/tags/:tag', (req, res, next) => {
browser.getTag(req.params.tag).then(tag => {
res.render('tag.html', {tag});
})
.catch(next);
});
app.get('/sets', (req, res) => {
res.redirect('/sources');
});
app.post('/contribute', upload.any(), (req, res, next) => {
const post = req.body;
const files = req.files;
new Promise((resolve, reject) => {
let error = '';
if (!post.urls && !files.length) {
error = "Please select some images to share";
}
else if (!post.license) {
error = "Please select a license for the images";
}
else if (!post.agree) {
error = "Please check the checkbox confirming that you give these images under the selected license";
}
if (error) {
return reject(error);
}
const created = Date.now();
const submission = JSON.stringify({
created,
post,
files,
headers: req.headers,
ips: req.ips,
}, undefined, 1);
const fileName = `${created}-${crypto.createHash('sha1').update(submission).digest('hex')}`;
writeFile(`uploads/submissions/${fileName}.json`, submission)
.then(resolve, reject);
})
.then(() => {
res.redirect('/thanks');
})
.catch(error => {
// Copy uploaded files to urls, so that re-submission doesn't lose them
if (!post.urls) {
post.urls = '';
} else {
if (!/\n$/.test(post.urls)) post.urls += '\n';
}
files.forEach(file => {
post.urls += `${file.path}\n`;
});
res.render('contribute.html', {
error,
post,
licenses: licenseOptions.map(l => l.key === post.license ? Object.assign({}, l, {selected:true}) : l),
});
})
.catch(next);
});
app.get('/contribute', (req, res) => {
res.render('contribute.html', {licenses: licenseOptions, post:{}});
});
app.use(express.static('public', {
maxAge: 7*24*3600*1000,
redirect: false,
}));
const port = process.env.PORT || 3333;
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}/`);
});