filelist.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const crypto = require('crypto');
  2. const config = require('../config');
  3. const storage = require('../storage');
  4. const Limiter = require('../limiter');
  5. function id(user, kid) {
  6. const sha = crypto.createHash('sha256');
  7. sha.update(user);
  8. sha.update(kid);
  9. const hash = sha.digest('hex');
  10. return `filelist-${hash}`;
  11. }
  12. module.exports = {
  13. async get(req, res) {
  14. const kid = req.params.id;
  15. try {
  16. const fileId = id(req.user, kid);
  17. const contentLength = await storage.length(fileId);
  18. const fileStream = await storage.get(fileId);
  19. res.writeHead(200, {
  20. 'Content-Type': 'application/octet-stream',
  21. 'Content-Length': contentLength
  22. });
  23. fileStream.pipe(res);
  24. } catch (e) {
  25. res.sendStatus(404);
  26. }
  27. },
  28. async post(req, res) {
  29. const kid = req.params.id;
  30. try {
  31. const limiter = new Limiter(1024 * 1024 * 10);
  32. const fileStream = req.pipe(limiter);
  33. await storage.set(
  34. id(req.user, kid),
  35. fileStream,
  36. null,
  37. config.max_expire_seconds
  38. );
  39. res.sendStatus(200);
  40. } catch (e) {
  41. if (e.message === 'limit') {
  42. return res.sendStatus(413);
  43. }
  44. res.sendStatus(500);
  45. }
  46. }
  47. };