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

deleteZipFile, encoding, metaFileName

mizozobu 6 лет назад
Родитель
Сommit
78a3beac6a

+ 2 - 1
src/server/routes/apiv3/export.js

@@ -2,6 +2,7 @@ const loggerFactory = require('@alias/logger');
 
 const logger = loggerFactory('growi:routes:apiv3:export'); // eslint-disable-line no-unused-vars
 const path = require('path');
+const fs = require('fs');
 
 const express = require('express');
 
@@ -114,7 +115,7 @@ module.exports = (crowi) => {
 
     try {
       const zipFile = exportService.getFile(fileName);
-      exportService.deleteZipFile(zipFile);
+      fs.unlinkSync(zipFile);
 
       // TODO: use res.apiv3
       return res.status(200).send({ ok: true });

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

@@ -2,6 +2,7 @@ const loggerFactory = require('@alias/logger');
 
 const logger = loggerFactory('growi:routes:apiv3:import'); // eslint-disable-line no-unused-vars
 const path = require('path');
+const fs = require('fs');
 const multer = require('multer');
 const { ObjectId } = require('mongoose').Types;
 
@@ -212,7 +213,7 @@ module.exports = (crowi) => {
 
     try {
       const zipFile = importService.getFile(fileName);
-      importService.deleteZipFile(zipFile);
+      fs.unlinkSync(zipFile);
 
       // TODO: use res.apiv3
       return res.send({

+ 3 - 15
src/server/service/export.js

@@ -13,8 +13,6 @@ class ExportService {
     this.growiBridgeService = crowi.growiBridgeService;
     this.getFile = this.growiBridgeService.getFile.bind(this);
     this.baseDir = path.join(crowi.tmpDir, 'downloads');
-    this.metaFileName = 'meta.json';
-    this.encoding = 'utf-8';
     this.per = 100;
     this.zlibLevel = 9; // 0(min) - 9(max)
 
@@ -54,8 +52,8 @@ class ExportService {
    * @return {string} path to meta.json
    */
   async createMetaJson() {
-    const metaJson = path.join(this.baseDir, this.metaFileName);
-    const writeStream = fs.createWriteStream(metaJson, { encoding: this.encoding });
+    const metaJson = path.join(this.baseDir, this.growiBridgeService.getMetaFileName());
+    const writeStream = fs.createWriteStream(metaJson, { encoding: this.growiBridgeService.getEncoding() });
 
     const metaData = {
       version: this.crowi.version,
@@ -83,7 +81,7 @@ class ExportService {
    */
   async export(file, readStream, total) {
     let n = 0;
-    const ws = fs.createWriteStream(file, { encoding: this.encoding });
+    const ws = fs.createWriteStream(file, { encoding: this.growiBridgeService.getEncoding() });
 
     // open an array
     ws.write('[');
@@ -208,16 +206,6 @@ class ExportService {
     return zipFile;
   }
 
-  /**
-   * remove zip file from downloads dir
-   *
-   * @param {string} zipFile absolute path to zip file
-   * @memberOf ExportService
-   */
-  deleteZipFile(zipFile) {
-    fs.unlinkSync(zipFile);
-  }
-
 }
 
 module.exports = ExportService;

+ 24 - 3
src/server/service/growi-bridge.js

@@ -1,4 +1,4 @@
-const logger = require('@alias/logger')('growi:services:ImportService'); // eslint-disable-line no-unused-vars
+const logger = require('@alias/logger')('growi:services:GrowiBridgeService'); // eslint-disable-line no-unused-vars
 const fs = require('fs');
 const path = require('path');
 const streamToPromise = require('stream-to-promise');
@@ -7,6 +7,7 @@ const unzipper = require('unzipper');
 class GrowiBridgeService {
 
   constructor(crowi) {
+    this.encoding = 'utf-8';
     this.metaFileName = 'meta.json';
 
     // { pages: Page, users: User, ... }
@@ -26,6 +27,26 @@ class GrowiBridgeService {
     }
   }
 
+  /**
+   * getter for encoding
+   *
+   * @memberOf GrowiBridgeService
+   * @return {string} encoding
+   */
+  getEncoding() {
+    return this.encoding;
+  }
+
+  /**
+   * getter for metaFileName
+   *
+   * @memberOf GrowiBridgeService
+   * @return {string} base name of meta file
+   */
+  getMetaFileName() {
+    return this.metaFileName;
+  }
+
   /**
    * get a model from collection name
    *
@@ -47,7 +68,7 @@ class GrowiBridgeService {
    * get the absolute path to a file
    * this method must must be bound to the caller (this.baseDir is undefined in this service)
    *
-   * @memberOf ImportService
+   * @memberOf GrowiBridgeService
    * @param {string} fileName base name of file
    * @return {string} absolute path to the file
    */
@@ -81,7 +102,7 @@ class GrowiBridgeService {
       const fileName = entry.path;
       const size = entry.vars.uncompressedSize; // There is also compressedSize;
 
-      if (fileName === this.metaFileName) {
+      if (fileName === this.getMetaFileName()) {
         meta = JSON.parse((await entry.buffer()).toString());
       }
       else {

+ 4 - 16
src/server/service/import.js

@@ -13,8 +13,6 @@ class ImportService {
     this.growiBridgeService = crowi.growiBridgeService;
     this.getFile = this.growiBridgeService.getFile.bind(this);
     this.baseDir = path.join(crowi.tmpDir, 'imports');
-    this.metaFileName = 'meta.json';
-    this.encoding = 'utf-8';
     this.per = 100;
     this.keepOriginal = this.keepOriginal.bind(this);
 
@@ -79,7 +77,7 @@ class ImportService {
     let failedIds = [];
     let unorderedBulkOp = Model.collection.initializeUnorderedBulkOp();
 
-    const readStream = fs.createReadStream(jsonFile, { encoding: this.encoding });
+    const readStream = fs.createReadStream(jsonFile, { encoding: this.growiBridgeService.getEncoding() });
     const jsonStream = readStream.pipe(JSONStream.parse('*'));
 
     jsonStream.on('data', async(document) => {
@@ -123,7 +121,7 @@ class ImportService {
     await streamToPromise(readStream);
 
     // clean up tmp directory
-    this.deleteZipFile(jsonFile);
+    fs.unlinkSync(jsonFile);
   }
 
   /**
@@ -141,13 +139,13 @@ class ImportService {
     unzipStream.on('entry', (entry) => {
       const fileName = entry.path;
 
-      if (fileName === this.metaFileName) {
+      if (fileName === this.growiBridgeService.getMetaFileName()) {
         // skip meta.json
         entry.autodrain();
       }
       else {
         const jsonFile = path.join(this.baseDir, fileName);
-        const writeStream = fs.createWriteStream(jsonFile, { encoding: this.encoding });
+        const writeStream = fs.createWriteStream(jsonFile, { encoding: this.growiBridgeService.getEncoding() });
         entry.pipe(writeStream);
         files.push(jsonFile);
       }
@@ -237,16 +235,6 @@ class ImportService {
     return document;
   }
 
-  /**
-   * remove zip file from imports dir
-   *
-   * @memberOf ImportService
-   * @param {string} zipFile absolute path to zip file
-   */
-  deleteZipFile(zipFile) {
-    fs.unlinkSync(zipFile);
-  }
-
   /**
    * validate using meta.json
    * to pass validation, all the criteria must be met