changed file name, uncommitted bundle

This commit is contained in:
Abhinav Adduri 2017-06-01 15:12:30 -07:00 committed by Danny Coates
parent 51910b5fc9
commit dd703b228a
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
7 changed files with 1998 additions and 749 deletions

View File

@ -1,21 +1,22 @@
const UIWrapper = require('./file').UIWrapper; const UIWrapper = require('./ui').UIWrapper;
let download = () => { let download = () => {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open("get", "/assets" + location.pathname.slice(0, -1), true); xhr.open('get', '/assets' + location.pathname.slice(0, -1), true);
xhr.responseType = "blob"; xhr.responseType = 'blob';
let listelem = setupUI(); let listelem = setupUI();
xhr.addEventListener("progress", updateProgress.bind(null, listelem)); xhr.addEventListener('progress', updateProgress.bind(null, listelem));
xhr.onload = function(e) { xhr.onload = function(e) {
// maybe send a separate request before this one to get the filename? // maybe send a separate request before this one to get the filename?
// maybe render the html itself with the filename, since it's generated server side // maybe render the html itself with the filename, since it's generated server side
// after a get request with the unique id // after a get request with the unique id
listelem.emit('name', xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1]); listelem.emit(
'name',
xhr.getResponseHeader('Content-Disposition').match(/filename="(.+)"/)[1]
);
if (this.status == 200) { if (this.status == 200) {
let self = this; let self = this;
@ -27,106 +28,115 @@ let download = () => {
let array = new Uint8Array(arrayBuffer); let array = new Uint8Array(arrayBuffer);
salt = strToIv(location.pathname.slice(10, -1)); salt = strToIv(location.pathname.slice(10, -1));
window.crypto.subtle.importKey( window.crypto.subtle
"jwk", .importKey(
'jwk',
{ {
kty: "oct", kty: 'oct',
k: location.hash.slice(1), k: location.hash.slice(1),
alg: "A128CBC", alg: 'A128CBC',
ext: true, ext: true
}, },
{ {
name: "AES-CBC", name: 'AES-CBC'
}, },
true, true,
["encrypt", "decrypt"]) ['encrypt', 'decrypt']
.then((key) => { )
.then(key => {
return window.crypto.subtle.decrypt( return window.crypto.subtle.decrypt(
{ {
name: "AES-CBC", name: 'AES-CBC',
iv: salt, iv: salt
}, },
key, key,
array) array
);
}) })
.then((decrypted) => { .then(decrypted => {
let dataView = new DataView(decrypted); let dataView = new DataView(decrypted);
let blob = new Blob([dataView]); let blob = new Blob([dataView]);
let downloadUrl = URL.createObjectURL(blob); let downloadUrl = URL.createObjectURL(blob);
let a = document.createElement("a"); let a = document.createElement('a');
a.href = downloadUrl; a.href = downloadUrl;
a.download = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1]; a.download = xhr
.getResponseHeader('Content-Disposition')
.match(/filename="(.+)"/)[1];
document.body.appendChild(a); document.body.appendChild(a);
a.click(); a.click();
}) })
.catch((err) => { .catch(err => {
alert("This link is either invalid or has expired, or the uploader has deleted the file."); alert(
'This link is either invalid or has expired, or the uploader has deleted the file.'
);
console.error(err); console.error(err);
}); });
}; };
fileReader.readAsArrayBuffer(blob); fileReader.readAsArrayBuffer(blob);
} else { } else {
alert("This link is either invalid or has expired, or the uploader has deleted the file.") alert(
'This link is either invalid or has expired, or the uploader has deleted the file.'
);
} }
}; };
xhr.send(); xhr.send();
} };
window.download = download; window.download = download;
let setupUI = () => { let setupUI = () => {
let li = document.createElement("li"); let li = document.createElement('li');
let name = document.createElement("p"); let name = document.createElement('p');
li.appendChild(name); li.appendChild(name);
let progress = document.createElement("p"); let progress = document.createElement('p');
li.appendChild(progress); li.appendChild(progress);
document.getElementById("downloaded_files").appendChild(li); document.getElementById('downloaded_files').appendChild(li);
return new UIWrapper(li, name, null, progress); return new UIWrapper(li, name, null, progress);
} };
let ivToStr = (iv) => { let ivToStr = iv => {
let hexStr = ""; let hexStr = '';
for (let i in iv) { for (let i in iv) {
if (iv[i] < 16) { if (iv[i] < 16) {
hexStr += "0" + iv[i].toString(16); hexStr += '0' + iv[i].toString(16);
} else { } else {
hexStr += iv[i].toString(16); hexStr += iv[i].toString(16);
} }
} }
window.hexStr = hexStr; window.hexStr = hexStr;
return hexStr; return hexStr;
} };
let strToIv = (str) => { let strToIv = str => {
let iv = new Uint8Array(16); let iv = new Uint8Array(16);
for (let i = 0; i < str.length; i += 2) { for (let i = 0; i < str.length; i += 2) {
iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16); iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
} }
return iv; return iv;
} };
let updateProgress = (UIelem, e) => { let updateProgress = (UIelem, e) => {
if (e.lengthComputable) { if (e.lengthComputable) {
let percentComplete = Math.floor((e.loaded / e.total) * 100); let percentComplete = Math.floor(e.loaded / e.total * 100);
UIelem.emit('progress', "Progress: " + percentComplete + "%"); UIelem.emit('progress', 'Progress: ' + percentComplete + '%');
if (percentComplete === 100) { if (percentComplete === 100) {
let finished = document.createElement("p"); let finished = document.createElement('p');
finished.innerText = "Your download has finished."; finished.innerText = 'Your download has finished.';
UIelem.li.appendChild(finished); UIelem.li.appendChild(finished);
let close = document.createElement("button"); let close = document.createElement('button');
close.innerText = "Ok"; close.innerText = 'Ok';
close.addEventListener("click", () => { close.addEventListener('click', () => {
document.getElementById("downloaded_files").removeChild(UIelem.li); document.getElementById('downloaded_files').removeChild(UIelem.li);
}); });
UIelem.li.appendChild(close); UIelem.li.appendChild(close);
} }
} }
} };

