Repository: mozilla/send
File: app.js (JavaScript)

Code before:
client.on('error', function(err) {
  console.log(err);
})

app.use(busboy());
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function (req, res) {
  res.send('Hello World!')
})

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;
  client.hget(id, "filename", function(err, reply) { // maybe some expiration logic too
  if (!reply) {
      res.send('This link has expired!');
    } else {
      res.setHeader('Content-Disposition', 'attachment; filename=' + reply);
      res.setHeader('Content-Type', 'application/octet-stream');
      client.del(id);
      res.download(__dirname + '/static/' + id, reply);
    }
  })
  
});

app.route('/upload/:id')
    .post(function (req, res, next) {

        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;
                client.hset(id, "filename", filename, redis.print);
                client.hset(id, "expiration", 0, redis.print);
                client.expire(id, 86400000);
                console.log("Upload Finished of " + filename);           
                res.send(id);
            });
        });

Review comment (bug):
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:
```js
res.download(__dirname + '/static/' + id, reply, function (err) {
  if (!err) {
    client.del(id)
    fs.unlinkSync(__dirname + '/static/' + id)
  }
  // log the error
})
```

Code after:
client.on('error', function(err) {
  console.log(err);
})

app.use(busboy());
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function (req, res) {
  res.send('Hello World!')
})

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;
  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.route('/upload/:id')
    .post(function (req, res, next) {

        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;
                client.hset(id, "filename", filename, redis.print);
                client.hset(id, "expiration", 0, redis.print);