|
|
|
|
@ -12,26 +12,25 @@ Meteor.methods |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
if service is 'url' |
|
|
|
|
headReq = request.headSync dataURI |
|
|
|
|
|
|
|
|
|
if headReq.response.statusCode != 200 |
|
|
|
|
console.log "Not a valid response, #{headReq.response.statusCode}, from the avatar url:", dataURI |
|
|
|
|
throw new Meteor.Error('invalid-avatar-url', '[methods] setAvatarFromService -> url service -> error on checking the image type') |
|
|
|
|
|
|
|
|
|
if headReq.response.headers['content-type'] isnt 'image/jpeg' |
|
|
|
|
throw new Meteor.Error('invalid-image-url', '[methods] setAvatarFromService -> url service -> Invalid url, it is not a jpeg') |
|
|
|
|
|
|
|
|
|
image = request.getSync dataURI, { encoding: null } |
|
|
|
|
ars = RocketChatFile.bufferToStream image.body |
|
|
|
|
aws = RocketChatFileAvatarInstance.createWriteStream "#{user.username}.jpg", headReq.response.headers['content-type'] |
|
|
|
|
aws.on 'end', Meteor.bindEnvironment -> |
|
|
|
|
Meteor.setTimeout -> |
|
|
|
|
console.log "Set #{user.username}'s avatar from the url: #{dataURI}" |
|
|
|
|
RocketChat.models.Users.setAvatarOrigin user._id, service |
|
|
|
|
RocketChat.Notifications.notifyAll 'updateAvatar', { username: user.username } |
|
|
|
|
, 500 |
|
|
|
|
|
|
|
|
|
ars.pipe(aws) |
|
|
|
|
try |
|
|
|
|
result = HTTP.get dataURI, npmRequestOptions: {encoding: 'binary'} |
|
|
|
|
|
|
|
|
|
if result.statusCode isnt 200 |
|
|
|
|
console.log "Not a valid response, #{result.statusCode}, from the avatar url: #{dataURI}" |
|
|
|
|
throw new Meteor.Error('invalid-avatar-url', '[methods] setAvatarFromService -> url service -> error on getting the avatar from url') |
|
|
|
|
|
|
|
|
|
ars = RocketChatFile.bufferToStream new Buffer(result.content, 'binary') |
|
|
|
|
aws = RocketChatFileAvatarInstance.createWriteStream "#{user.username}.jpg", result.headers['content-type'] |
|
|
|
|
aws.on 'end', Meteor.bindEnvironment -> |
|
|
|
|
Meteor.setTimeout -> |
|
|
|
|
console.log "Set #{user.username}'s avatar from the url: #{dataURI}" |
|
|
|
|
RocketChat.models.Users.setAvatarOrigin user._id, service |
|
|
|
|
RocketChat.Notifications.notifyAll 'updateAvatar', { username: user.username } |
|
|
|
|
, 500 |
|
|
|
|
|
|
|
|
|
ars.pipe(aws) |
|
|
|
|
catch e |
|
|
|
|
throw e |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
{image, contentType} = RocketChatFile.dataURIParse dataURI |
|
|
|
|
|