some code cleanup, fixed bug with regex in app.js

This commit is contained in:
Abhinav Adduri 2017-06-01 10:05:09 -07:00
parent 065f3c2014
commit 76c337836d
5 changed files with 16 additions and 23 deletions

3
app.js
View File

@ -23,12 +23,13 @@ app.get("/download/:id", function(req, res) {
app.get("/assets/download/:id", function(req, res) { app.get("/assets/download/:id", function(req, res) {
let id = req.params.id;
if (!validateID(id)){ if (!validateID(id)){
res.send(404); res.send(404);
return; return;
} }
let id = req.params.id;
client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too
if (!reply) { if (!reply) {
res.sendStatus(404); res.sendStatus(404);

View File

@ -7,7 +7,6 @@
<body> <body>
<button onclick="download()">DOWNLOAD</button> <button onclick="download()">DOWNLOAD</button>
<p id="downloadProgress"></p>
<ul id="downloaded_files"> <ul id="downloaded_files">
</ul> </ul>

View File

@ -7,10 +7,22 @@ function download() {
var li = document.createElement("li"); var li = document.createElement("li");
var progress = document.createElement("p"); var progress = document.createElement("p");
li.appendChild(progress); li.appendChild(progress);
document.getElementById("downloaded_files").appendChild(li);
xhr.addEventListener("progress", returnBindedLI(li, progress)); xhr.addEventListener("progress", returnBindedLI(li, progress));
xhr.onload = function(e) { 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) { if (this.status == 200) {
let self = this; let self = this;
var blob = new Blob([this.response]); var blob = new Blob([this.response]);
@ -43,20 +55,12 @@ function download() {
key, key,
array) array)
.then(function(decrypted){ .then(function(decrypted){
var filename = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1];
var name = document.createElement("p");
name.innerHTML = filename;
li.insertBefore(name, li.firstChild);
document.getElementById("downloaded_files").appendChild(li);
var dataView = new DataView(decrypted); var dataView = new DataView(decrypted);
var blob = new Blob([dataView]); var blob = new Blob([dataView]);
var downloadUrl = URL.createObjectURL(blob); var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a"); var a = document.createElement("a");
a.href = downloadUrl; a.href = downloadUrl;
a.download = filename a.download = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1];
console.log(xhr.getResponseHeader("Content-Disposition"));
document.body.appendChild(a); document.body.appendChild(a);
a.click(); a.click();
}) })

View File

@ -11,8 +11,6 @@
<input type="file" onchange="onChange(event)" name="fileUploaded" /> <input type="file" onchange="onChange(event)" name="fileUploaded" />
</form> </form>
<p id="downloadProgress"></p>
<ul id="uploaded_files"> <ul id="uploaded_files">
</ul> </ul>

View File

@ -57,16 +57,7 @@ function onChange(event) {
link.innerHTML = "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); link.setAttribute("href", "http://localhost:3000/download/" + hex + "/#" + keydata.k);
// if (curr_name) {
// localStorage.setItem(file.name, curr_name + "," + hex);
// } else {
// localStorage.setItem(file.name, hex)
// }
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);
alert("Share this link with a friend: http://localhost:3000/download/" + hex + "/#" + keydata.k);
}) })
} }
}; };