Просмотр исходного кода

introduce growi bridge service

mizozobu 6 лет назад
Родитель
Сommit
8608c684fc

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

@@ -46,6 +46,7 @@ function Crowi(rootdir) {
   this.appService = null;
   this.fileUploadService = null;
   this.restQiitaAPIService = null;
+  this.growiBridgeService = null;
   this.exportService = null;
   this.importService = null;
   this.cdnResourcesService = new CdnResourcesService();
@@ -87,10 +88,12 @@ Crowi.prototype.init = async function() {
   // customizeService depends on AppService and XssService
   // passportService depends on appService
   // slack depends on setUpSlacklNotification
+  // export and import depends on setUpGrowiBridge
   await Promise.all([
     this.setUpApp(),
     this.setUpXss(),
     this.setUpSlacklNotification(),
+    this.setUpGrowiBridge(),
   ]);
 
   await Promise.all([
@@ -126,6 +129,7 @@ Crowi.prototype.initForTest = async function() {
     this.setUpApp(),
     // this.setUpXss(),
     // this.setUpSlacklNotification(),
+    // this.setUpGrowiBridge(),
   ]);
 
   await Promise.all([
@@ -539,6 +543,13 @@ Crowi.prototype.setupUserGroup = async function() {
   }
 };
 
+Crowi.prototype.setUpGrowiBridge = async function() {
+  const GrowiBridgeService = require('../service/growi-bridge');
+  if (this.growiBridgeService == null) {
+    this.growiBridgeService = new GrowiBridgeService(this);
+  }
+};
+
 Crowi.prototype.setupExport = async function() {
   const ExportService = require('../service/export');
   if (this.exportService == null) {

+ 3 - 3
src/server/routes/apiv3/import.js

@@ -16,7 +16,7 @@ const router = express.Router();
  */
 
 module.exports = (crowi) => {
-  const { importService } = crowi;
+  const { growiBridgeService, importService } = crowi;
   const uploads = multer({
     storage: multer.diskStorage({
       destination: (req, file, cb) => {
@@ -102,7 +102,7 @@ module.exports = (crowi) => {
     // unzip
     await importService.unzip(zipFile);
     // eslint-disable-next-line no-unused-vars
-    const { meta, fileStats } = await importService.parseZipFile(zipFile);
+    const { meta, fileStats } = await growiBridgeService.parseZipFile(zipFile);
 
     // filter fileStats
     const filteredFileStats = fileStats.filter(({ fileName, collectionName, size }) => { return collections.includes(collectionName) });
@@ -138,7 +138,7 @@ module.exports = (crowi) => {
     const zipFile = importService.getFile(file.filename);
 
     try {
-      const data = await importService.parseZipFile(zipFile);
+      const data = await growiBridgeService.parseZipFile(zipFile);
 
       // TODO: use res.apiv3
       return res.send({

+ 53 - 0
src/server/service/growi-bridge.js

@@ -0,0 +1,53 @@
+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');
+const unzipper = require('unzipper');
+
+class GrowiBridgeService {
+
+  // constructor(crowi) {
+  // }
+
+  /**
+   * parse a zip file
+   *
+   * @memberOf ImportService
+   * @param {string} zipFile path to zip file
+   * @return {object} meta{object} and files{Array.<object>}
+   */
+  async parseZipFile(zipFile) {
+    const readStream = fs.createReadStream(zipFile);
+    const unzipStream = readStream.pipe(unzipper.Parse());
+    const fileStats = [];
+
+    unzipStream.on('entry', (entry) => {
+      const fileName = entry.path;
+      const size = entry.vars.uncompressedSize; // There is also compressedSize;
+
+      if (fileName === this.metaFileName) {
+        // TODO: parse meta.json
+        entry.autodrain();
+      }
+      else {
+        fileStats.push({
+          fileName,
+          collectionName: path.basename(fileName, '.json'),
+          size,
+        });
+      }
+
+      entry.autodrain();
+    });
+
+    await streamToPromise(unzipStream);
+
+    return {
+      meta: {},
+      fileStats,
+    };
+  }
+
+}
+
+module.exports = GrowiBridgeService;

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

@@ -138,45 +138,6 @@ class ImportService {
     fs.unlinkSync(jsonFile);
   }
 
-  /**
-   * parse a zip file
-   *
-   * @memberOf ImportService
-   * @param {string} zipFile path to zip file
-   * @return {object} meta{object} and files{Array.<object>}
-   */
-  async parseZipFile(zipFile) {
-    const readStream = fs.createReadStream(zipFile);
-    const unzipStream = readStream.pipe(unzipper.Parse());
-    const fileStats = [];
-
-    unzipStream.on('entry', (entry) => {
-      const fileName = entry.path;
-      const size = entry.vars.uncompressedSize; // There is also compressedSize;
-
-      if (fileName === this.metaFileName) {
-        // TODO: parse meta.json
-        entry.autodrain();
-      }
-      else {
-        fileStats.push({
-          fileName,
-          collectionName: path.basename(fileName, '.json'),
-          size,
-        });
-      }
-
-      entry.autodrain();
-    });
-
-    await streamToPromise(unzipStream);
-
-    return {
-      meta: {},
-      fileStats,
-    };
-  }
-
   /**
    * extract a zip file
    *