Przeglądaj źródła

WIP Stringify multipart form data body

Taichi Masuyama 3 lat temu
rodzic
commit
cd7ab62dcc

+ 28 - 18
packages/app/src/server/routes/apiv3/g2g-transfer.ts

@@ -26,6 +26,7 @@ const validator = {
   transfer: [
     body('transferKey').isString().withMessage('transferKey is required'),
     body('collections').isArray().withMessage('collections is required'),
+    body('optionsMap').isObject().withMessage('optionsMap is required'),
   ],
 };
 
@@ -135,9 +136,18 @@ module.exports = (crowi: Crowi): Router => {
     const zipFile = importService.getFile(file.filename);
     let data;
 
-    // ぶちこみ
+    const { collections: strCollections, optionsMap: strOptionsMap } = req.body;
 
-    const { collections, optionsMap } = req.body;
+    let collections;
+    let optionsMap;
+    try {
+      collections = JSON.parse(strCollections);
+      optionsMap = JSON.parse(strOptionsMap);
+    }
+    catch (err) {
+      logger.error(err);
+      return res.apiv3Err(new ErrorV3('Failed to parse body.', 'parse_failed'), 500);
+    }
 
     /*
      * unzip, parse
@@ -198,7 +208,7 @@ module.exports = (crowi: Crowi): Router => {
      */
     try {
       importService.import(collections, importSettingsMap);
-      const parameters = { action: SupportedAction.ACTION_ADMIN_GROWI_DATA_IMPORTED };
+      // const parameters = { action: SupportedAction.ACTION_ADMIN_GROWI_DATA_IMPORTED };
       // activityEvent.emit('update', res.locals.activity._id, parameters);
     }
     catch (err) {
@@ -206,7 +216,7 @@ module.exports = (crowi: Crowi): Router => {
       // adminEvent.emit('onErrorForImport', { message: err.message });
     }
 
-    // ここまで
+    // -----
 
     try {
       data = await growiBridgeService.parseZipFile(zipFile);
@@ -288,7 +298,7 @@ module.exports = (crowi: Crowi): Router => {
   // Auto export
   // TODO: Use socket to send progress info to the client
   // eslint-disable-next-line max-len
-  pushRouter.post('/transfer', accessTokenParser, loginRequiredStrictly, adminRequired, validator.transfer, apiV3FormValidator, async(req: Request, res: ApiV3Response) => {
+  pushRouter.post('/transfer', /* accessTokenParser, loginRequiredStrictly, adminRequired, */ validator.transfer, apiV3FormValidator, async(req: Request, res: ApiV3Response) => {
     const { transferKey: transferKeyString, collections, optionsMap } = req.body;
 
     // Parse transfer key
@@ -303,21 +313,21 @@ module.exports = (crowi: Crowi): Router => {
 
     // Ask growi info
     // TODO: Ask progress as well
-    let toGROWIInfo: IDataGROWIInfo;
-    try {
-      toGROWIInfo = await g2gTransferPusherService.askGROWIInfo(tk);
-    }
-    catch (err) {
-      logger.error(err);
-      return res.apiv3Err(new ErrorV3('GROWI is incompatible to transfer data.', 'growi_incompatible_to_transfer'));
-    }
+    // let toGROWIInfo: IDataGROWIInfo;
+    // try {
+    //   toGROWIInfo = await g2gTransferPusherService.askGROWIInfo(tk);
+    // }
+    // catch (err) {
+    //   logger.error(err);
+    //   return res.apiv3Err(new ErrorV3('Error occurred while asking GROWI growi info.', 'failed_to_ask_growi_info'));
+    // }
 
     // Check if can transfer
-    const canTransfer = await g2gTransferPusherService.canTransfer(toGROWIInfo);
-    if (!canTransfer) {
-      logger.debug('Could not transfer.');
-      return res.apiv3Err(new ErrorV3('GROWI is incompatible to transfer data.', 'growi_incompatible_to_transfer'));
-    }
+    // const canTransfer = await g2gTransferPusherService.canTransfer(toGROWIInfo);
+    // if (!canTransfer) {
+    //   logger.debug('Could not transfer.');
+    //   return res.apiv3Err(new ErrorV3('GROWI is incompatible to transfer data.', 'growi_incompatible_to_transfer'));
+    // }
 
     // Start transfer
     try {

+ 3 - 2
packages/app/src/server/service/g2g-transfer.ts

@@ -136,8 +136,8 @@ export class G2GTransferPusherService implements Pusher {
 
       const appTitle = this.crowi.appService.getAppTitle();
       form.append('transferDataZipFile', zipFileStream, `${appTitle}-${Date.now}.growi.zip`);
-      form.append('collections', collections);
-      form.append('optionsMap', optionsMap);
+      form.append('collections', JSON.stringify(collections));
+      form.append('optionsMap', JSON.stringify(optionsMap));
       await rawAxios.post('/_api/v3/g2g-transfer/', form, {
         baseURL: appUrl.origin,
         headers: {
@@ -147,6 +147,7 @@ export class G2GTransferPusherService implements Pusher {
       });
     }
     catch (errs) {
+      logger.error(errs);
       if (!Array.isArray(errs)) {
         // TODO: socker.emit(failed_to_transfer);
         return;