24
1
Fork 0

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) {
let id = req.params.id;
if (!validateID(id)){
res.send(404);
return;
}
let id = req.params.id;
client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too
if (!reply) {
res.sendStatus(404);

View File

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

View File

@ -7,10 +7,22 @@ function download() {
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]);
@ -43,20 +55,12 @@ function download() {
key,
array)
.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 blob = new Blob([dataView]);
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = filename
console.log(xhr.getResponseHeader("Content-Disposition"));
a.download = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1];
document.body.appendChild(a);
a.click();
})

View File

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

View File

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