Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Changed naming for some pages, no longer stores files by name on server #25

Merged
merged 6 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ app.get('/assets/download/:id', function(req, res) {
let id = req.params.id;
client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too
if (!reply) {
res.send('error');
res.send('This link has expired!');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return a 404 in this case.

} else {
res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
res.setHeader('Content-Type', 'application/octet-stream');

res.download(__dirname + '/static/' + reply);
client.del(id);
res.download(__dirname + '/static/' + id, reply);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move the client.del(id) into the callback of download. We'll get an error there if something went wrong, and at that point we can also delete the file off the disk.

Something like:

res.download(__dirname + '/static/' + id, reply, function (err) {
  if (!err) {
    client.del(id)
    fs.unlinkSync(__dirname + '/static/' + id)
  }
  // log the error
})

}
})

Expand All @@ -46,13 +46,14 @@ app.route('/upload/:id')
console.log("Uploading: " + filename);

//Path where image will be uploaded
fstream = fs.createWriteStream(__dirname + '/static/' + filename);
fstream = fs.createWriteStream(__dirname + '/static/' + req.params.id);
file.pipe(fstream);
fstream.on('close', function () {
let id = req.params.id;
client.hset(id, "filename", filename, redis.print);
client.hset(id, "expiration", 0, redis.print);
console.log("Upload Finished of " + filename);
client.expire(id, 86400000);
console.log("Upload Finished of " + filename);
res.send(id);
});
});
Expand Down
2 changes: 1 addition & 1 deletion public/download.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<title>Download your file</title>
<script type="text/javascript" src="/file.js"></script>
</head>
<body>
Expand Down
4 changes: 3 additions & 1 deletion public/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ function download() {
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = downloadUrl;
a.download = xhr.getResponseHeader('Content-Disposition').match(/filename="(.+)"/)[1];;
a.download = xhr.getResponseHeader('Content-Disposition').match(/filename="(.+)"/)[1];
console.log(xhr.getResponseHeader('Content-Disposition'));
document.body.appendChild(a);
a.click();
})
.catch(function(err){
alert('This link is either invalid or has expired.');
console.error(err);
});
})
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<title>Firefox Fileshare</title>
<script src="file.js"></script>

</head>
Expand Down
2 changes: 1 addition & 1 deletion static/info.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is where files will go.
This is where downloaded files are stored.