From 4821510daafb21856b725d6698b17a2a5a6dd5f4 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 11 Jul 2017 11:26:22 -0300 Subject: [PATCH] Fix file upload on Slack import Closes #7447 --- packages/rocketchat-importer-slack/server.js | 12 +- .../server/classes/ImporterBase.js | 123 ++++++++++-------- 2 files changed, 73 insertions(+), 62 deletions(-) diff --git a/packages/rocketchat-importer-slack/server.js b/packages/rocketchat-importer-slack/server.js index 94b719786faf..1ff1fc3eb3bc 100644 --- a/packages/rocketchat-importer-slack/server.js +++ b/packages/rocketchat-importer-slack/server.js @@ -250,7 +250,9 @@ Importer.Slack = class extends Importer.Base { RocketChat.models.Messages.createUserJoinWithRoomIdAndUser(room._id, this.getRocketUser(message.user), msgDataDefaults); } } else if (message.subtype === 'channel_leave') { - if (this.getRocketUser(message.user)) { RocketChat.models.Messages.createUserLeaveWithRoomIdAndUser(room._id, this.getRocketUser(message.user), msgDataDefaults); } + if (this.getRocketUser(message.user)) { + RocketChat.models.Messages.createUserLeaveWithRoomIdAndUser(room._id, this.getRocketUser(message.user), msgDataDefaults); + } } else if (message.subtype === 'me_message') { const msgObj = { ...msgDataDefaults, @@ -288,15 +290,15 @@ Importer.Slack = class extends Importer.Base { if (this.getRocketUser(message.user)) { RocketChat.models.Messages.createRoomSettingsChangedWithTypeRoomIdMessageAndUser('room_changed_description', room._id, message.purpose, this.getRocketUser(message.user), msgDataDefaults); } - } else if (message.subtype === 'channel_topic') { + } else if (message.subtype === 'channel_topic') { if (this.getRocketUser(message.user)) { RocketChat.models.Messages.createRoomSettingsChangedWithTypeRoomIdMessageAndUser('room_changed_topic', room._id, message.topic, this.getRocketUser(message.user), msgDataDefaults); } - } else if (message.subtype === 'channel_name') { + } else if (message.subtype === 'channel_name') { if (this.getRocketUser(message.user)) { RocketChat.models.Messages.createRoomRenamedWithRoomIdRoomNameAndUser(room._id, message.name, this.getRocketUser(message.user), msgDataDefaults); } - } else if (message.subtype === 'pinned_item') { + } else if (message.subtype === 'pinned_item') { if (message.attachments) { const msgObj = { ...msgDataDefaults, @@ -323,7 +325,7 @@ Importer.Slack = class extends Importer.Base { }; this.uploadFile(details, message.file.url_private_download, this.getRocketUser(message.user), room, new Date(parseInt(message.ts.split('.')[0]) * 1000)); } - } else if (!missedTypes[message.subtype] && !ignoreTypes[message.subtype]) { + } else if (!missedTypes[message.subtype] && !ignoreTypes[message.subtype]) { missedTypes[message.subtype] = message; } } else { diff --git a/packages/rocketchat-importer/server/classes/ImporterBase.js b/packages/rocketchat-importer/server/classes/ImporterBase.js index dc2429cf3562..38420f695863 100644 --- a/packages/rocketchat-importer/server/classes/ImporterBase.js +++ b/packages/rocketchat-importer/server/classes/ImporterBase.js @@ -12,6 +12,11 @@ // getProgress: => // #return the progress report, tbd what is expected // @version 1.0.0 +import http from 'http'; +import https from 'https'; +import AdmZip from 'adm-zip'; +import getFileType from 'file-type'; + Importer.Base = class Base { static getBSONSize(object) { // The max BSON object size we can store in MongoDB is 16777216 bytes @@ -41,11 +46,12 @@ Importer.Base = class Base { // @param [String] mimeType the of the expected file type // constructor(name, description, mimeType) { - this.MaxBSONSize = 8000000; - this.http = Npm.require('http'); - this.https = Npm.require('https'); - + this.http = http; + this.https = https; + this.AdmZip = AdmZip; + this.getFileType = getFileType; + this.MaxBSONSize = 8000000; this.prepare = this.prepare.bind(this); this.startImport = this.startImport.bind(this); this.getSelection = this.getSelection.bind(this); @@ -61,8 +67,6 @@ Importer.Base = class Base { this.logger = new Logger(`${ this.name } Importer`, {}); this.progress = new Importer.Progress(this.name); this.collection = Importer.RawImports; - this.AdmZip = Npm.require('adm-zip'); - this.getFileType = Npm.require('file-type'); const importId = Importer.Imports.insert({ 'type': this.name, 'ts': Date.now(), 'status': this.progress.step, 'valid': true, 'user': Meteor.user()._id }); this.importRecord = Importer.Imports.findOne(importId); this.users = {}; @@ -187,58 +191,63 @@ Importer.Base = class Base { // uploadFile(details, fileUrl, user, room, timeStamp) { this.logger.debug(`Uploading the file ${ details.name } from ${ fileUrl }.`); - const requestModule = /https/i.test(fileUrl) ? Importer.Base.https : Importer.Base.http; - - return requestModule.get(fileUrl, Meteor.bindEnvironment(function(stream) { - const fileStore = FileUpload.getStore('Uploads'); - fileStore.insert(details, stream, function(err, file) { - if (err) { - throw new Error(err); - } else { - const url = file.url.replace(Meteor.absoluteUrl(), '/'); - - const attachment = { - title: file.name, - title_link: url - }; - - if (/^image\/.+/.test(file.type)) { - attachment.image_url = url; - attachment.image_type = file.type; - attachment.image_size = file.size; - attachment.image_dimensions = file.identify != null ? file.identify.size : undefined; - } - - if (/^audio\/.+/.test(file.type)) { - attachment.audio_url = url; - attachment.audio_type = file.type; - attachment.audio_size = file.size; + const requestModule = /https/i.test(fileUrl) ? this.https : this.http; + + const fileStore = FileUpload.getStore('Uploads'); + + return requestModule.get(fileUrl, Meteor.bindEnvironment(function(res) { + const rawData = []; + res.on('data', chunk => rawData.push(chunk)); + res.on('end', Meteor.bindEnvironment(() => { + fileStore.insert(details, Buffer.concat(rawData), function(err, file) { + if (err) { + throw new Error(err); + } else { + const url = file.url.replace(Meteor.absoluteUrl(), '/'); + + const attachment = { + title: file.name, + title_link: url + }; + + if (/^image\/.+/.test(file.type)) { + attachment.image_url = url; + attachment.image_type = file.type; + attachment.image_size = file.size; + attachment.image_dimensions = file.identify != null ? file.identify.size : undefined; + } + + if (/^audio\/.+/.test(file.type)) { + attachment.audio_url = url; + attachment.audio_type = file.type; + attachment.audio_size = file.size; + } + + if (/^video\/.+/.test(file.type)) { + attachment.video_url = url; + attachment.video_type = file.type; + attachment.video_size = file.size; + } + + const msg = { + rid: details.rid, + ts: timeStamp, + msg: '', + file: { + _id: file._id + }, + groupable: false, + attachments: [attachment] + }; + + if ((details.message_id != null) && (typeof details.message_id === 'string')) { + msg['_id'] = details.message_id; + } + + return RocketChat.sendMessage(user, msg, room, true); } - - if (/^video\/.+/.test(file.type)) { - attachment.video_url = url; - attachment.video_type = file.type; - attachment.video_size = file.size; - } - - const msg = { - rid: details.rid, - ts: timeStamp, - msg: '', - file: { - _id: file._id - }, - groupable: false, - attachments: [attachment] - }; - - if ((details.message_id != null) && (typeof details.message_id === 'string')) { - msg['_id'] = details.message_id; - } - - return RocketChat.sendMessage(user, msg, room, true); - } - }); + }); + })); })); } };