Uploads: Fix review

pull/6788/head
Rodrigo Nascimento 9 years ago
parent 9ee948e415
commit 0ce0be0f02
  1. 6
      packages/rocketchat-file-upload/client/lib/fileUploadHandler.js
  2. 2
      packages/rocketchat-file-upload/lib/FileUploadBase.js
  3. 31
      packages/rocketchat-file-upload/server/lib/FileUpload.js
  4. 4
      packages/rocketchat-file-upload/ufs/AmazonS3/server.js
  5. 4
      packages/rocketchat-file-upload/ufs/GoogleStorage/server.js
  6. 85
      packages/rocketchat-importer/server/classes/ImporterBase.coffee
  7. 7
      packages/rocketchat-lib/server/functions/setUserAvatar.js
  8. 94
      packages/rocketchat-slackbridge/slackbridge.js

@ -29,6 +29,8 @@ fileUploadHandler = (directive, meta, file) => {
};
Tracker.autorun(function() {
document.cookie = `rc_uid=${ escape(Meteor.userId()) }; path=/`;
document.cookie = `rc_token=${ escape(Accounts._storedLoginToken()) }; path=/`;
if (Meteor.userId()) {
document.cookie = `rc_uid=${ escape(Meteor.userId()) }; path=/`;
document.cookie = `rc_token=${ escape(Accounts._storedLoginToken()) }; path=/`;
}
});

@ -19,8 +19,6 @@ FileUploadBase = class FileUploadBase {
this.id = Random.id();
this.meta = meta;
this.file = file;
console.log(store.options.name, {meta, file});
this.store = store;
}

@ -1,6 +1,7 @@
/* globals UploadFS */
import fs from 'fs';
import stream from 'stream';
import mime from 'mime-type/with-db';
import Future from 'fibers/future';
@ -256,9 +257,33 @@ export class FileUploadClass {
return this.delete(file._id);
}
insert(file, stream, cb) {
const fileId = this.store.create(file);
insert(fileData, streamOrBuffer, cb) {
const fileId = this.store.create(fileData);
const token = this.store.createToken(fileId);
const tmpFile = UploadFS.getTempFilePath(fileId);
this.store.write(stream, fileId, cb);
try {
if (streamOrBuffer instanceof stream) {
stream.pipe(fs.createWriteStream(tmpFile));
} else if (streamOrBuffer instanceof Buffer) {
fs.writeFileSync(tmpFile, streamOrBuffer);
} else {
throw new Error('Invalid file type');
}
const file = Meteor.call('ufsComplete', fileId, this.name, token);
if (cb) {
cb(null, file);
}
return file;
} catch (e) {
if (cb) {
cb(e);
} else {
throw e;
}
}
}
}

@ -63,6 +63,10 @@ export class AmazonS3Store extends UploadFS.Store {
this.create = function(file, callback) {
check(file, Object);
if (file._id == null) {
file._id = Random.id();
}
file.AmazonS3 = {
path: this.options.getPath(file)
};

@ -48,6 +48,10 @@ export class GoogleStorageStore extends UploadFS.Store {
this.create = function(file, callback) {
check(file, Object);
if (file._id == null) {
file._id = Random.id();
}
file.GoogleStorage = {
path: this.options.getPath(file)
};

@ -161,48 +161,45 @@ Importer.Base = class Importer.Base
requestModule = if /https/i.test(fileUrl) then Importer.Base.https else Importer.Base.http
requestModule.get fileUrl, Meteor.bindEnvironment((stream) ->
fileStore = UploadFS.getStore('Uploads')
fileId = fileStore.create details
if fileId
fileStore.write stream, fileId, (err, file) ->
if err
throw new Error(err)
else
url = file.url.replace(Meteor.absoluteUrl(), '/')
attachment =
title: "File Uploaded: #{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?.size
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
msg =
rid: details.rid
ts: timeStamp
msg: ''
file:
_id: file._id
groupable: false
attachments: [attachment]
if details.message_id? and (typeof details.message_id is 'string')
msg['_id'] = details.message_id
RocketChat.sendMessage user, msg, room, true
else
@logger.error "Failed to create the store for #{fileUrl}!!!"
fileStore = FileUpload.getStore('Uploads')
fileStore.insert details, stream, (err, file) ->
if err
throw new Error(err)
else
url = file.url.replace(Meteor.absoluteUrl(), '/')
attachment =
title: "File Uploaded: #{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?.size
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
msg =
rid: details.rid
ts: timeStamp
msg: ''
file:
_id: file._id
groupable: false
attachments: [attachment]
if details.message_id? and (typeof details.message_id is 'string')
msg['_id'] = details.message_id
RocketChat.sendMessage user, msg, room, true
)

@ -39,16 +39,17 @@ RocketChat.setUserAvatar = function(user, dataURI, contentType, service) {
contentType = fileData.contentType;
}
const rs = RocketChatFile.bufferToStream(new Buffer(image, encoding));
const buffer = new Buffer(image, encoding);
const fileStore = FileUpload.getStore('Avatars');
fileStore.deleteByName(user.username);
const file = {
userId: user._id,
type: contentType
type: contentType,
size: buffer.length
};
fileStore.insert(file, rs, () => {
fileStore.insert(file, buffer, () => {
Meteor.setTimeout(function() {
RocketChat.models.Users.setAvatarOrigin(user._id, service);
RocketChat.Notifications.notifyLogged('updateAvatar', {username: user.username});

@ -592,59 +592,57 @@ class SlackBridge {
const parsedUrl = url.parse(slackFileURL, true);
parsedUrl.headers = { 'Authorization': `Bearer ${ this.apiToken }` };
requestModule.get(parsedUrl, Meteor.bindEnvironment((stream) => {
const fileStore = UploadFS.getStore('Uploads');
const fileId = fileStore.create(details);
if (fileId) {
fileStore.write(stream, fileId, (err, file) => {
if (err) {
throw new Error(err);
} else {
const url = file.url.replace(Meteor.absoluteUrl(), '/');
const attachment = {
title: `File Uploaded: ${ 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 && file.identify.size;
}
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 fileStore = FileUpload.getStore('Uploads');
const msg = {
rid: details.rid,
ts: timeStamp,
msg: '',
file: {
_id: file._id
},
groupable: false,
attachments: [attachment]
};
fileStore.insert(details, stream, (err, file) => {
if (err) {
throw new Error(err);
} else {
const url = file.url.replace(Meteor.absoluteUrl(), '/');
const attachment = {
title: `File Uploaded: ${ file.name }`,
title_link: url
};
if (isImporting) {
msg.imported = 'slackbridge';
}
if (/^image\/.+/.test(file.type)) {
attachment.image_url = url;
attachment.image_type = file.type;
attachment.image_size = file.size;
attachment.image_dimensions = file.identify && file.identify.size;
}
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;
}
if (details.message_id && (typeof details.message_id === 'string')) {
msg['_id'] = details.message_id;
}
const msg = {
rid: details.rid,
ts: timeStamp,
msg: '',
file: {
_id: file._id
},
groupable: false,
attachments: [attachment]
};
return RocketChat.sendMessage(rocketUser, msg, rocketChannel, true);
if (isImporting) {
msg.imported = 'slackbridge';
}
});
}
if (details.message_id && (typeof details.message_id === 'string')) {
msg['_id'] = details.message_id;
}
return RocketChat.sendMessage(rocketUser, msg, rocketChannel, true);
}
});
}));
}

Loading…
Cancel
Save