done.js 940 B

12345678910111213141516171819202122232425262728293031
  1. const storage = require('../storage');
  2. const { statDownloadEvent } = require('../amplitude');
  3. module.exports = async function(req, res) {
  4. try {
  5. const id = req.params.id;
  6. const meta = req.meta;
  7. const ttl = await storage.ttl(id);
  8. statDownloadEvent({
  9. id,
  10. ip: req.ip,
  11. owner: meta.owner,
  12. download_count: meta.dl,
  13. ttl,
  14. agent: req.ua.browser.name || req.ua.ua.substring(0, 6)
  15. });
  16. await storage.incrementField(id, 'dl');
  17. if (meta.dl + 1 >= meta.dlimit) {
  18. // Only dlimit number of tokens will be issued
  19. // after which /download/token will return 403
  20. // however the protocol doesn't prevent one token
  21. // from making all the downloads and assumes
  22. // clients are well behaved. If this becomes
  23. // a problem we can keep track of used tokens.
  24. await storage.kill(id);
  25. }
  26. res.sendStatus(200);
  27. } catch (e) {
  28. res.sendStatus(404);
  29. }
  30. };