1+ import { EmojiCustom } from '@rocket.chat/models' ;
12import { Meteor } from 'meteor/meteor' ;
23import { WebApp } from 'meteor/webapp' ;
34import _ from 'underscore' ;
@@ -8,6 +9,35 @@ import { settings } from '../../../settings/server';
89
910export let RocketChatFileEmojiCustomInstance ;
1011
12+ const writeSvgFallback = ( res , req ) => {
13+ res . setHeader ( 'Content-Type' , 'image/svg+xml' ) ;
14+ res . setHeader ( 'Cache-Control' , 'public, max-age=0' ) ;
15+ res . setHeader ( 'Expires' , '-1' ) ;
16+ res . setHeader ( 'Last-Modified' , 'Thu, 01 Jan 2015 00:00:00 GMT' ) ;
17+
18+ const reqModifiedHeader = req . headers [ 'if-modified-since' ] ;
19+ if ( reqModifiedHeader != null ) {
20+ if ( reqModifiedHeader === 'Thu, 01 Jan 2015 00:00:00 GMT' ) {
21+ res . writeHead ( 304 ) ;
22+ res . end ( ) ;
23+ return ;
24+ }
25+ }
26+
27+ const color = '#000' ;
28+ const initials = '?' ;
29+
30+ const svg = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
31+ <svg xmlns="http://www.w3.org/2000/svg" pointer-events="none" width="50" height="50" style="width: 50px; height: 50px; background-color: ${ color } ;">
32+ <text text-anchor="middle" y="50%" x="50%" dy="0.36em" pointer-events="auto" fill="#ffffff" font-family="Helvetica, Arial, Lucida Grande, sans-serif" style="font-weight: 400; font-size: 28px;">
33+ ${ initials }
34+ </text>
35+ </svg>` ;
36+
37+ res . write ( svg ) ;
38+ res . end ( ) ;
39+ } ;
40+
1141Meteor . startup ( ( ) => {
1242 let storeType = 'GridFS' ;
1343
@@ -48,39 +78,19 @@ Meteor.startup(() => {
4878 return ;
4979 }
5080
51- const file = await RocketChatFileEmojiCustomInstance . getFileWithReadStream ( encodeURIComponent ( params . emoji ) ) ;
52-
5381 res . setHeader ( 'Content-Disposition' , 'inline' ) ;
5482
83+ const emoji = await EmojiCustom . findOneByName ( params . emoji . split ( '.' ) [ 0 ] , { projection : { _id : 1 } } ) ;
84+
85+ if ( ! emoji ) {
86+ return writeSvgFallback ( res , req ) ;
87+ }
88+
89+ const file = await RocketChatFileEmojiCustomInstance . getFileWithReadStream ( encodeURIComponent ( params . emoji ) ) ;
90+
5591 if ( ! file ) {
5692 // use code from username initials renderer until file upload is complete
57- res . setHeader ( 'Content-Type' , 'image/svg+xml' ) ;
58- res . setHeader ( 'Cache-Control' , 'public, max-age=0' ) ;
59- res . setHeader ( 'Expires' , '-1' ) ;
60- res . setHeader ( 'Last-Modified' , 'Thu, 01 Jan 2015 00:00:00 GMT' ) ;
61-
62- const reqModifiedHeader = req . headers [ 'if-modified-since' ] ;
63- if ( reqModifiedHeader != null ) {
64- if ( reqModifiedHeader === 'Thu, 01 Jan 2015 00:00:00 GMT' ) {
65- res . writeHead ( 304 ) ;
66- res . end ( ) ;
67- return ;
68- }
69- }
70-
71- const color = '#000' ;
72- const initials = '?' ;
73-
74- const svg = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
75- <svg xmlns="http://www.w3.org/2000/svg" pointer-events="none" width="50" height="50" style="width: 50px; height: 50px; background-color: ${ color } ;">
76- <text text-anchor="middle" y="50%" x="50%" dy="0.36em" pointer-events="auto" fill="#ffffff" font-family="Helvetica, Arial, Lucida Grande, sans-serif" style="font-weight: 400; font-size: 28px;">
77- ${ initials }
78- </text>
79- </svg>` ;
80-
81- res . write ( svg ) ;
82- res . end ( ) ;
83- return ;
93+ return writeSvgFallback ( res , req ) ;
8494 }
8595
8696 const fileUploadDate = file . uploadDate != null ? file . uploadDate . toUTCString ( ) : undefined ;
0 commit comments