attachment.js 2.9 KB

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