Taichi Masuyama 3 лет назад
Родитель
Сommit
cbd647a12c

+ 6 - 1
packages/app/src/server/routes/apiv3/import.js

@@ -294,7 +294,12 @@ export default function route(crowi) {
      * import
      */
     try {
-      importService.import(collections, importSettingsMap);
+      (async() => {
+        await importService.updateIsV5CompatibleBeforeImport(collections);
+        await importService.import(collections, importSettingsMap);
+        await importService.normalizeAllPublicPagesAfterImport(collections);
+      })();
+
       const parameters = { action: SupportedAction.ACTION_ADMIN_GROWI_DATA_IMPORTED };
       activityEvent.emit('update', res.locals.activity._id, parameters);
     }

+ 11 - 2
packages/app/src/server/service/g2g-transfer.ts

@@ -600,6 +600,15 @@ export class G2GTransferReceiverService implements Receiver {
     return importSettingsMap;
   }
 
+  private async processImportWithPrePostProcess(
+      collections: string[],
+      importSettingsMap: { [key: string]: ImportSettings; },
+  ): Promise<void> {
+    await this.crowi.importService.updateIsV5CompatibleBeforeImport(collections);
+    await this.crowi.importService.import(collections, importSettingsMap);
+    await this.crowi.importService.normalizeAllPublicPagesAfterImport(collections);
+  }
+
   public async importCollections(
       collections: string[],
       importSettingsMap: { [key: string]: ImportSettings; },
@@ -614,7 +623,7 @@ export class G2GTransferReceiverService implements Receiver {
       const fileUploadConfigs = await this.getFileUploadConfigs();
 
       // import mongo collections(overwrites file uplaod configs)
-      await importService.import(collections, importSettingsMap);
+      await this.processImportWithPrePostProcess(collections, importSettingsMap);
 
       // restore file upload config from cache
       await configManager.removeConfigsInTheSameNamespace('crowi', UPLOAD_CONFIG_KEYS);
@@ -622,7 +631,7 @@ export class G2GTransferReceiverService implements Receiver {
     }
     else {
       // import mongo collections(overwrites file uplaod configs)
-      await importService.import(collections, importSettingsMap);
+      await this.processImportWithPrePostProcess(collections, importSettingsMap);
 
       // update file upload config
       await configManager.updateConfigsInTheSameNamespace('crowi', sourceGROWIUploadConfigs);

+ 35 - 10
packages/app/src/server/service/import.js

@@ -174,21 +174,49 @@ class ImportService {
   }
 
   /**
-   * import collections from json
+   * Calculate whether page normalization is required
    *
    * @param {string} collections MongoDB collection name
-   * @param {array} importSettingsMap key: collection name, value: ImportSettings instance
    */
-  async import(collections, importSettingsMap) {
-    // init status object
-    this.currentProgressingStatus = new CollectionProgressingStatus(collections);
-
+  async getShouldNormalizePages(collections) {
     const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
     const isImportPagesCollection = collections.includes('pages');
     const shouldNormalizePages = isV5Compatible && isImportPagesCollection;
 
-    // set isV5Compatible to false
+    return shouldNormalizePages;
+  }
+
+  /**
+   * Update the config app:isV5Compatible to false when page normalization is required
+   *
+   * @param {string} collections MongoDB collection name
+   */
+  async updateIsV5CompatibleBeforeImport(collections) {
+    const shouldNormalizePages = this.getShouldNormalizePages(collections);
+
     if (shouldNormalizePages) await this.crowi.configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': false });
+  }
+
+  /**
+   * Run pageService.normalizeAllPublicPages when page normalization is required
+   *
+   * @param {string} collections MongoDB collection name
+   */
+  async normalizeAllPublicPagesAfterImport(collections) {
+    const shouldNormalizePages = this.getShouldNormalizePages(collections);
+
+    if (shouldNormalizePages) await this.crowi.pageService.normalizeAllPublicPages();
+  }
+
+  /**
+   * import collections from json
+   *
+   * @param {string} collections MongoDB collection name
+   * @param {array} importSettingsMap key: collection name, value: ImportSettings instance
+   */
+  async import(collections, importSettingsMap) {
+    // init status object
+    this.currentProgressingStatus = new CollectionProgressingStatus(collections);
 
     // process serially so as not to waste memory
     const promises = collections.map((collectionName) => {
@@ -207,9 +235,6 @@ class ImportService {
       }
     }
 
-    // run normalizeAllPublicPages
-    if (shouldNormalizePages) await this.crowi.pageService.normalizeAllPublicPages();
-
     this.currentProgressingStatus = null;
     this.emitTerminateEvent();