فهرست منبع

refactor: respond only id in /_api/v3/g2g-transfer/attachments

mizozobu 3 سال پیش
والد
کامیت
f8a39340fb

+ 10 - 3
packages/app/src/server/routes/apiv3/g2g-transfer.ts

@@ -4,6 +4,7 @@ import path from 'path';
 import { ErrorV3 } from '@growi/core';
 import express, { NextFunction, Request, Router } from 'express';
 import { body } from 'express-validator';
+import { type Document } from 'mongoose';
 import multer from 'multer';
 
 import { SupportedAction } from '~/interfaces/activity';
@@ -148,7 +149,13 @@ module.exports = (crowi: Crowi): Router => {
 
   // eslint-disable-next-line max-len
   receiveRouter.get('/attachments', verifyAndExtractTransferKey, async(req: Request & { transferKey: TransferKey, operatorUserId: string }, res: ApiV3Response) => {
-    const readStream = crowi.exportService.createExportCollectionStream('attachments');
+    const transform = (doc: Document) => JSON.stringify(doc._id.toString());
+    const readStream = crowi.exportService.createExportCollectionStream(
+      'attachments',
+      undefined,
+      { projection: { _id: 1 } },
+      transform,
+    );
     return readStream.pipe(res);
   });
 
@@ -392,11 +399,11 @@ module.exports = (crowi: Crowi): Router => {
     }
 
     // get attachments from new growi
-    const attachmentsFromNewGrowi = await g2gTransferPusherService.getAttachments(tk);
+    const attachmentIdsFromNewGrowi = await g2gTransferPusherService.getAttachments(tk);
 
     // Start transfer
     try {
-      await g2gTransferPusherService.startTransfer(tk, req.user, toGROWIInfo, collections, optionsMap, attachmentsFromNewGrowi);
+      await g2gTransferPusherService.startTransfer(tk, req.user, toGROWIInfo, collections, optionsMap, attachmentIdsFromNewGrowi);
     }
     catch (err) {
       logger.error(err);

+ 6 - 3
packages/app/src/server/service/export.js

@@ -169,12 +169,15 @@ class ExportService {
    *
    * @memberOf ExportService
    * @param {string} collectionName collection name
+   * @param {Filter<TSchema>} filter find filter
+   * @param {FindOptions} options find options
+   * @param {CursorStreamOptions.transform} transform a transformation method applied to each document emitted by the stream
    * @return {NodeJS.ReadStream} readstream for the collection
    */
-  createExportCollectionStream(collectionName) {
+  createExportCollectionStream(collectionName, filter, options, transform = JSON.stringify) {
     const collection = mongoose.connection.collection(collectionName);
-    const nativeCursor = collection.find();
-    const readStream = nativeCursor.stream({ transform: JSON.stringify });
+    const nativeCursor = collection.find(filter, options);
+    const readStream = nativeCursor.stream({ transform });
     const transformStream = this.generateTransformStream();
 
     return readStream

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

@@ -73,7 +73,7 @@ interface Pusher {
    * Transfer all Attachment data to destination GROWI
    * @param {TransferKey} tk Transfer key
    */
-  transferAttachments(tk: TransferKey, attachmentsFromNewGrowi: Attachment[]): Promise<void>
+  transferAttachments(tk: TransferKey, attachmentIdsFromNewGrowi: string[]): Promise<void>
   /**
    * Start transfer data between GROWIs
    * @param {TransferKey} tk TransferKey object
@@ -86,7 +86,7 @@ interface Pusher {
     toGROWIInfo: IDataGROWIInfo,
     collections: string[],
     optionsMap: any,
-    attachmentsFromNewGrowi: Attachment[]
+    attachmentIdsFromNewGrowi: string[]
   ): Promise<void>
 }
 
@@ -277,9 +277,9 @@ export class G2GTransferPusherService implements Pusher {
     return { canTransfer: true };
   }
 
-  public async getAttachments(tk: TransferKey): Promise<Attachment[]> {
+  public async getAttachments(tk: TransferKey): Promise<string[]> {
     try {
-      const { data } = await axios.get<Attachment[]>('/_api/v3/g2g-transfer/attachments', generateAxiosRequestConfigWithTransferKey(tk));
+      const { data } = await axios.get<string[]>('/_api/v3/g2g-transfer/attachments', generateAxiosRequestConfigWithTransferKey(tk));
       return data;
     }
     catch (err) {
@@ -288,14 +288,14 @@ export class G2GTransferPusherService implements Pusher {
     }
   }
 
-  public async transferAttachments(tk: TransferKey, attachmentsFromNewGrowi: Attachment[]): Promise<void> {
+  public async transferAttachments(tk: TransferKey, attachmentIdsFromNewGrowi: string[]): Promise<void> {
     const BATCH_SIZE = 100;
 
     const { fileUploadService } = this.crowi;
     const Attachment = this.crowi.model('Attachment');
 
     // batch get
-    const attachmentsCursor = await Attachment.find({ _id: { $nin: attachmentsFromNewGrowi.map(({ _id }) => _id) } }).cursor();
+    const attachmentsCursor = await Attachment.find({ _id: { $nin: attachmentIdsFromNewGrowi } }).cursor();
     const batchStream = createBatchStream(BATCH_SIZE);
 
     for await (const attachmentBatch of attachmentsCursor.pipe(batchStream)) {
@@ -324,7 +324,7 @@ export class G2GTransferPusherService implements Pusher {
   }
 
   // eslint-disable-next-line max-len
-  public async startTransfer(tk: TransferKey, user: any, toGROWIInfo: IDataGROWIInfo, collections: string[], optionsMap: any, attachmentsFromNewGrowi: Attachment[], shouldEmit = true): Promise<void> {
+  public async startTransfer(tk: TransferKey, user: any, toGROWIInfo: IDataGROWIInfo, collections: string[], optionsMap: any, attachmentIdsFromNewGrowi: string[], shouldEmit = true): Promise<void> {
     const socket = this.crowi.socketIoService.getAdminSocket();
 
     if (shouldEmit) socket.emit('admin:onStartTransferMongoData', {});
@@ -371,7 +371,7 @@ export class G2GTransferPusherService implements Pusher {
     if (shouldEmit) socket.emit('admin:onStartTransferAttachments', {});
 
     try {
-      await this.transferAttachments(tk, attachmentsFromNewGrowi);
+      await this.transferAttachments(tk, attachmentIdsFromNewGrowi);
     }
     catch (err) {
       logger.error(err);