Kaynağa Gözat

pass writeStream to ExportService.export and remove ExportService.files

mizozobu 6 yıl önce
ebeveyn
işleme
d3a97d3993
1 değiştirilmiş dosya ile 12 ekleme ve 24 silme
  1. 12 24
      src/server/service/export.js

+ 12 - 24
src/server/service/export.js

@@ -15,18 +15,6 @@ class ExportService {
     this.baseDir = path.join(crowi.tmpDir, 'downloads');
     this.per = 100;
     this.zlibLevel = 9; // 0(min) - 9(max)
-
-    // this.files = {
-    //   configs: path.join(this.baseDir, 'configs.json'),
-    //   pages: path.join(this.baseDir, 'pages.json'),
-    //   pagetagrelations: path.join(this.baseDir, 'pagetagrelations.json'),
-    //   ...
-    // };
-    this.files = {};
-    Object.values(crowi.models).forEach((m) => {
-      const name = m.collection.collectionName;
-      this.files[name] = path.join(this.baseDir, `${name}.json`);
-    });
   }
 
   /**
@@ -80,29 +68,28 @@ class ExportService {
    * @param {function} [getLogText] (n, total) => { ... }
    * @return {string} path to the exported json file
    */
-  async export(file, readStream, total, getLogText) {
+  async export(writeStream, readStream, total, getLogText) {
     let n = 0;
-    const ws = fs.createWriteStream(file, { encoding: this.growiBridgeService.getEncoding() });
 
     // open an array
-    ws.write('[');
+    writeStream.write('[');
 
     readStream.on('data', (chunk) => {
-      if (n !== 0) ws.write(',');
-      ws.write(JSON.stringify(chunk));
+      if (n !== 0) writeStream.write(',');
+      writeStream.write(JSON.stringify(chunk));
       n++;
       this.logProgress(n, total, getLogText);
     });
 
     readStream.on('end', () => {
       // close the array
-      ws.write(']');
-      ws.close();
+      writeStream.write(']');
+      writeStream.close();
     });
 
     await streamToPromise(readStream);
 
-    return file;
+    return writeStream.path;
   }
 
   /**
@@ -114,14 +101,15 @@ class ExportService {
    */
   async exportCollectionToJson(Model) {
     const { collectionName } = Model.collection;
-    const targetFile = this.files[collectionName];
+    const jsonFileToWrite = path.join(this.baseDir, `${collectionName}.json`);
+    const writeStream = fs.createWriteStream(jsonFileToWrite, { encoding: this.growiBridgeService.getEncoding() });
     const readStream = Model.find().cursor();
     const total = await Model.countDocuments();
     const getLogText = (n, total) => `${collectionName}: ${n}/${total} written`;
 
-    const file = await this.export(targetFile, readStream, total, getLogText);
+    const jsonFileWritten = await this.export(writeStream, readStream, total, getLogText);
 
-    return file;
+    return jsonFileWritten;
   }
 
   /**
@@ -198,7 +186,7 @@ class ExportService {
 
     await streamToPromise(archive);
 
-    logger.debug(`zipped growi data into ${zipFile} (${archive.pointer()} bytes)`);
+    logger.info(`zipped growi data into ${zipFile} (${archive.pointer()} bytes)`);
 
     return zipFile;
   }