├── Procfile ├── favicon.ico ├── template.svg ├── package.json └── index.js /Procfile: -------------------------------------------------------------------------------- 1 | web: node index.js 2 | -------------------------------------------------------------------------------- /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jsbin/gravatar/master/favicon.ico -------------------------------------------------------------------------------- /template.svg: -------------------------------------------------------------------------------- 1 | jsbin-avatar.svg -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jsbin-gravatar", 3 | "version": "0.0.0", 4 | "description": "Default gravatar for jsbin", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/jsbin/gravatar.git" 12 | }, 13 | "keywords": [ 14 | "jsbin" 15 | ], 16 | "author": "Remy Sharp", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/jsbin/gravatar/issues" 20 | }, 21 | "homepage": "https://github.com/jsbin/gravatar", 22 | "dependencies": { 23 | "svg2png": "~1.1.0", 24 | "express": "~4.8.3", 25 | "hbs": "~2.7.0", 26 | "serve-favicon": "~2.0.1" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | var express = require('express'); 5 | var svg2png = require('svg2png'); 6 | var favicon = require('serve-favicon'); 7 | var app = express(); 8 | 9 | var template = fs.readFileSync(path.join(__dirname, 'template.svg'), 'utf8'); 10 | var re = new RegExp('{{fill}}'); 11 | 12 | var outdir = path.join(__dirname, 'public'); 13 | 14 | fs.mkdir(outdir, function () {}); 15 | 16 | app.use(favicon(__dirname + '/favicon.ico')); 17 | app.use(express.static(outdir)); 18 | 19 | app.get(/(.*?)\.png$/, function(req, res, next){ 20 | var url = req.params[0]; 21 | url = url.replace(/.png$/, ''); 22 | url = url.replace(/[^a-zA-Z0-9_\-]+/g, ''); 23 | 24 | fs.writeFile(path.join(outdir, url + '.svg'), template.replace(re, 'hsl(' + hash(url, true) + ',100%,50%)'), 'utf8', function (error) { 25 | if (error) { 26 | console.error(error); 27 | return next(error); 28 | } 29 | 30 | svg2png(path.join(outdir, url + '.svg'), path.join(outdir, url + '.png'), function (error) { 31 | if (error) { 32 | console.error(error); 33 | return next(error); 34 | } 35 | url = '/' + url + '.png'; 36 | console.log('done and redirect to ' + url); 37 | res.redirect(url); 38 | }); 39 | }); 40 | }); 41 | 42 | app.listen(process.env.PORT || 8000); 43 | 44 | 45 | 46 | function hash(seed) { 47 | /*jshint bitwise:false */ 48 | var i, l; 49 | // username as seed instead of a string and a possible string? 50 | var hval = (seed === undefined) ? 0x811c9dc5 : seed; 51 | 52 | // i didn't change this, i have absolutely no knowledge of creating hashes. 53 | for (i = 0, l = seed.length; i < l; i++) { 54 | hval ^= seed.charCodeAt(i); 55 | hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); 56 | } 57 | 58 | // max of 8 numbers to prevent hitting upper level, no 59 | return ((hval >>> 0).toString()).substr(-8); 60 | } 61 | --------------------------------------------------------------------------------