| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- const loggerFactory = require('@alias/logger');
- const logger = loggerFactory('growi:routes:apiv3:import'); // eslint-disable-line no-unused-vars
- const path = require('path');
- const multer = require('multer');
- // eslint-disable-next-line no-unused-vars
- const { ObjectId } = require('mongoose').Types;
- const express = require('express');
- const router = express.Router();
- /**
- * @swagger
- * tags:
- * name: Import
- */
- /**
- * @swagger
- *
- * components:
- * schemas:
- * ImportStatus:
- * type: object
- * properties:
- * zipFileStat:
- * type: object
- * description: the property object
- * progressList:
- * type: array
- * items:
- * type: object
- * description: progress data for each exporting collections
- * isImporting:
- * type: boolean
- * description: whether the current importing job exists or not
- */
- module.exports = (crowi) => {
- const { growiBridgeService, importService } = crowi;
- const accessTokenParser = require('../../middleware/access-token-parser')(crowi);
- const loginRequired = require('../../middleware/login-required')(crowi);
- const adminRequired = require('../../middleware/admin-required')(crowi);
- const csrf = require('../../middleware/csrf')(crowi);
- this.adminEvent = crowi.event('admin');
- // setup event
- this.adminEvent.on('onProgressForImport', (data) => {
- crowi.getIo().sockets.emit('admin:onProgressForImport', data);
- });
- this.adminEvent.on('onTerminateForImport', (data) => {
- crowi.getIo().sockets.emit('admin:onTerminateForImport', data);
- });
- const uploads = multer({
- storage: multer.diskStorage({
- destination: (req, file, cb) => {
- cb(null, importService.baseDir);
- },
- filename(req, file, cb) {
- // to prevent hashing the file name. files with same name will be overwritten.
- cb(null, file.originalname);
- },
- }),
- fileFilter: (req, file, cb) => {
- if (path.extname(file.originalname) === '.zip') {
- return cb(null, true);
- }
- cb(new Error('Only ".zip" is allowed'));
- },
- });
- /**
- * defined overwrite params for each collection
- * all imported documents are overwriten by this value
- * each value can be any value or a function (_value, { _document, key, schema }) { return newValue }
- *
- * @param {string} collectionName MongoDB collection name
- * @param {object} req request object
- * @return {object} document to be persisted
- */
- const overwriteParamsFn = (collectionName, schema, req) => {
- /* eslint-disable no-case-declarations */
- switch (collectionName) {
- case 'pages':
- // TODO: use schema and req to generate overwriteParams
- // e.g. { creator: schema.creator === 'me' ? ObjectId(req.user._id) : importService.keepOriginal }
- return {
- status: 'published', // FIXME when importing users and user groups
- grant: 1, // FIXME when importing users and user groups
- grantedUsers: [], // FIXME when importing users and user groups
- grantedGroup: null, // FIXME when importing users and user groups
- creator: ObjectId(req.user._id), // FIXME when importing users
- lastUpdateUser: ObjectId(req.user._id), // FIXME when importing users
- liker: [], // FIXME when importing users
- seenUsers: [], // FIXME when importing users
- commentCount: 0, // FIXME when importing comments
- extended: {}, // FIXME when ?
- pageIdOnHackmd: undefined, // FIXME when importing hackmd?
- revisionHackmdSynced: undefined, // FIXME when importing hackmd?
- hasDraftOnHackmd: undefined, // FIXME when importing hackmd?
- };
- // case 'revisoins':
- // return {};
- // case 'users':
- // return {};
- // ... add more cases
- default:
- return {};
- }
- /* eslint-enable no-case-declarations */
- };
- /**
- * @swagger
- *
- * /import/status:
- * get:
- * tags: [Import]
- * description: Get properties of stored zip files for import
- * responses:
- * 200:
- * description: the zip file statuses
- * content:
- * application/json:
- * schema:
- * properties:
- * status:
- * $ref: '#/components/schemas/ImportStatus'
- */
- router.get('/status', accessTokenParser, loginRequired, adminRequired, async(req, res) => {
- try {
- const status = await importService.getStatus();
- return res.apiv3(status);
- }
- catch (err) {
- return res.apiv3Err(err, 500);
- }
- });
- /**
- * @swagger
- *
- * /import:
- * post:
- * tags: [Import]
- * description: import a collection from a zipped json
- * parameters:
- * - name: fileName
- * in: path
- * description: the file name of zip file
- * required: true
- * schema:
- * type: string
- * - name: collections
- * description: collection names to import
- * required: true
- * schema:
- * type: array
- * items:
- * type: string
- * - name: optionsMap
- * description: the map object of importing option
- * schema:
- * type: object
- * responses:
- * 200:
- * description: the data is successfully imported
- * content:
- * application/json:
- * schema:
- * properties:
- * results:
- * type: array
- * items:
- * type: object
- * description: collectionName, insertedIds, failedIds
- */
- router.post('/', accessTokenParser, loginRequired, adminRequired, csrf, async(req, res) => {
- // TODO: add express validator
- const { fileName, collections, optionsMap } = req.body;
- const zipFile = importService.getFile(fileName);
- /*
- * unzip, parse
- */
- let meta = null;
- let fileStatsToImport = null;
- try {
- // unzip
- await importService.unzip(zipFile);
- // eslint-disable-next-line no-unused-vars
- const { meta: parsedMeta, fileStats, innerFileStats } = await growiBridgeService.parseZipFile(zipFile);
- meta = parsedMeta;
- // filter innerFileStats
- fileStatsToImport = innerFileStats.filter(({ fileName, collectionName, size }) => {
- return collections.includes(collectionName);
- });
- }
- catch (err) {
- logger.error(err);
- return res.apiv3Err(err, 500);
- }
- /*
- * validate with meta.json
- */
- try {
- importService.validate(meta);
- }
- catch (err) {
- logger.error(err);
- return res.apiv3Err(err);
- }
- // generate maps of ImportOptions to import
- const importOptionsMap = {};
- fileStatsToImport.forEach(({ fileName, collectionName }) => {
- const options = optionsMap[collectionName];
- // generate options
- const importOptions = importService.generateImportOptions(options.mode);
- importOptions.jsonFileName = fileName;
- importOptions.overwriteParams = overwriteParamsFn(collectionName, options.schema, req);
- importOptionsMap[collectionName] = importOptions;
- });
- /*
- * import
- */
- try {
- importService.import(collections, importOptionsMap);
- return res.apiv3();
- }
- catch (err) {
- logger.error(err);
- return res.apiv3Err(err, 500);
- }
- });
- /**
- * @swagger
- *
- * /import/upload:
- * post:
- * tags: [Import]
- * description: upload a zip file
- * responses:
- * 200:
- * description: the file is uploaded
- * content:
- * application/json:
- * schema:
- * properties:
- * meta:
- * type: object
- * description: the meta data of the uploaded file
- * fileName:
- * type: string
- * description: the base name of the uploaded file
- * fileStats:
- * type: array
- * items:
- * type: object
- * description: the property of each extracted file
- */
- router.post('/upload', uploads.single('file'), accessTokenParser, loginRequired, adminRequired, csrf, async(req, res) => {
- const { file } = req;
- const zipFile = importService.getFile(file.filename);
- try {
- const data = await growiBridgeService.parseZipFile(zipFile);
- // validate with meta.json
- importService.validate(data.meta);
- return res.apiv3(data);
- }
- catch (err) {
- // TODO: use ApiV3Error
- logger.error(err);
- return res.status(500).send({ status: 'ERROR' });
- }
- });
- /**
- * @swagger
- *
- * /import/all:
- * delete:
- * tags: [Import]
- * description: Delete all zip files
- * responses:
- * 200:
- * description: all files are deleted
- */
- router.delete('/all', accessTokenParser, loginRequired, adminRequired, csrf, async(req, res) => {
- try {
- importService.deleteAllZipFiles();
- return res.apiv3();
- }
- catch (err) {
- logger.error(err);
- return res.apiv3Err(err, 500);
- }
- });
- return router;
- };
|