Merge pull request #25 from mozilla/download_from_url

Changed naming for some pages, no longer stores files by name on server
This commit is contained in:
Danny Coates 2017-05-31 10:16:08 -07:00 committed by GitHub
commit 1163f2b65b
5 changed files with 17 additions and 9 deletions

12
app.js
View File

@ -26,12 +26,17 @@ app.get('/assets/download/:id', function(req, res) {
let id = req.params.id; 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.send('error'); res.sendStatus(404);
} else { } else {
res.setHeader('Content-Disposition', 'attachment; filename=' + reply); res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
res.setHeader('Content-Type', 'application/octet-stream'); res.setHeader('Content-Type', 'application/octet-stream');
res.download(__dirname + '/static/' + reply); res.download(__dirname + '/static/' + id, reply, function(err) {
if (!err) {
client.del(id);
fs.unlinkSync(__dirname + '/static/' + id);
}
});
} }
}) })
@ -46,12 +51,13 @@ app.route('/upload/:id')
console.log("Uploading: " + filename); console.log("Uploading: " + filename);
//Path where image will be uploaded //Path where image will be uploaded
fstream = fs.createWriteStream(__dirname + '/static/' + filename); fstream = fs.createWriteStream(__dirname + '/static/' + req.params.id);
file.pipe(fstream); file.pipe(fstream);
fstream.on('close', function () { fstream.on('close', function () {
let id = req.params.id; let id = req.params.id;
client.hset(id, "filename", filename, redis.print); client.hset(id, "filename", filename, redis.print);
client.hset(id, "expiration", 0, redis.print); client.hset(id, "expiration", 0, redis.print);
client.expire(id, 86400000);
console.log("Upload Finished of " + filename); console.log("Upload Finished of " + filename);
res.send(id); res.send(id);
}); });

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Page Title</title> <title>Download your file</title>
<script type="text/javascript" src="/file.js"></script> <script type="text/javascript" src="/file.js"></script>
</head> </head>
<body> <body>

View File

@ -44,11 +44,13 @@ function download() {
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 = 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); document.body.appendChild(a);
a.click(); a.click();
}) })
.catch(function(err){ .catch(function(err){
alert('This link is either invalid or has expired.');
console.error(err); console.error(err);
}); });
}) })

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Page Title</title> <title>Firefox Fileshare</title>
<script src="file.js"></script> <script src="file.js"></script>
</head> </head>

View File

@ -1 +1 @@
This is where files will go. This is where downloaded files are stored.