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-lib/server/functions/setUserAvatar.js

64 lines
2.4 KiB

import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';
import { RocketChatFile } from 'meteor/rocketchat:file';
Convert rocketchat-file-upload to main module structure (#13094) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix missed export * Fix canBeDeleted and canBeCreated function, remove async
7 years ago
import { FileUpload } from 'meteor/rocketchat:file-upload';
RocketChat.setUserAvatar = function(user, dataURI, contentType, service) {
let encoding;
let image;
if (service === 'initials') {
return RocketChat.models.Users.setAvatarOrigin(user._id, service);
} else if (service === 'url') {
let result = null;
try {
result = HTTP.get(dataURI, { npmRequestOptions: { encoding: 'binary', rejectUnauthorized: false } });
} catch (error) {
if (!error.response || error.response.statusCode !== 404) {
console.log(`Error while handling the setting of the avatar from a url (${ dataURI }) for ${ user.username }:`, error);
throw new Meteor.Error('error-avatar-url-handling', `Error while handling avatar setting from a URL (${ dataURI }) for ${ user.username }`, { function: 'RocketChat.setUserAvatar', url: dataURI, username: user.username });
}
}
if (result.statusCode !== 200) {
console.log(`Not a valid response, ${ result.statusCode }, from the avatar url: ${ dataURI }`);
throw new Meteor.Error('error-avatar-invalid-url', `Invalid avatar URL: ${ dataURI }`, { function: 'RocketChat.setUserAvatar', url: dataURI });
}
if (!/image\/.+/.test(result.headers['content-type'])) {
console.log(`Not a valid content-type from the provided url, ${ result.headers['content-type'] }, from the avatar url: ${ dataURI }`);
throw new Meteor.Error('error-avatar-invalid-url', `Invalid avatar URL: ${ dataURI }`, { function: 'RocketChat.setUserAvatar', url: dataURI });
}
encoding = 'binary';
image = result.content;
contentType = result.headers['content-type'];
} else if (service === 'rest') {
encoding = 'binary';
image = dataURI;
} else {
const fileData = RocketChatFile.dataURIParse(dataURI);
encoding = 'base64';
image = fileData.image;
contentType = fileData.contentType;
}
const buffer = new Buffer(image, encoding);
const fileStore = FileUpload.getStore('Avatars');
fileStore.deleteByName(user.username);
const file = {
userId: user._id,
type: contentType,
size: buffer.length,
};
fileStore.insert(file, buffer, () => {
Meteor.setTimeout(function() {
RocketChat.models.Users.setAvatarOrigin(user._id, service);
RocketChat.Notifications.notifyLogged('updateAvatar', { username: user.username });
}, 500);
});
};