Skip to content
Snippets Groups Projects
Unverified Commit 695d3b99 authored by Pierre Lehnen's avatar Pierre Lehnen Committed by GitHub
Browse files

[FIX] Importer files are unnecessarily transferred over the network. (#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: default avatarGuilherme Gazzo <5263975+ggazzo@users.noreply.github.com>
parent 9f8d3d73
No related branches found
No related tags found
No related merge requests found
......@@ -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',
......@@ -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));
},
},
);
......
......@@ -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);
},
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment