working s3 integration

This commit is contained in:
Abhinav Adduri 2017-06-06 10:23:37 -07:00 committed by Danny Coates
parent 39a63cd16e
commit f377aa4551
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
2 changed files with 54 additions and 56 deletions

View File

@ -37,7 +37,7 @@ class FileReceiver extends EventEmitter {
data: this.result, data: this.result,
fname: xhr fname: xhr
.getResponseHeader('Content-Disposition') .getResponseHeader('Content-Disposition')
.match(/="(.+)"/)[1] .match(/=(.+)/)[1]
}); });
}; };

View File

@ -37,36 +37,48 @@ app.get('/assets/download/:id', (req, res) => {
return; return;
} }
redis_client.hget(id, "filename", (err, reply) => { // maybe some expiration logic too
if (!reply) {
res.sendStatus(404);
} else {
let params = { let params = {
Bucket: aws_credentials.bucketName, Bucket: aws_credentials.bucketName,
Key: req.params.id Key: id
} }
s3.getObject(params, function(err, data) { s3.headObject(params, function(err, data) {
if (err) { res.writeHead(200, {"Content-Disposition": "attachment; filename=" + reply,
console.log(err, err.stack); // an error occurred "Content-Type": "application/octet-stream",
} "Content-Length": data.ContentLength});
else { let file_stream = s3.getObject(params).createReadStream();
res.writeHead(200, {"Content-Disposition": "attachment; filename=response", //+ reply,
"Content-Type": "application/octet-stream"}); file_stream.on('finish', () => {
// res.setHeader("Content-Type", "application/octet-stream"); redis_client.del(id);
res.end(new Buffer(data.Body)) s3.deleteObject(params, function(err, data) {
if (!err) {
console.log('Deleted off s3.');
} }
}) })
});
// redis_client.hget(id, "filename", (err, reply) => { // maybe some expiration logic too file_stream.pipe(res);
// if (!reply) { });
// s3.getObject(params, function(err, data) {
// if (err) {
// console.log(err, err.stack); // an error occurred
// res.sendStatus(404); // res.sendStatus(404);
// } else {
// res.download(__dirname + "/../static/" + id, reply, (err) => {
// if (!err) {
// redis_client.del(id);
// fs.unlinkSync(__dirname + "/../static/" + id);
// } // }
// }); // else {
// } // }
// }) // })
}
})
}); });
app.post('/delete/:id', (req, res) => { app.post('/delete/:id', (req, res) => {
@ -83,21 +95,22 @@ app.post('/delete/:id', (req, res) => {
res.sendStatus(404); res.sendStatus(404);
} }
<<<<<<< HEAD
client.hget(id, 'delete', (err, reply) => {
if (!reply) {
res.sendStatus(404);
} else {
client.del(id);
fs.unlinkSync(__dirname + '/../static/' + id);
=======
redis_client.hget(id, "delete", (err, reply) => { redis_client.hget(id, "delete", (err, reply) => {
if (!reply) { if (!reply || (delete_token !== reply)) {
res.sendStatus(404); res.sendStatus(404);
} else { } else {
redis_client.del(id); redis_client.del(id);
fs.unlinkSync(__dirname + "/../static/" + id); let params = {
>>>>>>> currently not working, decryption seems to fail Bucket: aws_credentials.bucketName,
Key: id
}
s3.deleteObject(params, function(err, data) {
if (!err) {
console.log('Deleted off s3.');
}
})
res.sendStatus(200); res.sendStatus(200);
} }
}); });
@ -110,12 +123,10 @@ app.post("/upload/:id", (req, res, next) => {
return; return;
} }
let fstream;
req.pipe(req.busboy); req.pipe(req.busboy);
req.busboy.on("file", (fieldname, file, filename) => { req.busboy.on("file", (fieldname, file, filename) => {
console.log("Uploading: " + filename); console.log("Uploading: " + filename);
let params = { let params = {
Bucket: aws_credentials.bucketName, Bucket: aws_credentials.bucketName,
Key: req.params.id, Key: req.params.id,
@ -126,29 +137,16 @@ app.post("/upload/:id", (req, res, next) => {
if (err) { if (err) {
console.log(err, err.stack); // an error occurred console.log(err, err.stack); // an error occurred
} else { } 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 id = req.params.id;
let uuid = crypto.randomBytes(10).toString('hex'); let uuid = crypto.randomBytes(10).toString('hex');
redis_client.hmset([id, "filename", filename, "delete", uuid]); 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); redis_client.expire(id, 86400000);
console.log("Upload Finished of " + filename); console.log("Upload Finished of " + filename);
res.send(uuid); res.send(uuid);
}); }
})
}); });
}); });
}); });