comparison avatar.js @ 22:9e21bd93f79d

Refactoring of gravatar function.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 16 Jul 2011 15:13:52 +0200
parents 46c42ec52680
children d63a2784564d
comparison
equal deleted inserted replaced
21:46c42ec52680 22:9e21bd93f79d
121 121
122 var r = http.request(options, function(r) { 122 var r = http.request(options, function(r) {
123 if (r.statusCode != 200) 123 if (r.statusCode != 200)
124 return svgError(res, message + ' Additionaly, no gravatar available.'); 124 return svgError(res, message + ' Additionaly, no gravatar available.');
125 125
126 res.writeHead(200, {'Content-Type': r.headers['content-type']}); 126 var ext;
127 var type = r.headers['content-type']
128 for (var i in config.extensions)
129 if (type == config.extensions[i])
130 ext = i;
131
132 var file = fs.createWriteStream(config.directory + '/' + to + '.' + ext);
133
134 file.on('close', function() {
135 jids[to] = ext;
136 showImage(to, res);
137 });
138
127 r.on('data', function(chunk) { 139 r.on('data', function(chunk) {
128 res.write(chunk); 140 file.write(chunk);
129 }); 141 });
142
130 r.on('end', function() { 143 r.on('end', function() {
131 res.end(); 144 file.end();
132 }); 145 });
133 }); 146 });
134 147
135 r.on('error', function(e) { 148 r.on('error', function(e) {
136 return svgError(res, message + ' Additionaly, problem with gravatar request: ' + e.message); 149 return svgError(res, message + ' Additionaly, problem with gravatar request: ' + e.message);
198 return noAvatar(res, to, 'Error: this user’s avatar is in an unknown format.'); 211 return noAvatar(res, to, 'Error: this user’s avatar is in an unknown format.');
199 } 212 }
200 213
201 var binval = new Buffer(base64.replace(/\n/g, ''), 'base64'); 214 var binval = new Buffer(base64.replace(/\n/g, ''), 'base64');
202 215
203 fs.writeFile(config.directory+'/'+to+'.'+ext, binval, function() { 216 fs.writeFile(config.directory + '/' + to + '.' + ext, binval, function() {
204 jids[to] = ext; 217 jids[to] = ext;
205 showImage(to, res); 218 showImage(to, res);
206 }); 219 });
207 } catch (e) { 220 } catch (e) {
208 return noAvatar(res, to, 'Error: this user doesn’t have an avatar in his/her vCard.'); 221 return noAvatar(res, to, 'Error: this user doesn’t have an avatar in his/her vCard.');