Skip to content

Commit

Permalink
[FIX] Importer files are unnecessarily transferred over the network. (#…
Browse files Browse the repository at this point in the history
…25919)

<!-- This is a pull request template, you do not need to uncomment or remove the comments, they won't show up in the PR text. -->

<!-- Your Pull Request name should start with one of the following tags
  [NEW] For new features
  [IMPROVE] For an improvement (performance or little improvements) in existing features
  [FIX] For bug fixes that affect the end-user
  [BREAK] For pull requests including breaking changes
  Chore: For small tasks
  Doc: For documentation
-->

<!-- Checklist!!! If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code. 
  - I have read the Contributing Guide - https://github.com/RocketChat/Rocket.Chat/blob/develop/.github/CONTRIBUTING.md#contributing-to-rocketchat doc
  - I have signed the CLA - https://cla-assistant.io/RocketChat/Rocket.Chat
  - Lint and unit tests pass locally with my changes
  - I have added tests that prove my fix is effective or that my feature works (if applicable)
  - I have added necessary documentation (if applicable)
  - Any dependent changes have been merged and published in downstream modules
-->

## Proposed changes (including videos or screenshots)
<!-- CHANGELOG -->
<!--
  Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request.
  If it fixes a bug or resolves a feature request, be sure to link to that issue below.
  This description will appear in the release notes if we accept the contribution.
-->

<!-- END CHANGELOG -->

## Issue(s)
<!-- Link the issues being closed by or related to this PR. For example, you can use #594 if this PR closes issue number 594 -->

## Steps to test or reproduce
<!-- Mention how you would reproduce the bug if not mentioned on the issue page already. Also mention which screens are going to have the changes if applicable -->

## Further comments
<!-- If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc... -->


Co-authored-by: Guilherme Gazzo <5263975+ggazzo@users.noreply.github.com>
  • Loading branch information
pierre-lehnen-rc and ggazzo committed Jun 28, 2022
1 parent 9f8d3d7 commit 695d3b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
3 changes: 2 additions & 1 deletion apps/meteor/app/api/server/v1/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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',
Expand All @@ -26,7 +27,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));
},
},
);
Expand Down
62 changes: 33 additions & 29 deletions apps/meteor/app/importer/server/methods/uploadImportFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
},
});

0 comments on commit 695d3b9

Please sign in to comment.