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

refactor to not get filestats from req

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

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

@@ -14,7 +14,7 @@ class GrowiImportForm extends React.Component {
     this.initialState = {
       meta: {},
       zipFileName: '',
-      files: [],
+      fileStats: [],
       schema: {
         pages: {},
         revisions: {},
@@ -46,7 +46,7 @@ class GrowiImportForm extends React.Component {
 
     // TODO use appContainer.apiv3.post
     const { file, data } = await this.props.appContainer.apiPost('/v3/import/upload', formData);
-    this.setState({ meta: data.meta, zipFileName: file, files: data.files });
+    this.setState({ meta: data.meta, zipFileName: file, fileStats: data.fileStats });
     // TODO toastSuccess, toastError
   }
 
@@ -55,7 +55,7 @@ class GrowiImportForm extends React.Component {
 
     // TODO use appContainer.apiv3.post
     await this.props.appContainer.apiPost('/v3/import', {
-      zipFile: this.state.zipFileName,
+      fileName: this.state.zipFileName,
       schema: this.state.schema,
     });
     // TODO toastSuccess, toastError
@@ -106,7 +106,7 @@ class GrowiImportForm extends React.Component {
         </form>
 
         {/* TODO: move to another component 1 */}
-        {this.state.files.length > 0 && (
+        {this.state.fileStats.length > 0 && (
           <Fragment>
             {/* TODO: move to another component 2 */}
             <div>{this.state.zipFileName}</div>
@@ -119,7 +119,7 @@ class GrowiImportForm extends React.Component {
                 </tr>
               </thead>
               <tbody>
-                {this.state.files.map((file) => {
+                {this.state.fileStats.map((file) => {
                   return (
                     <tr key={file.fileName}>
                       <td>{file.fileName}</td>

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

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

+ 7 - 5
src/server/routes/apiv3/import.js

@@ -96,18 +96,20 @@ module.exports = (crowi) => {
   router.post('/', async(req, res) => {
     // TODO: add express validator
 
-    const { zipFile, schema } = req.body;
+    const { fileName, schema } = req.body;
+    const zipFile = importService.getFile(fileName);
 
     // unzip
     await importService.unzip(zipFile);
     // eslint-disable-next-line no-unused-vars
-    const { meta, files } = await importService.parseZipFile(zipFile);
+    const { meta, fileStats } = await importService.parseZipFile(zipFile);
 
     // TODO: validate using meta data
 
     try {
-      await Promise.all(files.map(async({ fileName, collectionName, size }) => {
+      await Promise.all(fileStats.map(async({ fileName, collectionName, size }) => {
         const Model = importService.getModelFromCollectionName(collectionName);
+        const jsonFile = importService.getFile(fileName);
 
         let overwriteParams;
         if (overwriteParamsFn[collectionName] != null) {
@@ -115,7 +117,7 @@ module.exports = (crowi) => {
           overwriteParams = await overwriteParamsFn(Model, schema[collectionName], req);
         }
 
-        await importService.import(Model, fileName, overwriteParams);
+        await importService.import(Model, jsonFile, overwriteParams);
       }));
 
       // TODO: use res.apiv3
@@ -130,7 +132,7 @@ module.exports = (crowi) => {
 
   router.post('/upload', uploads.single('file'), async(req, res) => {
     const { file } = req;
-    const zipFile = path.join(file.destination, file.filename);
+    const zipFile = importService.getFile(file.filename);
 
     try {
       const data = await importService.parseZipFile(zipFile);

+ 4 - 18
src/server/service/export.js

@@ -163,7 +163,7 @@ class ExportService {
    */
   async zipFiles(_configs) {
     const configs = toArrayIfNot(_configs);
-    const zipFile = this.getZipFile();
+    const zipFile = path.join(this.baseDir, this.zipFileName);
     const archive = archiver('zip', {
       zlib: { level: this.zlibLevel },
     });
@@ -204,27 +204,13 @@ class ExportService {
    * get the absolute path to the zip file
    *
    * @memberOf ImportService
-   * @param {boolean} [validate=false] boolean to check if the file exists
    * @return {string} absolute path to the zip file
    */
-  getZipFile(validate = false) {
+  getZipFile() {
     const zipFile = path.join(this.baseDir, this.zipFileName);
 
-    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;
-      }
-    }
+    // throws err if the file does not exist
+    fs.accessSync(zipFile);
 
     return zipFile;
   }

+ 6 - 20
src/server/service/import.js

@@ -148,7 +148,7 @@ class ImportService {
   async parseZipFile(zipFile) {
     const readStream = fs.createReadStream(zipFile);
     const unzipStream = readStream.pipe(unzipper.Parse());
-    const files = [];
+    const fileStats = [];
 
     unzipStream.on('entry', (entry) => {
       const fileName = entry.path;
@@ -159,7 +159,7 @@ class ImportService {
         entry.autodrain();
       }
       else {
-        files.push({
+        fileStats.push({
           fileName,
           collectionName: path.basename(fileName, '.json'),
           size,
@@ -173,7 +173,7 @@ class ImportService {
 
     return {
       meta: {},
-      files,
+      fileStats,
     };
   }
 
@@ -310,27 +310,13 @@ class ImportService {
    *
    * @memberOf ImportService
    * @param {string} fileName base name of file
-   * @param {boolean} [validate=false] boolean to check if the file exists
    * @return {string} absolute path to the file
    */
-  getJsonFile(fileName, validate = false) {
+  getFile(fileName) {
     const jsonFile = path.join(this.baseDir, fileName);
 
-    if (validate) {
-      try {
-        fs.accessSync(jsonFile);
-      }
-      catch (err) {
-        if (err.code === 'ENOENT') {
-          logger.error(`${jsonFile} does not exist`, err);
-        }
-        else {
-          logger.error(err);
-        }
-
-        throw err;
-      }
-    }
+    // throws err if the file does not exist
+    fs.accessSync(jsonFile);
 
     return jsonFile;
   }