changeset 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
files avatar.js
diffstat 1 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/avatar.js
+++ b/avatar.js
@@ -123,12 +123,25 @@ if (config.useGravatar)
 			if (r.statusCode != 200)
 				return svgError(res, message + ' Additionaly, no gravatar available.');
 
-			res.writeHead(200, {'Content-Type': r.headers['content-type']});
+			var ext;
+			var type = r.headers['content-type']
+			for (var i in config.extensions)
+				if (type == config.extensions[i])
+					ext = i;
+
+			var file = fs.createWriteStream(config.directory + '/' + to + '.' + ext);
+
+			file.on('close', function() {
+				jids[to] = ext;
+				showImage(to, res);
+			});
+
 			r.on('data', function(chunk) {
-				res.write(chunk);
+				file.write(chunk);
 			});
+
 			r.on('end', function() {
-				res.end();
+				file.end();
 			});
 		});
 
@@ -200,7 +213,7 @@ function onIq(stanza) {
 
 		var binval = new Buffer(base64.replace(/\n/g, ''), 'base64');
 
-		fs.writeFile(config.directory+'/'+to+'.'+ext, binval, function() {
+		fs.writeFile(config.directory + '/' + to + '.' + ext, binval, function() {
 			jids[to] = ext;
 			showImage(to, res);
 		});