refactoring code

This commit is contained in:
Abhinav Adduri 2017-06-01 13:14:14 -07:00 committed by Danny Coates
parent e3611ded17
commit c539ed6282
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
5 changed files with 225 additions and 95 deletions

View File

@ -5,7 +5,7 @@
"scripts": { "scripts": {
"format": "prettier --single-quote --write 'public/*.js' 'app.js'", "format": "prettier --single-quote --write 'public/*.js' 'app.js'",
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js" "start": "node server/portal_server.js"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",

10
public/file.js Normal file
View File

@ -0,0 +1,10 @@
function ProgressEmitter(name, uuid, type) {
this.name = name;
this.uuid = uuid;
this.type = type;
this.link = null;
this.emit = () => {
};
}

View File

@ -1,9 +1,9 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Firefox Fileshare</title> <title>Firefox Fileshare</title>
<script src="upload.js"></script> <script src="file.js"></script>
<script src="upload.js"></script>
</head> </head>
<body> <body>

View File

@ -1,101 +1,105 @@
function onChange(event) { function onChange(event) {
var file = event.target.files[0]; var file = event.target.files[0];
var reader = new FileReader(); var reader = new FileReader();
reader.readAsArrayBuffer(file);
let random_iv = window.crypto.getRandomValues(new Uint8Array(16));
var hex = ivToStr(random_iv);
reader.onload = function(event) { reader.onload = function(event) {
let self = this; let self = this;
window.crypto.subtle window.crypto.subtle.generateKey({
.generateKey( name: "AES-CBC",
{
name: 'AES-CBC',
length: 128 length: 128
}, },
true, true,
['encrypt', 'decrypt'] ["encrypt", "decrypt"])
) .then((key) => {
.then(function(key) { let arrayBuffer = self.result;
var arrayBuffer = self.result; let array = new Uint8Array(arrayBuffer);
var array = new Uint8Array(arrayBuffer);
var random_iv = window.crypto.getRandomValues(new Uint8Array(16)); window.crypto.subtle.encrypt({
name: "AES-CBC",
window.crypto.subtle iv: random_iv },
.encrypt(
{
name: 'AES-CBC',
iv: random_iv
},
key, key,
array array)
) .then(uploadFile.bind(null, file, hex, key))
.then(function(encrypted) { .catch((err) => console.error(err));
}).catch((err) => console.error(err));
};
}
let uploadFile = (file, hex, key, encrypted) => {
var dataView = new DataView(encrypted); var dataView = new DataView(encrypted);
var blob = new Blob([dataView], { type: file.type }); var blob = new Blob([dataView], { type: file.type });
var fd = new FormData(); var fd = new FormData();
fd.append('fname', file.name); fd.append("fname", file.name);
fd.append('data', blob, file.name); fd.append("data", blob, file.name);
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
var hex = ivToStr(random_iv);
xhr.open('post', '/upload/' + hex, true);
var li = document.createElement('li'); xhr.open("post", "/upload/" + hex, true);
var name = document.createElement('p');
let prog = new window.ProgressEmitter(file.name, hex, file.type);
var li = document.createElement("li");
var name = document.createElement("p");
name.innerHTML = file.name; name.innerHTML = file.name;
li.appendChild(name); li.appendChild(name);
var link = document.createElement('a'); var link = document.createElement("a");
li.appendChild(link); li.appendChild(link);
var progress = document.createElement('p'); var progress = document.createElement("p");
li.appendChild(progress); li.appendChild(progress);
document.getElementById('uploaded_files').appendChild(li);
xhr.upload.addEventListener(
'progress', document.getElementById("uploaded_files").appendChild(li);
returnBindedLI(progress, name, link, li)
);
xhr.upload.addEventListener("progress", returnBindedLI(progress, name, link, li));
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.readyState == XMLHttpRequest.DONE) {
window.crypto.subtle window.crypto.subtle.exportKey("jwk", key).then(function(keydata) {
.exportKey('jwk', key)
.then(function(keydata) {
var curr_name = localStorage.getItem(file.name); var curr_name = localStorage.getItem(file.name);
localStorage.setItem(hex, xhr.responseText); localStorage.setItem(hex, xhr.responseText);
link.innerHTML = prog.link = "http://localhost:3000/download/" + hex + "/#" + keydata.k;
'http://localhost:3000/download/' + prog.emit();
hex +
'/#' +
keydata.k;
link.setAttribute(
'href',
'http://localhost:3000/download/' + hex + '/#' + keydata.k
);
console.log( link.innerHTML = "http://localhost:3000/download/" + hex + "/#" + keydata.k;
'Share this link with a friend: http://localhost:3000/download/' + link.setAttribute("href", "http://localhost:3000/download/" + hex + "/#" + keydata.k);
hex +
'/#' + console.log("Share this link with a friend: http://localhost:3000/download/" + hex + "/#" + keydata.k);
keydata.k })
);
});
} }
}; };
xhr.send(fd); xhr.send(fd);
}) // setupLI(file.name);
.catch(function(err) {
console.error(err); }
function setupLI(emitter) {
var li = document.createElement("li");
var name = document.createElement("p");
name.innerHTML = emitter;
li.appendChild(name);
var link = document.createElement("a");
link.addEventListener('NotifyProgress', (progress_event) => {
console.log(progress_event);
}); });
}) li.appendChild(link);
.catch(function(err) {
console.error(err); var progress = document.createElement("p");
}); li.appendChild(progress);
}; document.getElementById("uploaded_files").appendChild(li);
reader.readAsArrayBuffer(file);
} }
function ivToStr(iv) { function ivToStr(iv) {

116
server/portal_server.js Normal file
View File

@ -0,0 +1,116 @@
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 app = express()
const redis = require("redis"),
client = redis.createClient();
client.on("error", function(err) {
console.log(err);
})
app.use(busboy());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "../public")));
app.get("/download/:id", function(req, res) {
res.sendFile(path.join(__dirname + "/../public/download.html"));
});
app.get("/assets/download/:id", function(req, res) {
let id = req.params.id;
if (!validateID(id)){
res.send(404);
return;
}
client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too
if (!reply) {
res.sendStatus(404);
} else {
res.setHeader("Content-Disposition", "attachment; filename=" + reply);
res.setHeader("Content-Type", "application/octet-stream");
res.download(__dirname + "/../static/" + id, reply, function(err) {
if (!err) {
client.del(id);
fs.unlinkSync(__dirname + "/../static/" + id);
}
});
}
})
});
app.post("/delete/:id", function(req, res) {
let id = req.params.id;
if (!validateID(id)){
res.send(404);
return;
}
let delete_token = req.body.delete_token;
if (!delete_token){
res.sendStatus(404);
}
client.hget(id, "delete", function(err, reply) {
if (!reply) {
res.sendStatus(404);
} else {
client.del(id);
fs.unlinkSync(__dirname + "/../static/" + id);
res.sendStatus(200);
}
})
});
app.post("/upload/:id", function (req, res, next) {
if (!validateID(req.params.id)){
res.send(404);
return;
}
var fstream;
req.pipe(req.busboy);
req.busboy.on("file", function (fieldname, file, filename) {
console.log("Uploading: " + filename);
//Path where image will be uploaded
fstream = fs.createWriteStream(__dirname + "/../static/" + req.params.id);
file.pipe(fstream);
fstream.on("close", function () {
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
// setTimeout(function() {
// fs.unlinkSync(__dirname + "/static/" + id);
// }, 86400000);
client.expire(id, 86400000);
console.log("Upload Finished of " + filename);
res.send(uuid);
});
});
});
app.listen(3000, function () {
console.log("Portal app listening on port 3000!")
})
function validateID(route_id) {
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null;
}