attachments.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:attachments'); // eslint-disable-line no-unused-vars
  3. const express = require('express');
  4. const router = express.Router();
  5. const ErrorV3 = require('../../models/vo/error-apiv3');
  6. /**
  7. * @swagger
  8. * tags:
  9. * name: Attachment
  10. */
  11. module.exports = (crowi) => {
  12. const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
  13. const loginRequired = require('../../middlewares/login-required')(crowi);
  14. const Page = crowi.model('Page');
  15. const Attachment = crowi.model('Attachment');
  16. /**
  17. * @swagger
  18. *
  19. * /attachments/list:
  20. * get:
  21. * tags: [Attachment]
  22. * description: Get attachment list
  23. * responses:
  24. * 200:
  25. * description: Return attachment list
  26. * parameters:
  27. * - name: page_id
  28. * in: query
  29. * required: true
  30. * description: page id
  31. * schema:
  32. * type: string
  33. */
  34. router.get('/list', accessTokenParser, loginRequired, async(req, res) => {
  35. try {
  36. const pageId = req.query.pageId;
  37. // check whether accessible
  38. const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
  39. if (!isAccessible) {
  40. const msg = 'Current user is not accessible to this page.';
  41. return res.apiv3Err(new ErrorV3(msg, 'attachment-list-failed'), 403);
  42. }
  43. const attachments = await Attachment.find({ page: pageId });
  44. return res.apiv3({ attachments });
  45. }
  46. catch (err) {
  47. logger.error('Attachment not found', err);
  48. return res.apiv3Err(err, 500);
  49. }
  50. });
  51. return router;
  52. };