2017-06-06 17:24:58 +00:00
|
|
|
const express = require('express');
|
2017-06-07 23:16:38 +00:00
|
|
|
const exphbs = require('express-handlebars');
|
2017-06-06 17:24:58 +00:00
|
|
|
const busboy = require('connect-busboy');
|
|
|
|
const path = require('path');
|
|
|
|
const bodyParser = require('body-parser');
|
2017-06-19 22:51:48 +00:00
|
|
|
const helmet = require('helmet');
|
2017-06-07 23:16:38 +00:00
|
|
|
const bytes = require('bytes');
|
|
|
|
const conf = require('./config.js');
|
2017-06-07 21:07:31 +00:00
|
|
|
const storage = require('./storage.js');
|
2017-06-23 18:29:45 +00:00
|
|
|
const pkg = require('../package.json');
|
|
|
|
|
|
|
|
const gitSHA = require('git-rev-sync').short();
|
2017-06-05 22:35:36 +00:00
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
const notLocalHost = conf.notLocalHost;
|
2017-06-05 22:35:36 +00:00
|
|
|
|
2017-06-08 20:45:28 +00:00
|
|
|
const mozlog = require('./log.js');
|
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
const log = mozlog('portal.server');
|
2017-06-05 22:35:36 +00:00
|
|
|
|
2017-06-06 17:24:58 +00:00
|
|
|
const app = express();
|
2017-06-05 22:35:36 +00:00
|
|
|
|
2017-06-22 21:50:57 +00:00
|
|
|
app.engine('handlebars', exphbs({
|
|
|
|
defaultLayout: 'main',
|
|
|
|
partialsDir: 'views/partials/'
|
|
|
|
}));
|
2017-06-07 23:16:38 +00:00
|
|
|
app.set('view engine', 'handlebars');
|
2017-06-01 20:14:14 +00:00
|
|
|
|
2017-06-19 22:51:48 +00:00
|
|
|
app.use(helmet());
|
2017-06-01 20:14:14 +00:00
|
|
|
app.use(busboy());
|
|
|
|
app.use(bodyParser.json());
|
2017-06-06 21:24:51 +00:00
|
|
|
app.use(express.static(path.join(__dirname, '../public')));
|
2017-06-01 20:14:14 +00:00
|
|
|
|
2017-06-22 21:50:57 +00:00
|
|
|
|
2017-06-07 23:16:38 +00:00
|
|
|
app.get('/', (req, res) => {
|
2017-06-21 20:54:24 +00:00
|
|
|
res.render('index', {
|
|
|
|
shouldRenderAnalytics: notLocalHost,
|
2017-06-22 21:50:57 +00:00
|
|
|
trackerId: conf.analytics_id,
|
|
|
|
dsn: conf.sentry_id
|
2017-06-21 20:54:24 +00:00
|
|
|
});
|
2017-06-07 23:16:38 +00:00
|
|
|
});
|
|
|
|
|
2017-06-08 20:45:28 +00:00
|
|
|
app.get('/exists/:id', (req, res) => {
|
2017-06-09 17:44:12 +00:00
|
|
|
const id = req.params.id;
|
2017-06-08 20:45:28 +00:00
|
|
|
storage.exists(id).then(doesExist => {
|
|
|
|
res.sendStatus(doesExist ? 200 : 404);
|
2017-06-08 22:02:00 +00:00
|
|
|
});
|
2017-06-08 20:45:28 +00:00
|
|
|
});
|
|
|
|
|
2017-06-06 21:24:51 +00:00
|
|
|
app.get('/download/:id', (req, res) => {
|
2017-06-09 17:44:12 +00:00
|
|
|
const id = req.params.id;
|
2017-06-07 23:16:38 +00:00
|
|
|
storage.filename(id).then(filename => {
|
|
|
|
storage
|
|
|
|
.length(id)
|
|
|
|
.then(contentLength => {
|
|
|
|
res.render('download', {
|
|
|
|
filename: filename,
|
2017-06-21 20:54:24 +00:00
|
|
|
filesize: bytes(contentLength),
|
|
|
|
shouldRenderAnalytics: notLocalHost,
|
2017-06-22 21:50:57 +00:00
|
|
|
trackerId: conf.analytics_id,
|
|
|
|
dsn: conf.sentry_id
|
2017-06-07 23:16:38 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
res.render('download');
|
|
|
|
});
|
|
|
|
});
|
2017-06-01 20:14:14 +00:00
|
|
|
});
|
|
|
|
|
2017-06-06 21:24:51 +00:00
|
|
|
app.get('/assets/download/:id', (req, res) => {
|
2017-06-09 17:44:12 +00:00
|
|
|
const id = req.params.id;
|
2017-06-06 21:24:51 +00:00
|
|
|
if (!validateID(id)) {
|
2017-06-06 17:24:58 +00:00
|
|
|
res.sendStatus(404);
|
2017-06-01 20:14:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-07 23:16:38 +00:00
|
|
|
storage
|
|
|
|
.filename(id)
|
|
|
|
.then(reply => {
|
2017-06-07 21:07:31 +00:00
|
|
|
storage.length(id).then(contentLength => {
|
2017-06-06 17:24:58 +00:00
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Disposition': 'attachment; filename=' + reply,
|
|
|
|
'Content-Type': 'application/octet-stream',
|
2017-06-07 21:07:31 +00:00
|
|
|
'Content-Length': contentLength
|
2017-06-06 17:23:37 +00:00
|
|
|
});
|
2017-06-09 17:44:12 +00:00
|
|
|
const file_stream = storage.get(id);
|
2017-06-05 22:35:36 +00:00
|
|
|
|
2017-06-08 00:03:03 +00:00
|
|
|
file_stream.on(notLocalHost ? 'finish' : 'close', () => {
|
2017-06-20 19:52:01 +00:00
|
|
|
storage
|
|
|
|
.forceDelete(id)
|
|
|
|
.then(err => {
|
|
|
|
if (!err) {
|
|
|
|
log.info('Deleted:', id);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
log.info('DeleteError:', id);
|
|
|
|
});
|
2017-06-07 21:07:31 +00:00
|
|
|
});
|
|
|
|
|
2017-06-08 00:03:03 +00:00
|
|
|
file_stream.pipe(res);
|
|
|
|
});
|
2017-06-07 23:16:38 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.sendStatus(404);
|
|
|
|
});
|
2017-06-01 20:14:14 +00:00
|
|
|
});
|
|
|
|
|
2017-06-06 21:24:51 +00:00
|
|
|
app.post('/delete/:id', (req, res) => {
|
2017-06-09 17:44:12 +00:00
|
|
|
const id = req.params.id;
|
2017-06-01 20:14:14 +00:00
|
|
|
|
2017-06-06 21:24:51 +00:00
|
|
|
if (!validateID(id)) {
|
2017-06-19 21:30:04 +00:00
|
|
|
res.sendStatus(404);
|
2017-06-01 20:14:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-06 21:24:51 +00:00
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
const delete_token = req.body.delete_token;
|
2017-06-06 21:24:51 +00:00
|
|
|
|
|
|
|
if (!delete_token) {
|
2017-06-01 20:14:14 +00:00
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:07:31 +00:00
|
|
|
storage
|
2017-06-07 23:16:38 +00:00
|
|
|
.delete(id, delete_token)
|
2017-06-20 19:52:01 +00:00
|
|
|
.then(err => {
|
2017-06-07 21:07:31 +00:00
|
|
|
if (!err) {
|
2017-06-08 20:45:28 +00:00
|
|
|
log.info('Deleted:', id);
|
2017-06-08 21:38:00 +00:00
|
|
|
res.sendStatus(200);
|
2017-06-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => res.sendStatus(404));
|
2017-06-01 20:14:14 +00:00
|
|
|
});
|
|
|
|
|
2017-06-06 17:24:58 +00:00
|
|
|
app.post('/upload/:id', (req, res, next) => {
|
|
|
|
if (!validateID(req.params.id)) {
|
2017-06-19 20:37:56 +00:00
|
|
|
res.sendStatus(404);
|
2017-06-06 17:24:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-01 20:14:14 +00:00
|
|
|
|
2017-06-06 17:24:58 +00:00
|
|
|
req.pipe(req.busboy);
|
|
|
|
req.busboy.on('file', (fieldname, file, filename) => {
|
2017-06-08 20:45:28 +00:00
|
|
|
log.info('Uploading:', req.params.id);
|
2017-06-08 20:48:51 +00:00
|
|
|
|
2017-06-08 22:01:04 +00:00
|
|
|
const protocol = notLocalHost ? 'https' : req.protocol;
|
2017-06-09 17:44:12 +00:00
|
|
|
const url = `${protocol}://${req.get('host')}/download/${req.params.id}/`;
|
2017-06-06 17:24:58 +00:00
|
|
|
|
2017-06-07 23:16:38 +00:00
|
|
|
storage.set(req.params.id, file, filename, url).then(linkAndID => {
|
|
|
|
res.json(linkAndID);
|
|
|
|
});
|
2017-06-06 21:24:51 +00:00
|
|
|
});
|
2017-06-01 20:14:14 +00:00
|
|
|
});
|
|
|
|
|
2017-06-22 19:18:07 +00:00
|
|
|
app.get('/__lbheartbeat__', (req, res) => {
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
2017-06-23 18:29:45 +00:00
|
|
|
app.get('/__version__', (req, res) => {
|
|
|
|
res.json({
|
|
|
|
commit: gitSHA,
|
|
|
|
source: pkg.homepage,
|
|
|
|
version: pkg.version
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
app.listen(conf.listen_port, () => {
|
2017-06-08 20:45:28 +00:00
|
|
|
log.info('startServer:', `Portal app listening on port ${conf.listen_port}!`);
|
2017-06-06 21:24:51 +00:00
|
|
|
});
|
2017-06-01 20:14:14 +00:00
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
const validateID = route_id => {
|
2017-06-01 20:14:14 +00:00
|
|
|
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null;
|
2017-06-06 21:24:51 +00:00
|
|
|
};
|