drop.chapril.org-firefoxsend/server/portal_server.js

139 lines
3.1 KiB
JavaScript
Raw Normal View History

const express = require('express');
const exphbs = require('express-handlebars');
const busboy = require('connect-busboy');
const path = require('path');
const bodyParser = require('body-parser');
const bytes = require('bytes');
const conf = require('./config.js');
const storage = require('./storage.js');
2017-06-09 19:44:12 +02:00
const notLocalHost = conf.notLocalHost;
2017-06-08 22:45:28 +02:00
const mozlog = require('./log.js');
2017-06-09 19:44:12 +02:00
const log = mozlog('portal.server');
const app = express();
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
2017-06-01 22:14:14 +02:00
app.use(busboy());
app.use(bodyParser.json());
2017-06-06 23:24:51 +02:00
app.use(express.static(path.join(__dirname, '../public')));
2017-06-01 22:14:14 +02:00
app.get('/', (req, res) => {
res.render('index');
});
2017-06-08 22:45:28 +02:00
app.get('/exists/:id', (req, res) => {
2017-06-09 19:44:12 +02:00
const id = req.params.id;
2017-06-08 22:45:28 +02:00
storage.exists(id).then(doesExist => {
res.sendStatus(doesExist ? 200 : 404);
2017-06-09 00:02:00 +02:00
});
2017-06-08 22:45:28 +02:00
});
2017-06-06 23:24:51 +02:00
app.get('/download/:id', (req, res) => {
2017-06-09 19:44:12 +02:00
const id = req.params.id;
storage.filename(id).then(filename => {
storage
.length(id)
.then(contentLength => {
res.render('download', {
filename: filename,
filesize: bytes(contentLength)
});
})
.catch(() => {
res.render('download');
});
});
2017-06-01 22:14:14 +02:00
});
2017-06-06 23:24:51 +02:00
app.get('/assets/download/:id', (req, res) => {
2017-06-09 19:44:12 +02:00
const id = req.params.id;
2017-06-06 23:24:51 +02:00
if (!validateID(id)) {
res.sendStatus(404);
2017-06-01 22:14:14 +02:00
return;
}
storage
.filename(id)
.then(reply => {
storage.length(id).then(contentLength => {
res.writeHead(200, {
'Content-Disposition': 'attachment; filename=' + reply,
'Content-Type': 'application/octet-stream',
'Content-Length': contentLength
2017-06-06 19:23:37 +02:00
});
2017-06-09 19:44:12 +02:00
const file_stream = storage.get(id);
2017-06-08 02:03:03 +02:00
file_stream.on(notLocalHost ? 'finish' : 'close', () => {
storage.forceDelete(id).then(err => {
if (!err) {
log.info('Deleted:', id);
2017-06-08 02:03:03 +02:00
}
});
});
2017-06-08 02:03:03 +02:00
file_stream.pipe(res);
});
})
.catch(err => {
res.sendStatus(404);
});
2017-06-01 22:14:14 +02:00
});
2017-06-06 23:24:51 +02:00
app.post('/delete/:id', (req, res) => {
2017-06-09 19:44:12 +02:00
const id = req.params.id;
2017-06-01 22:14:14 +02:00
2017-06-06 23:24:51 +02:00
if (!validateID(id)) {
2017-06-01 22:14:14 +02:00
res.send(404);
return;
}
2017-06-06 23:24:51 +02:00
2017-06-09 19:44:12 +02:00
const delete_token = req.body.delete_token;
2017-06-06 23:24:51 +02:00
if (!delete_token) {
2017-06-01 22:14:14 +02:00
res.sendStatus(404);
}
storage
.delete(id, delete_token)
.then(err => {
if (!err) {
2017-06-08 22:45:28 +02:00
log.info('Deleted:', id);
res.sendStatus(200);
}
})
.catch(err => res.sendStatus(404));
2017-06-01 22:14:14 +02:00
});
app.post('/upload/:id', (req, res, next) => {
if (!validateID(req.params.id)) {
res.send(404);
return;
}
2017-06-01 22:14:14 +02:00
req.pipe(req.busboy);
req.busboy.on('file', (fieldname, file, filename) => {
2017-06-08 22:45:28 +02:00
log.info('Uploading:', req.params.id);
const protocol = notLocalHost ? 'https' : req.protocol;
2017-06-09 19:44:12 +02:00
const url = `${protocol}://${req.get('host')}/download/${req.params.id}/`;
storage.set(req.params.id, file, filename, url).then(linkAndID => {
res.json(linkAndID);
});
2017-06-06 23:24:51 +02:00
});
2017-06-01 22:14:14 +02:00
});
2017-06-09 19:44:12 +02:00
app.listen(conf.listen_port, () => {
2017-06-08 22:45:28 +02:00
log.info('startServer:', `Portal app listening on port ${conf.listen_port}!`);
2017-06-06 23:24:51 +02:00
});
2017-06-01 22:14:14 +02:00
2017-06-09 19:44:12 +02:00
const validateID = route_id => {
2017-06-01 22:14:14 +02:00
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null;
2017-06-06 23:24:51 +02:00
};