mizozobu пре 6 година
родитељ
комит
964a464246

+ 12 - 0
src/server/crowi/index.js

@@ -47,6 +47,7 @@ function Crowi(rootdir) {
   this.fileUploadService = null;
   this.restQiitaAPIService = null;
   this.exportService = null;
+  this.importService = null;
   this.cdnResourcesService = new CdnResourcesService();
   this.interceptorManager = new InterceptorManager();
   this.xss = new Xss();
@@ -105,6 +106,7 @@ Crowi.prototype.init = async function() {
     this.setUpRestQiitaAPI(),
     this.setupUserGroup(),
     this.setupExport(),
+    this.setupImport(),
   ]);
 
   // globalNotification depends on slack and mailer
@@ -137,6 +139,9 @@ Crowi.prototype.initForTest = async function() {
     this.setUpAcl(),
   //   this.setUpCustomize(),
   //   this.setUpRestQiitaAPI(),
+  //   this.setupUserGroup(),
+  //   this.setupExport(),
+  //   this.setupImport(),
   ]);
 
   // globalNotification depends on slack and mailer
@@ -541,4 +546,11 @@ Crowi.prototype.setupExport = async function() {
   }
 };
 
+Crowi.prototype.setupImport = async function() {
+  const ImportService = require('../service/import');
+  if (this.importService == null) {
+    this.importService = new ImportService(this);
+  }
+};
+
 module.exports = Crowi;

+ 57 - 0
src/server/routes/apiv3/import.js

@@ -0,0 +1,57 @@
+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');
+const autoReap = require('multer-autoreap');
+
+const express = require('express');
+
+const router = express.Router();
+
+/**
+ * @swagger
+ *  tags:
+ *    name: Import
+ */
+
+module.exports = (crowi) => {
+  const { importService } = crowi;
+  const { Page } = crowi.models;
+  const uploads = multer({ dest: `${crowi.tmpDir}uploads` });
+
+  /**
+   * @swagger
+   *
+   *  /export/pages:
+   *    post:
+   *      tags: [Import]
+   *      description: import a collection from a zipped json for page collection
+   *      produces:
+   *        - application/json
+   *      responses:
+   *        200:
+   *          description: data is successfully imported
+   *          content:
+   *            application/json:
+   */
+  router.post('/pages', uploads.single('file'), autoReap, async(req, res) => {
+    // TODO: rename path to "/:collection" and add express validator
+    const { file } = req;
+    const zipFilePath = path.join(file.destination, file.filename);
+
+    try {
+      await importService.importFromZip(Page, zipFilePath);
+
+      // TODO: use res.apiv3
+      return res.send({ status: 'OK' });
+    }
+    catch (err) {
+      // TODO: use ApiV3Error
+      logger.error(err);
+      return res.status(500).send({ status: 'ERROR' });
+    }
+  });
+
+  return router;
+};

+ 2 - 0
src/server/routes/apiv3/index.js

@@ -11,5 +11,7 @@ module.exports = (crowi) => {
 
   router.use('/export', require('./export')(crowi));
 
+  router.use('/import', require('./import')(crowi));
+
   return router;
 };

+ 34 - 0
src/server/service/import.js

@@ -0,0 +1,34 @@
+const logger = require('@alias/logger')('growi:services:ImportService'); // eslint-disable-line no-unused-vars
+const fs = require('fs');
+const path = require('path');
+const streamToPromise = require('stream-to-promise');
+
+class ImportService {
+
+  constructor(crowi) {
+    this.baseDir = path.join(crowi.tmpDir, 'downloads');
+    this.extension = 'json';
+    this.encoding = 'utf-8';
+    this.per = 100;
+    this.zlibLevel = 9; // 0(min) - 9(max)
+  }
+
+
+  /**
+   * import a collection from json
+   *
+   * @memberOf ImportService
+   * @param {object} Model instance of mongoose model
+   * @param {string} filePath path to zipped json
+   */
+  async importFromZip(Model, filePath) {
+    console.log(Model.collection.collectionName);
+    console.log(filePath);
+    const readStream = fs.createReadStream(filePath);
+    const writeStream = fs.createWriteStream(path.join(this.baseDir, 'test.json'));
+    readStream.on('data', (data) => { console.log(data); writeStream.write(data); writeStream.write('---------------'); console.log('---------------') });
+  }
+
+}
+
+module.exports = ImportService;