Procházet zdrojové kódy

catch error when zip is broken

Yuki Takei před 6 roky
rodič
revize
bb072d65df
2 změnil soubory, kde provedl 17 přidání a 4 odebrání
  1. 6 1
      src/server/service/export.js
  2. 11 3
      src/server/service/growi-bridge.js

+ 6 - 1
src/server/service/export.js

@@ -78,7 +78,10 @@ class ExportService {
       return this.growiBridgeService.parseZipFile(zipFile);
       return this.growiBridgeService.parseZipFile(zipFile);
     }));
     }));
 
 
-    return zipFileStats;
+    // filter null object (broken zip)
+    const filtered = zipFileStats.filter(element => element != null);
+
+    return filtered;
   }
   }
 
 
   /**
   /**
@@ -227,6 +230,8 @@ class ExportService {
 
 
     // get stats for the zip file
     // get stats for the zip file
     return this.growiBridgeService.parseZipFile(zipFile);
     return this.growiBridgeService.parseZipFile(zipFile);
+
+    // TODO: remove broken zip file
   }
   }
 
 
   /**
   /**

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

@@ -97,11 +97,12 @@ class GrowiBridgeService {
    * @return {object} meta{object} and files{Array.<object>}
    * @return {object} meta{object} and files{Array.<object>}
    */
    */
   async parseZipFile(zipFile) {
   async parseZipFile(zipFile) {
-    const readStream = fs.createReadStream(zipFile);
-    const unzipStream = readStream.pipe(unzipper.Parse());
     const fileStats = [];
     const fileStats = [];
     let meta = {};
     let meta = {};
 
 
+    const readStream = fs.createReadStream(zipFile);
+    const unzipStream = readStream.pipe(unzipper.Parse());
+
     unzipStream.on('entry', async(entry) => {
     unzipStream.on('entry', async(entry) => {
       const fileName = entry.path;
       const fileName = entry.path;
       const size = entry.vars.uncompressedSize; // There is also compressedSize;
       const size = entry.vars.uncompressedSize; // There is also compressedSize;
@@ -120,7 +121,14 @@ class GrowiBridgeService {
       entry.autodrain();
       entry.autodrain();
     });
     });
 
 
-    await streamToPromise(unzipStream);
+    try {
+      await streamToPromise(unzipStream);
+    }
+    // if zip is broken
+    catch (err) {
+      logger.error(err);
+      return null;
+    }
 
 
     return {
     return {
       meta,
       meta,