|
1 | 1 | import { Meteor } from 'meteor/meteor'; |
2 | 2 | import limax from 'limax'; |
| 3 | +import sharp from 'sharp'; |
3 | 4 |
|
4 | 5 | import { hasPermission } from '../../../authorization'; |
5 | 6 | import { RocketChatFile } from '../../../file'; |
6 | 7 | import { RocketChatFileEmojiCustomInstance } from '../startup/emoji-custom'; |
7 | 8 | import { api } from '../../../../server/sdk/api'; |
8 | 9 |
|
| 10 | +const getFile = async (file, extension) => { |
| 11 | + if (extension !== 'svg+xml') { |
| 12 | + return file; |
| 13 | + } |
| 14 | + |
| 15 | + return sharp(file) |
| 16 | + .png() |
| 17 | + .toBuffer(); |
| 18 | +}; |
| 19 | + |
9 | 20 | Meteor.methods({ |
10 | | - uploadEmojiCustom(binaryContent, contentType, emojiData) { |
| 21 | + async uploadEmojiCustom(binaryContent, contentType, emojiData) { |
11 | 22 | if (!hasPermission(this.userId, 'manage-emoji')) { |
12 | 23 | throw new Meteor.Error('not_authorized'); |
13 | 24 | } |
14 | 25 |
|
15 | 26 | emojiData.name = limax(emojiData.name, { replacement: '_' }); |
16 | 27 | // delete aliases for notification purposes. here, it is a string rather than an array |
17 | 28 | delete emojiData.aliases; |
18 | | - const file = new Buffer(binaryContent, 'binary'); |
| 29 | + |
| 30 | + const file = await getFile(Buffer.from(binaryContent, 'binary'), emojiData.extension); |
| 31 | + |
| 32 | + emojiData.extension = emojiData.extension === 'svg+xml' ? 'png' : emojiData.extension; |
19 | 33 |
|
20 | 34 | const rs = RocketChatFile.bufferToStream(file); |
21 | 35 | RocketChatFileEmojiCustomInstance.deleteFile(encodeURIComponent(`${ emojiData.name }.${ emojiData.extension }`)); |
|
0 commit comments