-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoogleFontsRepo.js
98 lines (85 loc) · 3.76 KB
/
googleFontsRepo.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
/**
* Created by rweneck on 3/11/15.
*/
var port = process.env.PORT || 8090;
var express = require('express');
var bodyParser = require('body-parser');
var httpreq = require('httpreq');
var archiver = require('archiver');
var stringStream = require('string-stream');
console.log('Starting font repo server up on port ' + port);
var fontRouter = express.Router();
fontRouter.use(bodyParser.json());
fontRouter['get'](
'/*',
function(req, res) {
var errors = [];
var params, rep;
var requestUrl = 'http://fonts.googleapis.com' + '/css'+req.originalUrl.substring(req.originalUrl.indexOf('?'));
var indexOfFamilySeperator = req.originalUrl.indexOf(":");
if(indexOfFamilySeperator<0){
indexOfFamilySeperator = req.originalUrl.length;
}
var requestedFontName = req.originalUrl.substring(req.originalUrl.indexOf('=')+1, indexOfFamilySeperator);
requestedFontName = requestedFontName.replace(/\+/g, "");
requestedFontName = requestedFontName.charAt(0).toLocaleLowerCase()+requestedFontName.substr(1);
var requestedPackageName = req.originalUrl.substring(req.originalUrl.lastIndexOf("/")+1, req.originalUrl.indexOf('?'));
httpreq.get(requestUrl, function (err, response){
if (err){
console.log(err);
res.send(500, {error:err});
}else {
if (response.statusCode !== 200) {
res.status(response.statusCode).send(response.body);
return
}
var cssString = response.body;
var files = [];
var completion = function(){
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-disposition', 'attachment; filename='+requestedFontName+'.zip');
var dl = archiver('zip');
dl.pipe(res);
dl.append(new stringStream(cssString), {name:requestedFontName+'.css'});
files.forEach(function(file){
dl.append(file.file, {name:file.name});
});
dl.append(JSON.stringify({
"name": requestedPackageName,
"main": [
"*.ttf",
"*.css"
]
}), { name: 'bower.json' });
dl.finalize(function (err) {
if (err) res.send(500);
})
};
//get all the url's within the css
var fontReferences = cssString.match(/url\(.*?\)/gi);
fontReferences.forEach(function (fontReference) {
var reference = fontReference.substring(4, fontReference.length - 1);
var fontName = reference.substring(fontReference.lastIndexOf("/"));
cssString = cssString.replace(reference, fontName);
httpreq.get(reference, {binary: true}, function (err, response) {
if (err) {
console.log(err);
} else {
files.push({
name:fontName,
file:response.body
});
}
if(files.length === fontReferences.length){
completion();
}
});
});
}
});
});
var app = express()
app.use('/googleFonts', fontRouter);
app.use('/', express.static(__dirname + '/'));
app.listen(port);
console.log('Started font repo server up on port ' + port);