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

+ 0 - 2
src/client/js/components/Admin/Import/GrowiZipImportForm.jsx

@@ -22,8 +22,6 @@ class GrowiImportForm extends React.Component {
 
     this.state = this.initialState;
 
-    this.inputRef = React.createRef();
-
     this.toggleCheckbox = this.toggleCheckbox.bind(this);
     this.import = this.import.bind(this);
     this.validateForm = this.validateForm.bind(this);

+ 2 - 1
src/client/js/components/Admin/Import/GrowiZipImportSection.jsx

@@ -31,7 +31,8 @@ class GrowiZipImportSection extends React.Component {
     });
   }
 
-  discardData() {
+  async discardData() {
+    await this.props.appContainer.apiRequest('delete', `/v3/import/${this.state.fileName}`, {});
     this.setState(this.initialState);
   }
 

+ 69 - 0
src/server/routes/apiv3/import.js

@@ -134,6 +134,35 @@ module.exports = (crowi) => {
     }
   });
 
+  /**
+   * @swagger
+   *
+   *  /import/upload:
+   *    post:
+   *      tags: [Import]
+   *      description: upload a zip file
+   *      produces:
+   *        - application/json
+   *      responses:
+   *        200:
+   *          description: file is uploaded
+   *          content:
+   *            application/json:
+   *              schema:
+   *                properties:
+   *                  properties:
+   *                    meta:
+   *                      type: object
+   *                      description: meta data of the uploaded file
+   *                    fileName:
+   *                      type: string
+   *                      description: base name of the uploaded file
+   *                    fileStats:
+   *                      type: array
+   *                      items:
+   *                        type: object
+   *                        description: property of each extracted file
+   */
   router.post('/upload', uploads.single('file'), async(req, res) => {
     const { file } = req;
     const zipFile = importService.getFile(file.filename);
@@ -157,5 +186,45 @@ module.exports = (crowi) => {
     }
   });
 
+  /**
+   * @swagger
+   *
+   *  /import/upload:
+   *    post:
+   *      tags: [Import]
+   *      description: delete a zip file
+   *      produces:
+   *        - application/json
+   *      parameters:
+   *        - name: fileName
+   *          in: path
+   *          description: file name of zip file
+   *          schema:
+   *            type: string
+   *      responses:
+   *        200:
+   *          description: file is deleted
+   *          content:
+   *            application/json:
+   */
+  router.delete('/:fileName', async(req, res) => {
+    const { fileName } = req.params;
+
+    try {
+      const zipFile = importService.getFile(fileName);
+      importService.deleteZipFile(zipFile);
+
+      // TODO: use res.apiv3
+      return res.send({
+        ok: true,
+      });
+    }
+    catch (err) {
+      // TODO: use ApiV3Error
+      logger.error(err);
+      return res.status(500).send({ status: 'ERROR' });
+    }
+  });
+
   return router;
 };

+ 11 - 1
src/server/service/import.js

@@ -137,7 +137,7 @@ class ImportService {
     await streamToPromise(readStream);
 
     // clean up tmp directory
-    fs.unlinkSync(jsonFile);
+    this.deleteZipFile(jsonFile);
   }
 
   /**
@@ -284,6 +284,16 @@ class ImportService {
     return jsonFile;
   }
 
+  /**
+   * 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