forked from metaltoad/memeplacer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemeplacer.js
131 lines (119 loc) · 3.33 KB
/
memeplacer.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs'),
util = require('util'),
child_process = require('child_process');
var sources = [];
fs.readdir('sources', function(err, files) {
for (var i = 0; i < files.length; i++) {
if (files[i].match(/(\d+)x(\d+)\.\d+\.jpg/)) {
sources.push(files[i]);
}
}
http.createServer(listener).listen(process.env.PORT);
});
/**
* The main request listener.
*/
function listener(request, response) {
var uri = url.parse(request.url)['pathname'];
var parts, x, y;
var gray = false;
if (uri.match(/^\/g\//)) {
uri = uri.replace('/g', '');
gray = true;
}
if (
(parts = uri.match(/^\/(\d+)x(\d+)$/)) &&
request.method == 'GET' &&
(x = parseInt(parts[1],10)) && (y = parseInt(parts[2],10)) &&
x >= 1 && x <= 1000 && y >= 1 && y <= 1000) {
streamImage(response, x, y, gray);
} else if (request.method == 'GET' && !uri.match(/\.\./)) {
streamFile(response, uri);
} else {
notFound(response);
}
}
/**
* Stream the image.
*/
function streamImage(response, x, y, gray) {
var geometry = x + 'x' + y;
var params = ['-quality', '75%', '-resize', geometry + '^', '-gravity', 'center', '-extent', geometry, '-gravity', 'center', 'sources/' + findSource(x, y), '-'];
if (gray) {
params.unshift('-colorspace', 'gray');
}
var spawn = child_process.spawn,
convert = spawn('convert', params);
response.writeHead(200, {
'Content-Type': 'image/jpeg',
'Cache-Control': 'public, max-age=99999999'
});
convert.stdout.pipe(response);
}
/**
* Find the closest source image
*/
function findSource(x, y) {
var delta;
var targetAspect = x / y;
var results = [];
for (var i = 0; i < sources.length; i++) {
var parts = sources[i].match(/(\d+)x(\d+)\.\d+\.jpg/);
var sourceAspect = parseInt(parts[1],10) / parseInt(parts[2],10);
if (Math.abs(sourceAspect - targetAspect) < delta || !delta) {
delta = Math.abs(sourceAspect - targetAspect);
results = [sources[i]];
} else if (Math.abs(sourceAspect - targetAspect) == delta) {
results.push(sources[i]);
}
}
return results[Math.floor(Math.random() * results.length)];
}
/**
* A file server made of mud and sticks.
*/
function streamFile(response, uri) {
uri = (uri == '/') ? '/index.html' : uri;
var fileStream = fs.createReadStream('files' + uri);
fileStream.on('error', function () { notFound(response); });
var mime = mimeType(uri);
var cache = 'public, max-age=99999999';
if (mime == 'text/html') {
cache = 'public, max-age=300';
}
response.writeHead(200, {
'Content-Type': mimeType(uri),
'Cache-Control': cache
});
fileStream.pipe(response);
}
/**
* Get the MIME type for a file.
*/
function mimeType(filename) {
var types = {
'.html' : 'text/html',
'.css' : 'text/css',
'.js' : 'application/javascript',
'.jpg' : 'image/jpeg',
'.jpeg' : 'image/jpeg',
'.png' : 'image/png',
'.ico' : 'image/x-icon'
};
var i = filename.lastIndexOf('.');
var extension = (i < 0) ? '' : filename.substr(i);
return types[extension.toLowerCase()] || 'application/octet-stream';
}
/**
* 404.
*/
function notFound(response) {
response.writeHead(404, {
'Content-Type': 'text/html',
'Cache-Control': 'public, max-age=300'
});
response.end('Not Found');
}