-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
60 lines (47 loc) · 1.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
var fs = require('fs');
var path = require('path');
var express = require('express');
var svg2png = require('svg2png');
var favicon = require('serve-favicon');
var app = express();
var template = fs.readFileSync(path.join(__dirname, 'template.svg'), 'utf8');
var re = new RegExp('{{fill}}');
var outdir = path.join(__dirname, 'public');
fs.mkdir(outdir, function () {});
app.use(favicon(__dirname + '/favicon.ico'));
app.use(express.static(outdir));
app.get(/(.*?)\.png$/, function(req, res, next){
var url = req.params[0];
url = url.replace(/.png$/, '');
url = url.replace(/[^a-zA-Z0-9_\-]+/g, '');
fs.writeFile(path.join(outdir, url + '.svg'), template.replace(re, 'hsl(' + hash(url, true) + ',100%,50%)'), 'utf8', function (error) {
if (error) {
console.error(error);
return next(error);
}
svg2png(path.join(outdir, url + '.svg'), path.join(outdir, url + '.png'), function (error) {
if (error) {
console.error(error);
return next(error);
}
url = '/' + url + '.png';
console.log('done and redirect to ' + url);
res.redirect(url);
});
});
});
app.listen(process.env.PORT || 8000);
function hash(seed) {
/*jshint bitwise:false */
var i, l;
// username as seed instead of a string and a possible string?
var hval = (seed === undefined) ? 0x811c9dc5 : seed;
// i didn't change this, i have absolutely no knowledge of creating hashes.
for (i = 0, l = seed.length; i < l; i++) {
hval ^= seed.charCodeAt(i);
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
// max of 8 numbers to prevent hitting upper level, no
return ((hval >>> 0).toString()).substr(-8);
}