attachment.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:attachment'); // eslint-disable-line no-unused-vars
  3. const express = require('express');
  4. const router = express.Router();
  5. const { query } = require('express-validator');
  6. const ErrorV3 = require('../../models/vo/error-apiv3');
  7. /**
  8. * @swagger
  9. * tags:
  10. * name: Attachment
  11. */
  12. module.exports = (crowi) => {
  13. const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
  14. const loginRequired = require('../../middlewares/login-required')(crowi);
  15. const Page = crowi.model('Page');
  16. const User = crowi.model('User');
  17. const Attachment = crowi.model('Attachment');
  18. const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
  19. const validator = {
  20. retrieveAttachments: [
  21. query('pageId').isMongoId().withMessage('pageId is required'),
  22. query('limit').if(value => value != null).isInt({ max: 100 }).withMessage('You should set less than 100 or not to set limit.'),
  23. ],
  24. };
  25. /**
  26. * @swagger
  27. *
  28. * /attachment/list:
  29. * get:
  30. * tags: [Attachment]
  31. * description: Get attachment list
  32. * responses:
  33. * 200:
  34. * description: Return attachment list
  35. * parameters:
  36. * - name: page_id
  37. * in: query
  38. * required: true
  39. * description: page id
  40. * schema:
  41. * type: string
  42. */
  43. router.get('/list', accessTokenParser, loginRequired, validator.retrieveAttachments, apiV3FormValidator, async(req, res) => {
  44. const limit = req.query.limit || await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationS') || 10;
  45. const page = req.query.page;
  46. const offset = (page - 1) * limit;
  47. try {
  48. const pageId = req.query.pageId;
  49. // check whether accessible
  50. const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
  51. if (!isAccessible) {
  52. const msg = 'Current user is not accessible to this page.';
  53. return res.apiv3Err(new ErrorV3(msg, 'attachment-list-failed'), 403);
  54. }
  55. // directly get paging-size from db. not to delivery from client side.
  56. const paginateResult = await Attachment.paginate(
  57. { page: pageId },
  58. {
  59. limit,
  60. offset,
  61. populate: {
  62. path: 'creator',
  63. select: User.USER_PUBLIC_FIELDS,
  64. },
  65. },
  66. );
  67. paginateResult.docs.forEach((doc) => {
  68. if (doc.creator != null && doc.creator instanceof User) {
  69. doc.creator = doc.creator.toObject();
  70. }
  71. });
  72. return res.apiv3({ paginateResult });
  73. }
  74. catch (err) {
  75. logger.error('Attachment not found', err);
  76. return res.apiv3Err(err, 500);
  77. }
  78. });
  79. return router;
  80. };