From 52183e1a4c699871ce102321d24e545528951509 Mon Sep 17 00:00:00 2001 From: Pierre Lehnen Date: Sat, 18 Jun 2022 22:24:06 -0300 Subject: [PATCH 1/2] [FIX] Importer files are unnecessarily transferred over the network. --- apps/meteor/app/api/server/v1/import.js | 3 +- .../server/methods/uploadImportFile.js | 62 ++++++++++--------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/apps/meteor/app/api/server/v1/import.js b/apps/meteor/app/api/server/v1/import.js index b681b480d9d5..7cc62956cb09 100644 --- a/apps/meteor/app/api/server/v1/import.js +++ b/apps/meteor/app/api/server/v1/import.js @@ -4,6 +4,7 @@ import { API } from '../api'; import { hasPermission } from '../../../authorization/server'; import { Imports } from '../../../models/server'; import { Importers } from '../../../importer/server'; +import { executeUploadImportFile } from '../../../importer/server/methods/uploadImportFile'; API.v1.addRoute( 'uploadImportFile', @@ -12,7 +13,7 @@ API.v1.addRoute( post() { const { binaryContent, contentType, fileName, importerKey } = this.bodyParams; - return API.v1.success(Meteor.call('uploadImportFile', binaryContent, contentType, fileName, importerKey)); + return API.v1.success(executeUploadImportFile(this.userId, binaryContent, contentType, fileName, importerKey)); }, }, ); diff --git a/apps/meteor/app/importer/server/methods/uploadImportFile.js b/apps/meteor/app/importer/server/methods/uploadImportFile.js index 115559664982..b23183210395 100644 --- a/apps/meteor/app/importer/server/methods/uploadImportFile.js +++ b/apps/meteor/app/importer/server/methods/uploadImportFile.js @@ -6,6 +6,38 @@ import { hasPermission } from '../../../authorization'; import { ProgressStep } from '../../lib/ImporterProgressStep'; import { Importers } from '..'; +export const executeUploadImportFile = (userId, binaryContent, contentType, fileName, importerKey) => { + const importer = Importers.get(importerKey); + if (!importer) { + throw new Meteor.Error('error-importer-not-defined', `The importer (${importerKey}) has no import class defined.`, { + method: 'uploadImportFile', + }); + } + + importer.instance = new importer.importer(importer); // eslint-disable-line new-cap + + const date = new Date(); + const dateStr = `${date.getUTCFullYear()}${date.getUTCMonth()}${date.getUTCDate()}${date.getUTCHours()}${date.getUTCMinutes()}${date.getUTCSeconds()}`; + const newFileName = `${dateStr}_${userId}_${fileName}`; + + // Store the file name and content type on the imports collection + importer.instance.startFileUpload(newFileName, contentType); + + // Save the file on the File Store + const file = Buffer.from(binaryContent, 'base64'); + const readStream = RocketChatFile.bufferToStream(file); + const writeStream = RocketChatImportFileInstance.createWriteStream(newFileName, contentType); + + writeStream.on( + 'end', + Meteor.bindEnvironment(() => { + importer.instance.updateProgress(ProgressStep.FILE_LOADED); + }), + ); + + readStream.pipe(writeStream); +}; + Meteor.methods({ uploadImportFile(binaryContent, contentType, fileName, importerKey) { const userId = Meteor.userId(); @@ -20,34 +52,6 @@ Meteor.methods({ }); } - const importer = Importers.get(importerKey); - if (!importer) { - throw new Meteor.Error('error-importer-not-defined', `The importer (${importerKey}) has no import class defined.`, { - method: 'uploadImportFile', - }); - } - - importer.instance = new importer.importer(importer); // eslint-disable-line new-cap - - const date = new Date(); - const dateStr = `${date.getUTCFullYear()}${date.getUTCMonth()}${date.getUTCDate()}${date.getUTCHours()}${date.getUTCMinutes()}${date.getUTCSeconds()}`; - const newFileName = `${dateStr}_${userId}_${fileName}`; - - // Store the file name and content type on the imports collection - importer.instance.startFileUpload(newFileName, contentType); - - // Save the file on the File Store - const file = Buffer.from(binaryContent, 'base64'); - const readStream = RocketChatFile.bufferToStream(file); - const writeStream = RocketChatImportFileInstance.createWriteStream(newFileName, contentType); - - writeStream.on( - 'end', - Meteor.bindEnvironment(() => { - importer.instance.updateProgress(ProgressStep.FILE_LOADED); - }), - ); - - readStream.pipe(writeStream); + executeUploadImportFile(userId, binaryContent, contentType, fileName, importerKey); }, }); From e1bce63d14131145170d6e227643073057473fdc Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 27 Jun 2022 19:18:57 -0300 Subject: [PATCH 2/2] test --- .github/workflows/no-new-js-files.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/no-new-js-files.yml b/.github/workflows/no-new-js-files.yml index ea101e0a26eb..77045946b52c 100644 --- a/.github/workflows/no-new-js-files.yml +++ b/.github/workflows/no-new-js-files.yml @@ -6,6 +6,6 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: ggazzo/no-js-action@1.0.3 + - uses: ggazzo/no-js-action@1.0.2 with: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}