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": {
"format": "prettier --single-quote --write 'public/*.js' 'app.js'",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
"start": "node server/portal_server.js"
},
"author": "",
"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>
<html>
<head>
<title>Firefox Fileshare</title>
<script src="upload.js"></script>
<title>Firefox Fileshare</title>
<script src="file.js"></script>
<script src="upload.js"></script>
</head>
<body>

View File

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