The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/packages/rocketchat-importer/server/methods/uploadImportFile.js

38 lines
1.4 KiB

import { Meteor } from 'meteor/meteor';
import { Importers } from 'meteor/rocketchat:importer';
import { RocketChatFile } from 'meteor/rocketchat:file';
import { RocketChatImportFileInstance } from '../startup/store';
Meteor.methods({
uploadImportFile(binaryContent, contentType, fileName, importerKey) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'uploadImportFile' });
}
if (!RocketChat.authz.hasRole(userId, 'admin')) {
throw new Meteor.Error('not_authorized', 'User not authorized', { method: 'uploadImportFile' });
}
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' });
}
const date = new Date();
const dateStr = `${ date.getUTCFullYear() }${ date.getUTCMonth() }${ date.getUTCDate() }${ date.getUTCHours() }${ date.getUTCMinutes() }${ date.getUTCSeconds() }`;
const newFileName = `${ dateStr }_${ userId }_${ fileName }`;
importer.instance.startFileUpload(newFileName, contentType);
const file = new Buffer(binaryContent, 'binary');
const readStream = RocketChatFile.bufferToStream(file);
const writeStream = RocketChatImportFileInstance.createWriteStream(newFileName, contentType);
writeStream.on('end', Meteor.bindEnvironment(() => {
}));
readStream.pipe(writeStream);
},
});