certify-shared-file.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const loggerFactory = require('@alias/logger');
  2. const url = require('url');
  3. const logger = loggerFactory('growi:middleware:certify-shared-fire');
  4. module.exports = (crowi) => {
  5. return async(req, res, next) => {
  6. const { referer } = req.headers;
  7. const {path} = url.parse(referer)
  8. if (!path.startsWith('/share/')) {
  9. next();
  10. }
  11. const fileId = req.params.id || null;
  12. const Attachment = crowi.model('Attachment');
  13. const ShareLink = crowi.model('ShareLink');
  14. const attachment = await Attachment.findOne({ _id: fileId });
  15. if (attachment == null) {
  16. next();
  17. }
  18. const shareLinks = await ShareLink.find({ relatedPage: attachment.page });
  19. // If sharelinks don't exist, skip it
  20. if (shareLinks.length === 0) {
  21. next();
  22. }
  23. // Is there a valid share link
  24. shareLinks.map((sharelink) => {
  25. if (!sharelink.isExpired()) {
  26. logger.debug('Confirmed target file belong to a share page');
  27. req.isSharedPage = true;
  28. }
  29. return;
  30. });
  31. next();
  32. };
  33. };