Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/no-new-js-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
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);
},
});