pages.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const routes = require('../../app/routes');
  2. const storage = require('../storage');
  3. const state = require('../state');
  4. function stripEvents(str) {
  5. // For CSP we need to remove all the event handler placeholders.
  6. // It's ok, app.js will add them when it attaches to the DOM.
  7. return str.replace(/\son\w+=""/g, '');
  8. }
  9. module.exports = {
  10. index: async function(req, res) {
  11. const appState = await state(req);
  12. res.send(stripEvents(routes().toString('/blank', appState)));
  13. },
  14. blank: async function(req, res) {
  15. const appState = await state(req);
  16. res.send(stripEvents(routes().toString('/blank', appState)));
  17. },
  18. download: async function(req, res, next) {
  19. const id = req.params.id;
  20. const appState = await state(req);
  21. try {
  22. const { nonce, pwd } = await storage.metadata(id);
  23. res.set('WWW-Authenticate', `send-v1 ${nonce}`);
  24. res.send(
  25. stripEvents(
  26. routes().toString(
  27. `/download/${id}`,
  28. Object.assign(appState, {
  29. downloadMetadata: { nonce, pwd }
  30. })
  31. )
  32. )
  33. );
  34. } catch (e) {
  35. next();
  36. }
  37. },
  38. unsupported: async function(req, res) {
  39. const appState = await state(req);
  40. res.send(
  41. stripEvents(
  42. routes().toString(`/unsupported/${req.params.reason}`, appState)
  43. )
  44. );
  45. },
  46. notfound: async function(req, res) {
  47. const appState = await state(req);
  48. res
  49. .status(404)
  50. .send(
  51. stripEvents(
  52. routes().toString(
  53. '/404',
  54. Object.assign(appState, { downloadMetadata: { status: 404 } })
  55. )
  56. )
  57. );
  58. }
  59. };