-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·74 lines (56 loc) · 1.87 KB
/
server.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
/**
* @file fontstore 字体同步服务
* @author Varsha
*/
var http = require('http');
var path = require('path');
var url = require('url');
var fs = require('fs');
var querystring = require('querystring');
function getFontPath(req) {
var urlData = url.parse(req.url, true);
var pathname = urlData.pathname;
if (!pathname.match(/\.(?:font|ttf|woff2?|eot|svg)$/g)) {
pathname += '/bdfont.font';
}
return path.resolve(process.cwd(), path.dirname(pathname).slice(1));
}
function getFontData(data) {
data= Buffer.concat(data).toString();
return querystring.parse(data);
}
function writeFiles(fontData, fontPath) {
var fontName = fontData.fontName;
var encode = fontData.encode;
var fontTypes = fontData.fontType.split(',');
for (var i = 0, len = fontTypes.length; i < len; i++) {
var type = fontTypes[i];
var filePath = path.resolve(fontPath, fontName + '.' + type);
writeFontFile(fontData[type], filePath, encode);
}
}
function writeFontFile(data, filePath, encode) {
try {
fs.writeFileSync(filePath, new Buffer(data, 'base64'), encode);
console.log('>> File: \033[0;32m' + filePath + '\033[0m sync success!');
}
catch (exp) {
console.log('>> \033[0;31mError syncing font data to: ' + filePath
+ '\033[0m\n' + exp.message);
}
}
exports.start = function (options) {
var port = options.port;
var server = http.createServer(function (req, res) {
var fontPath = getFontPath(req);
var data = [];
req.on('data',function(reqData){
data.push(reqData);
});
req.on('end', function () {
var fontData = getFontData(data);
writeFiles(fontData, fontPath);
});
}).listen(port);
console.log('FontStore WebServer Listened at \033[0;32m http://127.0.0.1:' + port, '\033[0m');
}