Skip to content

Commit

Permalink
Fix file upload on Slack import
Browse files Browse the repository at this point in the history
Closes #7447
  • Loading branch information
sampaiodiego committed Jul 11, 2017
1 parent b46dcc7 commit 4821510
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 62 deletions.
12 changes: 7 additions & 5 deletions packages/rocketchat-importer-slack/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
123 changes: 66 additions & 57 deletions packages/rocketchat-importer/server/classes/ImporterBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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);
}
});
});
}));
}));
}
};

0 comments on commit 4821510

Please sign in to comment.