24
1
Fork 0

currently not working, decryption seems to fail

This commit is contained in:
Abhinav Adduri 2017-06-05 15:35:36 -07:00 committed by Danny Coates
parent 0677603d74
commit 39a63cd16e
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
4 changed files with 119 additions and 48 deletions

View File

@ -5,6 +5,7 @@ class FileReceiver extends EventEmitter {
constructor() { constructor() {
super(); super();
this.salt = strToIv(location.pathname.slice(10, -1)); this.salt = strToIv(location.pathname.slice(10, -1));
window.salt = this.salt;
} }
download() { download() {
@ -30,11 +31,13 @@ class FileReceiver extends EventEmitter {
let blob = new Blob([this.response]); let blob = new Blob([this.response]);
let fileReader = new FileReader(); let fileReader = new FileReader();
fileReader.onload = function() { fileReader.onload = function() {
window.data = this.result;
console.log(this.result);
resolve({ resolve({
data: this.result, data: this.result,
fname: xhr fname: xhr
.getResponseHeader('Content-Disposition') .getResponseHeader('Content-Disposition')
.match(/filename="(.+)"/)[1] .match(/="(.+)"/)[1]
}); });
}; };

View File

@ -4,8 +4,10 @@
"version": "1.0.0", "version": "1.0.0",
"author": "Mozilla (https://mozilla.org)", "author": "Mozilla (https://mozilla.org)",
"dependencies": { "dependencies": {
"aws-sdk": "^2.62.0",
"body-parser": "^1.17.2", "body-parser": "^1.17.2",
"connect-busboy": "0.0.2", "connect-busboy": "0.0.2",
"convict": "^3.0.0",
"express": "^4.15.3", "express": "^4.15.3",
"fs-extra": "^3.0.1", "fs-extra": "^3.0.1",
"path": "^0.12.7", "path": "^0.12.7",

16
server/config.js Normal file
View File

@ -0,0 +1,16 @@
const convict = require('convict');
let conf = convict({
aws_credentials: {
region: 'us-west-2',
bucketName: 'testpilot-p2p'
}
})
// var env = conf.get('env');
// conf.loadFile('./config/' + env + '.json');
// Perform validation
conf.validate({allowed: 'strict'});
module.exports = conf;

View File

@ -1,15 +1,24 @@
const express = require('express'); const express = require("express")
const busboy = require('connect-busboy'); const busboy = require("connect-busboy");
const path = require('path'); const path = require("path");
const fs = require('fs-extra'); const fs = require("fs-extra");
const bodyParser = require('body-parser'); const bodyParser = require("body-parser");
const crypto = require('crypto'); const crypto = require("crypto");
const conf = require('./config.js');
const stream = require('stream');
const app = express(); let aws_credentials = conf.get('aws_credentials');
const redis = require('redis'),
client = redis.createClient();
client.on('error', err => { const AWS = require('aws-sdk');
AWS.config.loadFromPath('../../.aws/credentials');
const s3 = new AWS.S3();
const app = express()
const redis = require("redis"),
redis_client = redis.createClient();
redis_client.on("error", (err) => {
console.log(err); console.log(err);
}); });
@ -28,22 +37,36 @@ app.get('/assets/download/:id', (req, res) => {
return; return;
} }
client.hget(id, 'filename', (err, reply) => { let params = {
// maybe some expiration logic too Bucket: aws_credentials.bucketName,
if (!reply) { Key: req.params.id
res.sendStatus(404); }
} else {
res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
res.setHeader('Content-Type', 'application/octet-stream');
res.download(__dirname + '/../static/' + id, reply, err => { s3.getObject(params, function(err, data) {
if (!err) { if (err) {
client.del(id); console.log(err, err.stack); // an error occurred
fs.unlinkSync(__dirname + '/../static/' + id);
}
});
} }
}); else {
res.writeHead(200, {"Content-Disposition": "attachment; filename=response", //+ reply,
"Content-Type": "application/octet-stream"});
// res.setHeader("Content-Type", "application/octet-stream");
res.end(new Buffer(data.Body))
}
})
// redis_client.hget(id, "filename", (err, reply) => { // maybe some expiration logic too
// if (!reply) {
// res.sendStatus(404);
// } else {
// res.download(__dirname + "/../static/" + id, reply, (err) => {
// if (!err) {
// redis_client.del(id);
// fs.unlinkSync(__dirname + "/../static/" + id);
// }
// });
// }
// })
}); });
app.post('/delete/:id', (req, res) => { app.post('/delete/:id', (req, res) => {
@ -60,45 +83,72 @@ app.post('/delete/:id', (req, res) => {
res.sendStatus(404); res.sendStatus(404);
} }
<<<<<<< HEAD
client.hget(id, 'delete', (err, reply) => { client.hget(id, 'delete', (err, reply) => {
if (!reply) { if (!reply) {
res.sendStatus(404); res.sendStatus(404);
} else { } else {
client.del(id); client.del(id);
fs.unlinkSync(__dirname + '/../static/' + id); fs.unlinkSync(__dirname + '/../static/' + id);
=======
redis_client.hget(id, "delete", (err, reply) => {
if (!reply) {
res.sendStatus(404);
} else {
redis_client.del(id);
fs.unlinkSync(__dirname + "/../static/" + id);
>>>>>>> currently not working, decryption seems to fail
res.sendStatus(200); res.sendStatus(200);
} }
}); });
}); });
app.post('/upload/:id', (req, res, next) => { app.post("/upload/:id", (req, res, next) => {
if (!validateID(req.params.id)) {
res.send(404);
return;
}
let fstream; if (!validateID(req.params.id)){
req.pipe(req.busboy); res.send(404);
req.busboy.on('file', (fieldname, file, filename) => { return;
console.log('Uploading: ' + filename); }
//Path where image will be uploaded let fstream;
fstream = fs.createWriteStream(__dirname + '/../static/' + req.params.id); req.pipe(req.busboy);
file.pipe(fstream); req.busboy.on("file", (fieldname, file, filename) => {
fstream.on('close', () => { console.log("Uploading: " + filename);
let id = req.params.id;
let uuid = crypto.randomBytes(10).toString('hex');
client.hmset([id, 'filename', filename, 'delete', uuid]);
// delete the file off the server in 24 hours let params = {
// setTimeout(() => { Bucket: aws_credentials.bucketName,
// fs.unlinkSync(__dirname + "/static/" + id); Key: req.params.id,
// }, 86400000); Body: file
}
client.expire(id, 86400000); s3.upload(params, function(err, data) {
console.log('Upload Finished of ' + filename); if (err) {
res.send(uuid); console.log(err, err.stack); // an error occurred
} else {
console.log(data);
}
})
return;
fstream = fs.createWriteStream(__dirname + "/../static/" + req.params.id);
file.pipe(fstream);
fstream.on("close", () => {
let id = req.params.id;
let uuid = crypto.randomBytes(10).toString('hex');
redis_client.hmset([id, "filename", filename, "delete", uuid]);
// delete the file off the server in 24 hours
// setTimeout(() => {
// fs.unlinkSync(__dirname + "/static/" + id);
// }, 86400000);
redis_client.expire(id, 86400000);
console.log("Upload Finished of " + filename);
res.send(uuid);
});
}); });
}); });
}); });