mizozobu 6 лет назад
Родитель
Сommit
43f3c8d64d
2 измененных файлов с 25 добавлено и 2 удалено
  1. 6 2
      src/server/routes/apiv3/import.js
  2. 19 0
      src/server/service/import.js

+ 6 - 2
src/server/routes/apiv3/import.js

@@ -107,9 +107,10 @@ module.exports = (crowi) => {
     // filter fileStats
     // filter fileStats
     const filteredFileStats = fileStats.filter(({ fileName, collectionName, size }) => { return collections.includes(collectionName) });
     const filteredFileStats = fileStats.filter(({ fileName, collectionName, size }) => { return collections.includes(collectionName) });
 
 
-    // TODO: validate using meta data
-
     try {
     try {
+      // validate with meta.json
+      importService.validate(meta);
+
       await Promise.all(filteredFileStats.map(async({ fileName, collectionName, size }) => {
       await Promise.all(filteredFileStats.map(async({ fileName, collectionName, size }) => {
         const Model = importService.getModelFromCollectionName(collectionName);
         const Model = importService.getModelFromCollectionName(collectionName);
         const jsonFile = importService.getFile(fileName);
         const jsonFile = importService.getFile(fileName);
@@ -140,6 +141,9 @@ module.exports = (crowi) => {
     try {
     try {
       const data = await growiBridgeService.parseZipFile(zipFile);
       const data = await growiBridgeService.parseZipFile(zipFile);
 
 
+      // validate with meta.json
+      importService.validate(data.meta);
+
       // TODO: use res.apiv3
       // TODO: use res.apiv3
       return res.send({
       return res.send({
         ok: true,
         ok: true,

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

@@ -9,6 +9,7 @@ const { ObjectId } = require('mongoose').Types;
 class ImportService {
 class ImportService {
 
 
   constructor(crowi) {
   constructor(crowi) {
+    this.crowi = crowi;
     this.baseDir = path.join(crowi.tmpDir, 'imports');
     this.baseDir = path.join(crowi.tmpDir, 'imports');
     this.metaFileName = 'meta.json';
     this.metaFileName = 'meta.json';
     this.encoding = 'utf-8';
     this.encoding = 'utf-8';
@@ -282,6 +283,24 @@ class ImportService {
     return jsonFile;
     return jsonFile;
   }
   }
 
 
+  /**
+   * validate using meta.json
+   * to pass validation, all the criteria must be met
+   * - ${version of this growi} === ${version of growi that exported data}
+   *
+   * @memberOf ImportService
+   * @param {object} meta meta data from meta.json
+   */
+  validate(meta) {
+    if (meta.version !== this.crowi.version) {
+      throw new Error('the version of this growi and the growi that exported the data are not met');
+    }
+
+    // TODO: check if all migrations are completed
+    // - export: throw err if there are pending migrations
+    // - import: throw err if there are pending migrations
+  }
+
 }
 }
 
 
 module.exports = ImportService;
 module.exports = ImportService;