added browserify, refactored code to separate UI and network logic
This commit is contained in:
parent
c539ed6282
commit
51910b5fc9
111
app.js
111
app.js
@ -1,111 +0,0 @@
|
||||
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;
|
||||
}
|
132
frontend/src/download.js
Normal file
132
frontend/src/download.js
Normal file
@ -0,0 +1,132 @@
|
||||
const UIWrapper = require('./file').UIWrapper;
|
||||
|
||||
let download = () => {
|
||||
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("get", "/assets" + location.pathname.slice(0, -1), true);
|
||||
xhr.responseType = "blob";
|
||||
|
||||
let listelem = setupUI();
|
||||
xhr.addEventListener("progress", updateProgress.bind(null, listelem));
|
||||
|
||||
xhr.onload = function(e) {
|
||||
|
||||
// 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
|
||||
// after a get request with the unique id
|
||||
listelem.emit('name', xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1]);
|
||||
|
||||
if (this.status == 200) {
|
||||
let self = this;
|
||||
let blob = new Blob([this.response]);
|
||||
let arrayBuffer;
|
||||
let fileReader = new FileReader();
|
||||
fileReader.onload = function() {
|
||||
arrayBuffer = this.result;
|
||||
let array = new Uint8Array(arrayBuffer);
|
||||
salt = strToIv(location.pathname.slice(10, -1));
|
||||
|
||||
window.crypto.subtle.importKey(
|
||||
"jwk",
|
||||
{
|
||||
kty: "oct",
|
||||
k: location.hash.slice(1),
|
||||
alg: "A128CBC",
|
||||
ext: true,
|
||||
},
|
||||
{
|
||||
name: "AES-CBC",
|
||||
},
|
||||
true,
|
||||
["encrypt", "decrypt"])
|
||||
.then((key) => {
|
||||
return window.crypto.subtle.decrypt(
|
||||
{
|
||||
name: "AES-CBC",
|
||||
iv: salt,
|
||||
},
|
||||
key,
|
||||
array)
|
||||
})
|
||||
.then((decrypted) => {
|
||||
let dataView = new DataView(decrypted);
|
||||
let blob = new Blob([dataView]);
|
||||
let downloadUrl = URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = downloadUrl;
|
||||
a.download = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1];
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
})
|
||||
.catch((err) => {
|
||||
alert("This link is either invalid or has expired, or the uploader has deleted the file.");
|
||||
console.error(err);
|
||||
});
|
||||
};
|
||||
|
||||
fileReader.readAsArrayBuffer(blob);
|
||||
} else {
|
||||
alert("This link is either invalid or has expired, or the uploader has deleted the file.")
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
window.download = download;
|
||||
|
||||
let setupUI = () => {
|
||||
let li = document.createElement("li");
|
||||
let name = document.createElement("p");
|
||||
li.appendChild(name);
|
||||
|
||||
let progress = document.createElement("p");
|
||||
li.appendChild(progress);
|
||||
|
||||
document.getElementById("downloaded_files").appendChild(li);
|
||||
|
||||
return new UIWrapper(li, name, null, progress);
|
||||
}
|
||||
|
||||
let ivToStr = (iv) => {
|
||||
let hexStr = "";
|
||||
for (let i in iv) {
|
||||
if (iv[i] < 16) {
|
||||
hexStr += "0" + iv[i].toString(16);
|
||||
} else {
|
||||
hexStr += iv[i].toString(16);
|
||||
}
|
||||
}
|
||||
window.hexStr = hexStr;
|
||||
return hexStr;
|
||||
}
|
||||
|
||||
let strToIv = (str) => {
|
||||
let iv = new Uint8Array(16);
|
||||
for (let i = 0; i < str.length; i += 2) {
|
||||
iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16);
|
||||
}
|
||||
|
||||
return iv;
|
||||
}
|
||||
|
||||
let updateProgress = (UIelem, e) => {
|
||||
if (e.lengthComputable) {
|
||||
let percentComplete = Math.floor((e.loaded / e.total) * 100);
|
||||
UIelem.emit('progress', "Progress: " + percentComplete + "%");
|
||||
|
||||
if (percentComplete === 100) {
|
||||
let finished = document.createElement("p");
|
||||
finished.innerText = "Your download has finished.";
|
||||
UIelem.li.appendChild(finished);
|
||||
|
||||
let close = document.createElement("button");
|
||||
close.innerText = "Ok";
|
||||
close.addEventListener("click", () => {
|
||||
document.getElementById("downloaded_files").removeChild(UIelem.li);
|
||||
});
|
||||
|
||||
UIelem.li.appendChild(close);
|
||||
}
|
||||
}
|
||||
}
|
27
frontend/src/file.js
Normal file
27
frontend/src/file.js
Normal file
@ -0,0 +1,27 @@
|
||||
const EventEmitter = require('events');
|
||||
|
||||
class UIWrapper extends EventEmitter {
|
||||
constructor(li, name, link, progress) {
|
||||
super();
|
||||
this.li = li;
|
||||
this.name = name;
|
||||
this.link = link;
|
||||
this.progress = progress;
|
||||
|
||||
this.on("name", (filename) => {
|
||||
this.name.innerText = filename;
|
||||
});
|
||||
|
||||
this.on("link", (link) => {
|
||||
this.link.innerText = link;
|
||||
this.link.setAttribute('href', link);
|
||||
});
|
||||
|
||||
this.on("progress", (progress) => {
|
||||
this.progress.innerText = progress;
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
exports.UIWrapper = UIWrapper;
|
3
frontend/src/main.js
Normal file
3
frontend/src/main.js
Normal file
@ -0,0 +1,3 @@
|
||||
require('./upload');
|
||||
require('./download');
|
||||
require('./file');
|
142
frontend/src/upload.js
Normal file
142
frontend/src/upload.js
Normal file
@ -0,0 +1,142 @@
|
||||
const EventEmitter = require('events');
|
||||
const UIWrapper = require('./file').UIWrapper;
|
||||
|
||||
let onChange = (event) => {
|
||||
let file = event.target.files[0];
|
||||
let reader = new FileReader();
|
||||
reader.readAsArrayBuffer(file);
|
||||
|
||||
let random_iv = window.crypto.getRandomValues(new Uint8Array(16));
|
||||
let hex = ivToStr(random_iv);
|
||||
|
||||
reader.onload = function(event) {
|
||||
let self = this;
|
||||
window.crypto.subtle.generateKey({
|
||||
name: "AES-CBC",
|
||||
length: 128
|
||||
},
|
||||
true,
|
||||
["encrypt", "decrypt"])
|
||||
.then((key) => {
|
||||
let arrayBuffer = self.result;
|
||||
let array = new Uint8Array(arrayBuffer);
|
||||
|
||||
window.crypto.subtle.encrypt({
|
||||
name: "AES-CBC",
|
||||
iv: random_iv },
|
||||
key,
|
||||
array)
|
||||
.then(uploadFile.bind(null, file, hex, key))
|
||||
.catch((err) => console.error(err));
|
||||
|
||||
}).catch((err) => console.error(err));
|
||||
};
|
||||
}
|
||||
|
||||
window.onChange = onChange;
|
||||
|
||||
let uploadFile = (file, hex, key, encrypted) => {
|
||||
let dataView = new DataView(encrypted);
|
||||
let blob = new Blob([dataView], { type: file.type });
|
||||
|
||||
let fd = new FormData();
|
||||
fd.append("fname", file.name);
|
||||
fd.append("data", blob, file.name);
|
||||
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("post", "/upload/" + hex, true);
|
||||
|
||||
let listelem = setupUI();
|
||||
listelem.emit('name', file.name);
|
||||
xhr.upload.addEventListener("progress", updateProgress.bind(null, listelem));
|
||||
|
||||
xhr.onreadystatechange = () => {
|
||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
||||
window.crypto.subtle.exportKey("jwk", key).then((keydata) => {
|
||||
localStorage.setItem(hex, xhr.responseText);
|
||||
|
||||
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);
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(fd);
|
||||
}
|
||||
|
||||
let updateProgress = (UIelem, e) => {
|
||||
if (e.lengthComputable) {
|
||||
let percentComplete = Math.floor((e.loaded / e.total) * 100);
|
||||
UIelem.emit('progress', "Progress: " + percentComplete + "%")
|
||||
|
||||
if (percentComplete === 100) {
|
||||
let btn = document.createElement("button");
|
||||
btn.innerText = "Delete from server";
|
||||
btn.addEventListener("click", () => {
|
||||
let segments = UIelem.link.innerText.split("/");
|
||||
let key = segments[segments.length - 2];
|
||||
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("post", "/delete/" + key, true);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
if (!localStorage.getItem(key)) return;
|
||||
|
||||
xhr.send(JSON.stringify({delete_token: localStorage.getItem(key)}));
|
||||
|
||||
xhr.onreadystatechange = () => {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
document.getElementById("uploaded_files").removeChild(UIelem.li);
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
if (xhr.status === 200) {
|
||||
console.log("The file was successfully deleted.");
|
||||
} else {
|
||||
console.log("The file has expired, or has already been deleted.");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
UIelem.li.appendChild(btn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let setupUI = () => {
|
||||
let li = document.createElement("li");
|
||||
let name = document.createElement("p");
|
||||
li.appendChild(name);
|
||||
|
||||
let link = document.createElement("a");
|
||||
li.appendChild(link);
|
||||
|
||||
let progress = document.createElement("p");
|
||||
li.appendChild(progress);
|
||||
|
||||
document.getElementById("uploaded_files").appendChild(li);
|
||||
|
||||
return new UIWrapper(li, name, link, progress);
|
||||
}
|
||||
|
||||
let ivToStr = (iv) => {
|
||||
let hexStr = "";
|
||||
for (let i in iv) {
|
||||
if (iv[i] < 16) {
|
||||
hexStr += "0" + iv[i].toString(16);
|
||||
} else {
|
||||
hexStr += iv[i].toString(16);
|
||||
}
|
||||
}
|
||||
window.hexStr = hexStr;
|
||||
return hexStr;
|
||||
}
|
||||
|
||||
let strToIv = (str) => {
|
||||
let iv = new Uint8Array(16);
|
||||
for (let i = 0; i < str.length; i += 2) {
|
||||
iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16);
|
||||
}
|
||||
|
||||
return iv;
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
"scripts": {
|
||||
"format": "prettier --single-quote --write 'public/*.js' 'app.js'",
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node server/portal_server.js"
|
||||
"start": "watchify frontend/src/main.js -o public/bundle.js -d | node server/portal_server.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
@ -17,5 +17,9 @@
|
||||
"path": "^0.12.7",
|
||||
"prettier": "^1.3.1",
|
||||
"redis": "^2.7.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"browserify": "^14.4.0",
|
||||
"watchify": "^3.9.0"
|
||||
}
|
||||
}
|
||||
|
614
public/bundle.js
Normal file
614
public/bundle.js
Normal file
File diff suppressed because one or more lines are too long
@ -2,7 +2,7 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Download your file</title>
|
||||
<script type="text/javascript" src="/download.js"></script>
|
||||
<script type="text/javascript" src="/bundle.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
@ -1,135 +0,0 @@
|
||||
function download() {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('get', '/assets' + location.pathname.slice(0, -1), true);
|
||||
xhr.responseType = 'blob';
|
||||
|
||||
var li = document.createElement('li');
|
||||
var progress = document.createElement('p');
|
||||
li.appendChild(progress);
|
||||
document.getElementById('downloaded_files').appendChild(li);
|
||||
|
||||
xhr.addEventListener('progress', returnBindedLI(li, progress));
|
||||
|
||||
xhr.onload = function(e) {
|
||||
// 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
|
||||
// after a get request with the unique id
|
||||
var name = document.createElement('p');
|
||||
name.innerHTML = xhr
|
||||
.getResponseHeader('Content-Disposition')
|
||||
.match(/filename="(.+)"/)[1];
|
||||
li.insertBefore(name, li.firstChild);
|
||||
|
||||
if (this.status == 200) {
|
||||
let self = this;
|
||||
var blob = new Blob([this.response]);
|
||||
var arrayBuffer;
|
||||
var fileReader = new FileReader();
|
||||
fileReader.onload = function() {
|
||||
arrayBuffer = this.result;
|
||||
var array = new Uint8Array(arrayBuffer);
|
||||
salt = strToIv(location.pathname.slice(10, -1));
|
||||
|
||||
window.crypto.subtle
|
||||
.importKey(
|
||||
'jwk',
|
||||
{
|
||||
kty: 'oct',
|
||||
k: location.hash.slice(1),
|
||||
alg: 'A128CBC',
|
||||
ext: true
|
||||
},
|
||||
{
|
||||
name: 'AES-CBC'
|
||||
},
|
||||
true,
|
||||
['encrypt', 'decrypt']
|
||||
)
|
||||
.then(function(key) {
|
||||
window.crypto.subtle
|
||||
.decrypt(
|
||||
{
|
||||
name: 'AES-CBC',
|
||||
iv: salt
|
||||
},
|
||||
key,
|
||||
array
|
||||
)
|
||||
.then(function(decrypted) {
|
||||
var dataView = new DataView(decrypted);
|
||||
var blob = new Blob([dataView]);
|
||||
var downloadUrl = URL.createObjectURL(blob);
|
||||
var a = document.createElement('a');
|
||||
a.href = downloadUrl;
|
||||
a.download = xhr
|
||||
.getResponseHeader('Content-Disposition')
|
||||
.match(/filename="(.+)"/)[1];
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
})
|
||||
.catch(function(err) {
|
||||
alert(
|
||||
'This link is either invalid or has expired, or the uploader has deleted the file.'
|
||||
);
|
||||
console.error(err);
|
||||
});
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
};
|
||||
fileReader.readAsArrayBuffer(blob);
|
||||
} else {
|
||||
alert(
|
||||
'This link is either invalid or has expired, or the uploader has deleted the file.'
|
||||
);
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function ivToStr(iv) {
|
||||
let hexStr = '';
|
||||
for (var i in iv) {
|
||||
if (iv[i] < 16) {
|
||||
hexStr += '0' + iv[i].toString(16);
|
||||
} else {
|
||||
hexStr += iv[i].toString(16);
|
||||
}
|
||||
}
|
||||
window.hexStr = hexStr;
|
||||
return hexStr;
|
||||
}
|
||||
|
||||
function strToIv(str) {
|
||||
var iv = new Uint8Array(16);
|
||||
for (var i = 0; i < str.length; i += 2) {
|
||||
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
||||
}
|
||||
|
||||
return iv;
|
||||
}
|
||||
|
||||
function returnBindedLI(li, progress) {
|
||||
return function updateProgress(e) {
|
||||
if (e.lengthComputable) {
|
||||
var percentComplete = Math.floor(e.loaded / e.total * 100);
|
||||
progress.innerHTML = 'Progress: ' + percentComplete + '%';
|
||||
}
|
||||
|
||||
if (percentComplete === 100) {
|
||||
var finished = document.createElement('p');
|
||||
finished.innerHTML = 'Your download has finished.';
|
||||
li.appendChild(finished);
|
||||
|
||||
var close = document.createElement('button');
|
||||
close.innerHTML = 'Ok';
|
||||
close.addEventListener('click', function() {
|
||||
document.getElementById('downloaded_files').removeChild(li);
|
||||
});
|
||||
|
||||
li.appendChild(close);
|
||||
}
|
||||
};
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
function ProgressEmitter(name, uuid, type) {
|
||||
this.name = name;
|
||||
this.uuid = uuid;
|
||||
this.type = type;
|
||||
this.link = null;
|
||||
|
||||
this.emit = () => {
|
||||
|
||||
};
|
||||
}
|
@ -2,8 +2,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Firefox Fileshare</title>
|
||||
<script src="file.js"></script>
|
||||
<script src="upload.js"></script>
|
||||
<script src="bundle.js"></script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
164
public/upload.js
164
public/upload.js
@ -1,164 +0,0 @@
|
||||
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",
|
||||
length: 128
|
||||
},
|
||||
true,
|
||||
["encrypt", "decrypt"])
|
||||
.then((key) => {
|
||||
let arrayBuffer = self.result;
|
||||
let array = new Uint8Array(arrayBuffer);
|
||||
|
||||
window.crypto.subtle.encrypt({
|
||||
name: "AES-CBC",
|
||||
iv: random_iv },
|
||||
key,
|
||||
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);
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
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");
|
||||
li.appendChild(link);
|
||||
|
||||
var progress = document.createElement("p");
|
||||
li.appendChild(progress);
|
||||
|
||||
|
||||
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) {
|
||||
var curr_name = localStorage.getItem(file.name);
|
||||
|
||||
localStorage.setItem(hex, xhr.responseText);
|
||||
|
||||
prog.link = "http://localhost:3000/download/" + hex + "/#" + keydata.k;
|
||||
prog.emit();
|
||||
|
||||
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);
|
||||
// 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);
|
||||
});
|
||||
li.appendChild(link);
|
||||
|
||||
var progress = document.createElement("p");
|
||||
li.appendChild(progress);
|
||||
document.getElementById("uploaded_files").appendChild(li);
|
||||
}
|
||||
|
||||
function ivToStr(iv) {
|
||||
let hexStr = '';
|
||||
for (var i in iv) {
|
||||
if (iv[i] < 16) {
|
||||
hexStr += '0' + iv[i].toString(16);
|
||||
} else {
|
||||
hexStr += iv[i].toString(16);
|
||||
}
|
||||
}
|
||||
window.hexStr = hexStr;
|
||||
return hexStr;
|
||||
}
|
||||
|
||||
function strToIv(str) {
|
||||
var iv = new Uint8Array(16);
|
||||
for (var i = 0; i < str.length; i += 2) {
|
||||
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
||||
}
|
||||
|
||||
return iv;
|
||||
}
|
||||
|
||||
function returnBindedLI(a_element, name, link, li) {
|
||||
return function updateProgress(e) {
|
||||
if (e.lengthComputable) {
|
||||
var percentComplete = Math.floor(e.loaded / e.total * 100);
|
||||
a_element.innerHTML = 'Progress: ' + percentComplete + '%';
|
||||
|
||||
if (percentComplete === 100) {
|
||||
var btn = document.createElement('button');
|
||||
btn.innerHTML = 'Delete from server';
|
||||
btn.addEventListener('click', function() {
|
||||
var segments = link.innerHTML.split('/');
|
||||
var key = segments[segments.length - 2];
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('post', '/delete/' + key, true);
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
if (!localStorage.getItem(key)) return;
|
||||
|
||||
xhr.send(JSON.stringify({ delete_token: localStorage.getItem(key) }));
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
document.getElementById('uploaded_files').removeChild(li);
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
if (xhr.status === 200) {
|
||||
console.log('The file was successfully deleted.');
|
||||
} else {
|
||||
console.log('The file has expired, or has already been deleted.');
|
||||
}
|
||||
};
|
||||
});
|
||||
li.appendChild(btn);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
|
||||
const express = require("express")
|
||||
const busboy = require("connect-busboy");
|
||||
const path = require("path");
|
||||
@ -10,7 +9,7 @@ const app = express()
|
||||
const redis = require("redis"),
|
||||
client = redis.createClient();
|
||||
|
||||
client.on("error", function(err) {
|
||||
client.on("error", (err) => {
|
||||
console.log(err);
|
||||
})
|
||||
|
||||
@ -18,11 +17,11 @@ app.use(busboy());
|
||||
app.use(bodyParser.json());
|
||||
app.use(express.static(path.join(__dirname, "../public")));
|
||||
|
||||
app.get("/download/:id", function(req, res) {
|
||||
app.get("/download/:id", (req, res) => {
|
||||
res.sendFile(path.join(__dirname + "/../public/download.html"));
|
||||
});
|
||||
|
||||
app.get("/assets/download/:id", function(req, res) {
|
||||
app.get("/assets/download/:id", (req, res) => {
|
||||
|
||||
let id = req.params.id;
|
||||
if (!validateID(id)){
|
||||
@ -31,14 +30,14 @@ app.get("/assets/download/:id", function(req, res) {
|
||||
}
|
||||
|
||||
|
||||
client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too
|
||||
client.hget(id, "filename", (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) {
|
||||
res.download(__dirname + "/../static/" + id, reply, (err) => {
|
||||
if (!err) {
|
||||
client.del(id);
|
||||
fs.unlinkSync(__dirname + "/../static/" + id);
|
||||
@ -49,7 +48,7 @@ app.get("/assets/download/:id", function(req, res) {
|
||||
|
||||
});
|
||||
|
||||
app.post("/delete/:id", function(req, res) {
|
||||
app.post("/delete/:id", (req, res) => {
|
||||
let id = req.params.id;
|
||||
|
||||
if (!validateID(id)){
|
||||
@ -63,7 +62,7 @@ app.post("/delete/:id", function(req, res) {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
|
||||
client.hget(id, "delete", function(err, reply) {
|
||||
client.hget(id, "delete", (err, reply) => {
|
||||
if (!reply) {
|
||||
res.sendStatus(404);
|
||||
} else {
|
||||
@ -74,29 +73,29 @@ app.post("/delete/:id", function(req, res) {
|
||||
})
|
||||
});
|
||||
|
||||
app.post("/upload/:id", function (req, res, next) {
|
||||
app.post("/upload/:id", (req, res, next) => {
|
||||
|
||||
if (!validateID(req.params.id)){
|
||||
res.send(404);
|
||||
return;
|
||||
}
|
||||
|
||||
var fstream;
|
||||
let fstream;
|
||||
req.pipe(req.busboy);
|
||||
req.busboy.on("file", function (fieldname, file, filename) {
|
||||
req.busboy.on("file", (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 () {
|
||||
fstream.on("close", () => {
|
||||
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() {
|
||||
// setTimeout(() => {
|
||||
// fs.unlinkSync(__dirname + "/static/" + id);
|
||||
// }, 86400000);
|
||||
|
||||
@ -107,10 +106,10 @@ app.post("/upload/:id", function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(3000, function () {
|
||||
app.listen(3000, () => {
|
||||
console.log("Portal app listening on port 3000!")
|
||||
})
|
||||
|
||||
function validateID(route_id) {
|
||||
let validateID = (route_id) => {
|
||||
return route_id.match(/^[0-9a-fA-F]{32}$/) !== null;
|
||||
}
|
Loading…
Reference in New Issue
Block a user