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

fix jsdoc and create exportService.getZipFile

mizozobu 6 лет назад
Родитель
Сommit
4a90389c3d

+ 4 - 1
src/client/js/components/Admin/Import/GrowiImportForm.jsx

@@ -11,7 +11,7 @@ class GrowiImportForm extends React.Component {
   constructor(props) {
   constructor(props) {
     super(props);
     super(props);
 
 
-    this.state = {
+    this.initialState = {
       meta: {},
       meta: {},
       files: [],
       files: [],
       schema: {
       schema: {
@@ -20,6 +20,8 @@ class GrowiImportForm extends React.Component {
       },
       },
     };
     };
 
 
+    this.state = this.initialState;
+
     this.inputRef = React.createRef();
     this.inputRef = React.createRef();
 
 
     this.changeFileName = this.changeFileName.bind(this);
     this.changeFileName = this.changeFileName.bind(this);
@@ -61,6 +63,7 @@ class GrowiImportForm extends React.Component {
       }),
       }),
     });
     });
     // TODO toastSuccess, toastError
     // TODO toastSuccess, toastError
+    this.setState(this.initialState);
   }
   }
 
 
   validateForm() {
   validateForm() {

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

@@ -56,7 +56,7 @@ module.exports = (crowi) => {
   router.get('/', async(req, res) => {
   router.get('/', async(req, res) => {
     // TODO: add express validator
     // TODO: add express validator
     try {
     try {
-      return res.download(exportService.zipFile);
+      return res.download(exportService.getZipFile(true));
     }
     }
     catch (err) {
     catch (err) {
       // TODO: use ApiV3Error
       // TODO: use ApiV3Error

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

@@ -108,7 +108,7 @@ module.exports = (crowi) => {
       }));
       }));
 
 
       // TODO: use res.apiv3
       // TODO: use res.apiv3
-      return res.send({ status: 'OK' });
+      return res.send({ ok: true });
     }
     }
     catch (err) {
     catch (err) {
       // TODO: use ApiV3Error
       // TODO: use ApiV3Error

+ 22 - 22
src/server/service/export.js

@@ -163,7 +163,7 @@ class ExportService {
    */
    */
   async zipFiles(_configs) {
   async zipFiles(_configs) {
     const configs = toArrayIfNot(_configs);
     const configs = toArrayIfNot(_configs);
-    const zipFile = path.join(this.baseDir, this.zipFileName);
+    const zipFile = this.getZipFile();
     const archive = archiver('zip', {
     const archive = archiver('zip', {
       zlib: { level: this.zlibLevel },
       zlib: { level: this.zlibLevel },
     });
     });
@@ -201,32 +201,32 @@ class ExportService {
   }
   }
 
 
   /**
   /**
-   * replace a file extension
+   * get the absolute path to the zip file
    *
    *
-   * @memberOf ExportService
-   * @param {string} file file path
-   * @param {string} extension new extension
-   * @return {string} path to file with new extension
+   * @memberOf ImportService
+   * @param {boolean} [validate=false] boolean to check if the file exists
+   * @return {string} absolute path to the zip file
    */
    */
-  replaceExtension(file, extension) {
-    return `${path.join(path.dirname(file), `${path.basename(file, path.extname(file))}.${extension}`)}`;
-  }
+  getZipFile(validate = false) {
+    const zipFile = path.join(this.baseDir, this.zipFileName);
 
 
-  /**
-   * get the path to the zipped file for a collection
-   *
-   * @memberOf ExportService
-   * @param {object} Model instance of mongoose model
-   * @return {string} path to zip file
-   */
-  getZipFile(Model) {
-    const json = this.files[Model.collection.collectionName];
-    const zip = this.replaceExtension(json, 'zip');
-    if (!fs.existsSync(zip)) {
-      throw new Error(`${zip} does not exist`);
+    if (validate) {
+      try {
+        fs.accessSync(zipFile);
+      }
+      catch (err) {
+        if (err.code === 'ENOENT') {
+          logger.error(`${zipFile} does not exist`, err);
+        }
+        else {
+          logger.error(err);
+        }
+
+        throw err;
+      }
     }
     }
 
 
-    return zip;
+    return zipFile;
   }
   }
 
 
   /**
   /**

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

@@ -59,7 +59,7 @@ class ImportService {
    * automatically convert ObjectId
    * automatically convert ObjectId
    *
    *
    * @memberOf ImportService
    * @memberOf ImportService
-   * @param {array<object>} _value value from imported document
+   * @param {any} _value value from imported document
    * @param {{ _document: object, schema: object, key: string }}
    * @param {{ _document: object, schema: object, key: string }}
    * @return {any} new value for the document
    * @return {any} new value for the document
    */
    */
@@ -143,7 +143,7 @@ class ImportService {
    *
    *
    * @memberOf ImportService
    * @memberOf ImportService
    * @param {string} zipFile path to zip file
    * @param {string} zipFile path to zip file
-   * @return  {object} meta{object} and files{array<object>}
+   * @return {object} meta{object} and files{array<object>}
    */
    */
   async unzip(zipFile) {
   async unzip(zipFile) {
     const readStream = fs.createReadStream(zipFile);
     const readStream = fs.createReadStream(zipFile);
@@ -261,7 +261,7 @@ class ImportService {
    * get a model from collection name
    * get a model from collection name
    *
    *
    * @memberOf ImportService
    * @memberOf ImportService
-   * @param {object} collectionName collection name
+   * @param {string} collectionName collection name
    * @return {object} instance of mongoose model
    * @return {object} instance of mongoose model
    */
    */
   getModelFromCollectionName(collectionName) {
   getModelFromCollectionName(collectionName) {
@@ -278,7 +278,7 @@ class ImportService {
    * get the absolute path to a file
    * get the absolute path to a file
    *
    *
    * @memberOf ImportService
    * @memberOf ImportService
-   * @param {object} fileName base name of file
+   * @param {string} fileName base name of file
    * @param {boolean} [validate=false] boolean to check if the file exists
    * @param {boolean} [validate=false] boolean to check if the file exists
    * @return {string} absolute path to the file
    * @return {string} absolute path to the file
    */
    */