send/server/portal_server.js

120 lines
2.8 KiB
JavaScript
Raw Normal View History

const express = require('express');
const busboy = require('connect-busboy');
const path = require('path');
const fs = require('fs-extra');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const conf = require('./config.js');
const stream = require('stream');
const fetch = require('node-fetch');
const storage = require('./storage.js');
let isProduction =
conf.env === 'production' &&
conf.s3_bucket !== 'localhost' &&
conf.bitly_key !== 'localhost';
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const app = express();
const redis = require('redis');
const redis_client = redis.createClient();
redis_client.on('error', err => {
2017-06-01 20:14:14 +00:00
console.log(err);
2017-06-06 21:24:51 +00:00
});
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-06 21:24:51 +00:00
app.get('/download/:id', (req, res) => {
res.sendFile(path.join(__dirname + '/../public/download.html'));
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-01 20:14:14 +00:00
let id = req.params.id;
2017-06-06 21:24:51 +00:00
if (!validateID(id)) {
res.sendStatus(404);
2017-06-01 20:14:14 +00:00
return;
}
redis_client.hget(id, 'filename', (err, reply) => {
2017-06-06 17:23:37 +00:00
if (!reply) {
res.sendStatus(404);
} else {
storage.length(id).then(contentLength => {
res.writeHead(200, {
'Content-Disposition': 'attachment; filename=' + reply,
'Content-Type': 'application/octet-stream',
'Content-Length': contentLength
2017-06-06 17:23:37 +00:00
});
});
let file_stream = storage.get(id);
file_stream.on('close', () => {
storage.forceDelete(id, redis_client).then(err => {
if (!err) {
console.log('Deleted.');
}
});
2017-06-06 17:23:37 +00:00
});
file_stream.pipe(res);
2017-06-06 17:23:37 +00:00
}
});
2017-06-01 20:14:14 +00:00
});
2017-06-06 21:24:51 +00:00
app.post('/delete/:id', (req, res) => {
2017-06-01 20:14:14 +00:00
let id = req.params.id;
2017-06-06 21:24:51 +00:00
if (!validateID(id)) {
2017-06-01 20:14:14 +00:00
res.send(404);
return;
}
2017-06-06 21:24:51 +00:00
2017-06-01 20:14:14 +00:00
let 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);
}
storage
.delete(id, redis_client, delete_token)
.then(err => {
if (!err) {
console.log('Deleted off s3.');
}
})
.catch(err => res.sendStatus(404));
2017-06-01 20:14:14 +00:00
});
app.post('/upload/:id', (req, res, next) => {
if (!validateID(req.params.id)) {
res.send(404);
return;
}
2017-06-01 20:14:14 +00:00
req.pipe(req.busboy);
req.busboy.on('file', (fieldname, file, filename) => {
console.log('Uploading: ' + filename);
let url = `${req.protocol}://${req.get('host')}/download/${req.params.id}/`;
storage
.set(req.params.id, file, filename, redis_client, url)
.then(linkAndID => {
res.json(linkAndID);
});
2017-06-06 21:24:51 +00:00
});
2017-06-01 20:14:14 +00:00
});
2017-06-07 06:52:56 +00:00
let server = app.listen(conf.listen_port, () => {
console.log(`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-06 21:24:51 +00:00
let 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
};