2017-06-06 17:24:58 +00:00
|
|
|
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');
|
2017-06-05 22:35:36 +00:00
|
|
|
const conf = require('./config.js');
|
|
|
|
const stream = require('stream');
|
2017-06-06 17:24:58 +00:00
|
|
|
const fetch = require('node-fetch');
|
2017-06-05 22:35:36 +00:00
|
|
|
|
2017-06-06 17:24:58 +00:00
|
|
|
let isProduction = conf.env === 'production';
|
2017-06-05 22:35:36 +00:00
|
|
|
|
|
|
|
const AWS = require('aws-sdk');
|
|
|
|
const s3 = new AWS.S3();
|
|
|
|
|
2017-06-06 17:24:58 +00:00
|
|
|
const app = express();
|
|
|
|
const redis = require('redis');
|
|
|
|
const redis_client = redis.createClient();
|
2017-06-05 22:35:36 +00:00
|
|
|
|
2017-06-06 17:24:58 +00:00
|
|
|
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)) {
|
2017-06-06 17:24:58 +00:00
|
|
|
res.sendStatus(404);
|
2017-06-01 20:14:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:24:58 +00:00
|
|
|
redis_client.hget(id, 'filename', (err, reply) => {
|
|
|
|
// maybe some expiration logic too
|
2017-06-06 17:23:37 +00:00
|
|
|
if (!reply) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
} else {
|
|
|
|
let params = {
|
2017-06-07 06:52:56 +00:00
|
|
|
Bucket: config.s3_bucket,
|
2017-06-06 17:23:37 +00:00
|
|
|
Key: id
|
2017-06-06 17:24:58 +00:00
|
|
|
};
|
2017-06-06 17:23:37 +00:00
|
|
|
|
|
|
|
s3.headObject(params, function(err, data) {
|
2017-06-06 17:24:58 +00:00
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Disposition': 'attachment; filename=' + reply,
|
|
|
|
'Content-Type': 'application/octet-stream',
|
|
|
|
'Content-Length': data.ContentLength
|
|
|
|
});
|
2017-06-06 17:23:37 +00:00
|
|
|
let file_stream = s3.getObject(params).createReadStream();
|
|
|
|
|
|
|
|
file_stream.on('finish', () => {
|
|
|
|
redis_client.del(id);
|
|
|
|
s3.deleteObject(params, function(err, data) {
|
|
|
|
if (!err) {
|
|
|
|
console.log('Deleted off s3.');
|
|
|
|
}
|
2017-06-06 17:24:58 +00:00
|
|
|
});
|
2017-06-06 17:23:37 +00:00
|
|
|
});
|
2017-06-05 22:35:36 +00:00
|
|
|
|
2017-06-06 17:23:37 +00:00
|
|
|
file_stream.pipe(res);
|
|
|
|
});
|
|
|
|
}
|
2017-06-06 17:24:58 +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);
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:24:58 +00:00
|
|
|
redis_client.hget(id, 'delete', (err, reply) => {
|
|
|
|
if (!reply || delete_token !== reply) {
|
2017-06-05 22:35:36 +00:00
|
|
|
res.sendStatus(404);
|
|
|
|
} else {
|
|
|
|
redis_client.del(id);
|
2017-06-06 17:23:37 +00:00
|
|
|
let params = {
|
2017-06-07 06:52:56 +00:00
|
|
|
Bucket: config.s3_bucket,
|
2017-06-06 17:23:37 +00:00
|
|
|
Key: id
|
2017-06-06 17:24:58 +00:00
|
|
|
};
|
2017-06-06 17:23:37 +00:00
|
|
|
|
|
|
|
s3.deleteObject(params, function(err, data) {
|
|
|
|
if (!err) {
|
|
|
|
console.log('Deleted off s3.');
|
|
|
|
}
|
2017-06-06 17:24:58 +00:00
|
|
|
});
|
2017-06-06 17:23:37 +00:00
|
|
|
|
2017-06-01 20:14:14 +00:00
|
|
|
res.sendStatus(200);
|
|
|
|
}
|
2017-06-06 21:24:51 +00:00
|
|
|
});
|
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)) {
|
|
|
|
res.send(404);
|
|
|
|
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) => {
|
|
|
|
console.log('Uploading: ' + filename);
|
|
|
|
|
|
|
|
let params = {
|
2017-06-07 06:52:56 +00:00
|
|
|
Bucket: config.s3_bucket,
|
2017-06-06 17:24:58 +00:00
|
|
|
Key: req.params.id,
|
|
|
|
Body: file
|
|
|
|
};
|
|
|
|
|
|
|
|
s3.upload(params, function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err, err.stack); // an error occurred
|
|
|
|
} else {
|
|
|
|
let id = req.params.id;
|
|
|
|
let uuid = crypto.randomBytes(10).toString('hex');
|
|
|
|
|
|
|
|
redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
|
|
|
|
|
|
|
|
redis_client.expire(id, 86400000);
|
|
|
|
console.log('Upload Finished of ' + filename);
|
2017-06-07 06:52:56 +00:00
|
|
|
let url = `${req.protocol}://${req.get('host')}/download/${req.params.id}/`;
|
|
|
|
if (config.bitly_key) {
|
2017-06-06 17:24:58 +00:00
|
|
|
fetch(
|
|
|
|
'https://api-ssl.bitly.com/v3/shorten?access_token=' +
|
2017-06-07 06:52:56 +00:00
|
|
|
config.bitly_key +
|
2017-06-06 17:24:58 +00:00
|
|
|
'&longUrl=' +
|
|
|
|
encodeURIComponent(url) +
|
|
|
|
'&format=txt'
|
|
|
|
)
|
|
|
|
.then(res => {
|
|
|
|
return res.text();
|
|
|
|
})
|
|
|
|
.then(body => {
|
|
|
|
res.json({
|
|
|
|
uuid: uuid,
|
|
|
|
url: body
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
uuid: uuid,
|
|
|
|
url: url
|
|
|
|
});
|
2017-06-05 22:35:36 +00:00
|
|
|
}
|
2017-06-06 17:24:58 +00:00
|
|
|
}
|
2017-06-01 20:14:14 +00:00
|
|
|
});
|
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
|
|
|
};
|