View File

@ -1,3 +1,3 @@
require('./upload'); require('./upload');
require('./download'); require('./download');
require('./file'); require('./ui');

View File

@ -8,18 +8,17 @@ class UIWrapper extends EventEmitter {
this.link = link; this.link = link;
this.progress = progress; this.progress = progress;
this.on("name", (filename) => { this.on('name', filename => {
this.name.innerText = filename; this.name.innerText = filename;
}); });
this.on("link", (link) => { this.on('link', link => {
this.link.innerText = link; this.link.innerText = link;
this.link.setAttribute('href', link); this.link.setAttribute('href', link);
}); });
this.on("progress", (progress) => { this.on('progress', progress => {
this.progress.innerText = progress; this.progress.innerText = progress;
}); });
} }
} }

View File

@ -1,7 +1,7 @@
const EventEmitter = require('events'); const EventEmitter = require('events');
const UIWrapper = require('./file').UIWrapper; const UIWrapper = require('./ui').UIWrapper;
let onChange = (event) => { let onChange = event => {
let file = event.target.files[0]; let file = event.target.files[0];
let reader = new FileReader(); let reader = new FileReader();
reader.readAsArrayBuffer(file); reader.readAsArrayBuffer(file);
@ -11,27 +11,34 @@ let onChange = (event) => {
reader.onload = function(event) { reader.onload = function(event) {
let self = this; let self = this;
window.crypto.subtle.generateKey({ window.crypto.subtle
name: "AES-CBC", .generateKey(
{
name: 'AES-CBC',
length: 128 length: 128
}, },
true, true,
["encrypt", "decrypt"]) ['encrypt', 'decrypt']
.then((key) => { )
.then(key => {
let arrayBuffer = self.result; let arrayBuffer = self.result;
let array = new Uint8Array(arrayBuffer); let array = new Uint8Array(arrayBuffer);
window.crypto.subtle.encrypt({ window.crypto.subtle
name: "AES-CBC", .encrypt(
iv: random_iv }, {
name: 'AES-CBC',
iv: random_iv
},
key, key,
array) array
)
.then(uploadFile.bind(null, file, hex, key)) .then(uploadFile.bind(null, file, hex, key))
.catch((err) => console.error(err)); .catch(err => console.error(err));
})
}).catch((err) => console.error(err)); .catch(err => console.error(err));
};
}; };
}
window.onChange = onChange; window.onChange = onChange;
@ -40,103 +47,110 @@ let uploadFile = (file, hex, key, encrypted) => {
let blob = new Blob([dataView], { type: file.type }); let blob = new Blob([dataView], { type: file.type });
let fd = new FormData(); let 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);
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open("post", "/upload/" + hex, true); xhr.open('post', '/upload/' + hex, true);
let listelem = setupUI(); let listelem = setupUI();
listelem.emit('name', file.name); listelem.emit('name', file.name);
xhr.upload.addEventListener("progress", updateProgress.bind(null, listelem)); xhr.upload.addEventListener('progress', updateProgress.bind(null, listelem));
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.readyState == XMLHttpRequest.DONE) {
window.crypto.subtle.exportKey("jwk", key).then((keydata) => { window.crypto.subtle.exportKey('jwk', key).then(keydata => {
localStorage.setItem(hex, xhr.responseText); localStorage.setItem(hex, xhr.responseText);
listelem.emit('link', "http://localhost:3000/download/" + hex + "/#" + keydata.k) listelem.emit(
'link',
'http://localhost:3000/download/' + hex + '/#' + keydata.k
);
console.log("Share this link with a friend: 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); xhr.send(fd);
} };
let updateProgress = (UIelem, e) => { let updateProgress = (UIelem, e) => {
if (e.lengthComputable) { if (e.lengthComputable) {
let percentComplete = Math.floor((e.loaded / e.total) * 100); let percentComplete = Math.floor(e.loaded / e.total * 100);
UIelem.emit('progress', "Progress: " + percentComplete + "%") UIelem.emit('progress', 'Progress: ' + percentComplete + '%');
if (percentComplete === 100) { if (percentComplete === 100) {
let btn = document.createElement("button"); let btn = document.createElement('button');
btn.innerText = "Delete from server"; btn.innerText = 'Delete from server';
btn.addEventListener("click", () => { btn.addEventListener('click', () => {
let segments = UIelem.link.innerText.split("/"); let segments = UIelem.link.innerText.split('/');
let key = segments[segments.length - 2]; let key = segments[segments.length - 2];
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open("post", "/delete/" + key, true); xhr.open('post', '/delete/' + key, true);
xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader('Content-Type', 'application/json');
if (!localStorage.getItem(key)) return; if (!localStorage.getItem(key)) return;
xhr.send(JSON.stringify({ delete_token: localStorage.getItem(key) })); xhr.send(JSON.stringify({ delete_token: localStorage.getItem(key) }));
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.readyState === XMLHttpRequest.DONE) {
document.getElementById("uploaded_files").removeChild(UIelem.li); document.getElementById('uploaded_files').removeChild(UIelem.li);
localStorage.removeItem(key); localStorage.removeItem(key);
} }
if (xhr.status === 200) { if (xhr.status === 200) {
console.log("The file was successfully deleted."); console.log('The file was successfully deleted.');
} else { } else {
console.log("The file has expired, or has already been deleted."); console.log('The file has expired, or has already been deleted.');
} }
} };
}); });
UIelem.li.appendChild(btn); UIelem.li.appendChild(btn);
} }
} }
} };
let setupUI = () => { let setupUI = () => {
let li = document.createElement("li"); let li = document.createElement('li');
let name = document.createElement("p"); let name = document.createElement('p');
li.appendChild(name); li.appendChild(name);
let link = document.createElement("a"); let link = document.createElement('a');
li.appendChild(link); li.appendChild(link);
let progress = document.createElement("p"); let progress = document.createElement('p');
li.appendChild(progress); li.appendChild(progress);
document.getElementById("uploaded_files").appendChild(li); document.getElementById('uploaded_files').appendChild(li);
return new UIWrapper(li, name, link, progress); return new UIWrapper(li, name, link, progress);
} };
let ivToStr = (iv) => { let ivToStr = iv => {
let hexStr = ""; let hexStr = '';
for (let i in iv) { for (let i in iv) {
if (iv[i] < 16) { if (iv[i] < 16) {
hexStr += "0" + iv[i].toString(16); hexStr += '0' + iv[i].toString(16);
} else { } else {
hexStr += iv[i].toString(16); hexStr += iv[i].toString(16);
} }
} }
window.hexStr = hexStr; window.hexStr = hexStr;
return hexStr; return hexStr;
} };
let strToIv = (str) => { let strToIv = str => {
let iv = new Uint8Array(16); let iv = new Uint8Array(16);
for (let i = 0; i < str.length; i += 2) { for (let i = 0; i < str.length; i += 2) {
iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16); iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
} }
return iv; return iv;
} };

1840
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"scripts": { "scripts": {
"format": "prettier --single-quote --write 'public/*.js' 'app.js'", "format": "prettier --single-quote --write 'frontend/src/*.js'",
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "watchify frontend/src/main.js -o public/bundle.js -d | node server/portal_server.js" "start": "watchify frontend/src/main.js -o public/bundle.js -d | node server/portal_server.js"
}, },

File diff suppressed because one or more lines are too long