| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import ErrorV3 from '@growi/core/src/models/vo/error-apiv3';
- import loggerFactory from '~/utils/logger';
- import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
- const logger = loggerFactory('growi:routes:apiv3:attachment'); // eslint-disable-line no-unused-vars
- const express = require('express');
- const router = express.Router();
- const { query } = require('express-validator');
- const { serializeUserSecurely } = require('../../models/serializers/user-serializer');
- /**
- * @swagger
- * tags:
- * name: Attachment
- */
- module.exports = (crowi) => {
- const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
- const loginRequired = require('../../middlewares/login-required')(crowi, true);
- const Page = crowi.model('Page');
- const User = crowi.model('User');
- const Attachment = crowi.model('Attachment');
- const validator = {
- retrieveAttachments: [
- query('pageId').isMongoId().withMessage('pageId is required'),
- query('pageNumber').optional().isInt().withMessage('pageNumber must be a number'),
- query('limit').optional().isInt({ max: 100 }).withMessage('You should set less than 100 or not to set limit.'),
- ],
- };
- /**
- * @swagger
- *
- * /attachment/list:
- * get:
- * tags: [Attachment]
- * description: Get attachment list
- * responses:
- * 200:
- * description: Return attachment list
- * parameters:
- * - name: page_id
- * in: query
- * required: true
- * description: page id
- * schema:
- * type: string
- */
- router.get('/list', accessTokenParser, loginRequired, validator.retrieveAttachments, apiV3FormValidator, async(req, res) => {
- const limit = req.query.limit || await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationS') || 10;
- const pageNumber = req.query.pageNumber || 1;
- const offset = (pageNumber - 1) * limit;
- try {
- const pageId = req.query.pageId;
- // check whether accessible
- const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
- if (!isAccessible) {
- const msg = 'Current user is not accessible to this page.';
- return res.apiv3Err(new ErrorV3(msg, 'attachment-list-failed'), 403);
- }
- // directly get paging-size from db. not to delivery from client side.
- const paginateResult = await Attachment.paginate(
- { page: pageId },
- {
- limit,
- offset,
- populate: 'creator',
- },
- );
- paginateResult.docs.forEach((doc) => {
- if (doc.creator != null && doc.creator instanceof User) {
- doc.creator = serializeUserSecurely(doc.creator);
- }
- });
- return res.apiv3({ paginateResult });
- }
- catch (err) {
- logger.error('Attachment not found', err);
- return res.apiv3Err(err, 500);
- }
- });
- return router;
- };
|