-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
185 lines (165 loc) · 5.24 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var http = require('http'),
fs = require('fs'),
path = require('path'),
url = require('url'),
express = require("express"),
app = express(),
bodyParser = require("body-parser"),
events = require('events'),
eventEmitter = new events.EventEmitter(),
archiver = require('archiver'),
mkdirp = require('mkdirp'),
rimraf = require( 'rimraf'),
PORT = "1337",
DOWNLOAD_FOLDER_BASE = "resources_",
FONT_DIRECTORY = "fonts",
IMAGE_DIRECTORY = "images",
ZIP_DIRECTORY = "zip",
REGEX = /url\([^\)]*?(\.jpg|\.png|\.gif|\.eot|\.woff|\.woff2|\.svg|\.otf|\.ttf)[^\)]*?\)/ig;
function downloadFromCssFile(url_string, folder){
var url_base = url.parse(url_string).host,
file_path = url.parse(url_string).path,
path_name = path.dirname(file_path) + "/",
cssOptions = {
host: url_base,
port: 80,
path: file_path
};
//Make directories
mkdirp(path.join(__dirname, folder, FONT_DIRECTORY), function(err) {});
mkdirp(path.join(__dirname, folder, IMAGE_DIRECTORY), function(err) {});
mkdirp(path.join(__dirname, ZIP_DIRECTORY), function(err) {});
http.get(cssOptions, function(res){
var cssdata = '';
res.setEncoding('binary');
//On Data
res.on('data', function(chunk){
cssdata += chunk;
})
//On End
res.on('end', function(){
var resources = cssdata.match(REGEX);
if (resources == null) {
eventEmitter.emit('noResources' + folder);
} else {
downloadResources(resources, resources.length - 1, url_base, path_name, folder);
}
})
});
}
//Save Zip File
function saveZip(folder){
var archive = archiver.create('zip', {});
var output = fs.createWriteStream(path.join(ZIP_DIRECTORY, folder + '.zip'));
archive.directory(folder, '');
archive.pipe(output);
output.on('finish', function(){
eventEmitter.emit('doneZipping' + folder);
})
archive.finalize();
}
//Download Resources function
function downloadResources(resources, length, url_base, path_name, folder){
downloadResource(length);
function downloadResource(count) {
var resource = resources[count].replace(/url\((.*?)\)/g, '$1').replace(/'/g, "").replace(/"/g, "").replace(/\s/g, ""),
host = url.parse(resource).host != null ? url.parse(resource).host : url_base,
_path = resource.match(/^\//) != null || url.parse(resource).protocol != null ? "" : path_name;
var options = {
host: host,
port: 80,
path: _path + resource
}
console.log(host + _path + resource);
//HTTP request
http.get(options, function(res){
var resourcedata = '',
resourceName = url.parse(path.basename(resource)).pathname;
res.setEncoding('binary');
//On Data
res.on('data', function(chunk){
resourcedata += chunk;
})
//On End
res.on('end', function(){
//Save to correct folder
var extName = path.extname(resourceName),
resourceFolder = (extName == '.jpg' || extName == '.gif' || extName == '.png') ? IMAGE_DIRECTORY : FONT_DIRECTORY;
//Write file
fs.writeFile(path.join(folder, resourceFolder, resourceName), resourcedata, 'binary', function(err){
if (err) throw err
console.log('File saved: ' + host + _path + resource);
//Recursively download - so it will download one at a time
if (count > 0) {
downloadResource(count - 1)
} else {
eventEmitter.emit('doneDownloading' + folder);
}
})
})
})
}
}
//Server
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
//Get Resources
app.post('/getResourcesFromUrl', function(req,res){
var stylesheetUrl = req.body.stylesheetUrl,
folderName = DOWNLOAD_FOLDER_BASE + Date.now();
//Fix stylesheet URL if missing protocol
stylesheetUrl = (url.parse(stylesheetUrl).protocol == null) ? "http://" + stylesheetUrl : stylesheetUrl;
//Fix stylesheet URL - Remove query string
stylesheetUrl = "http://" + url.parse(stylesheetUrl).host + url.parse(stylesheetUrl).pathname;
//Error if not valid URL
if (path.extname(stylesheetUrl) != ".css") {
res.status(500).send({ error: 'Invalid URL.' });
} else {
//Download resources
downloadFromCssFile(stylesheetUrl, folderName);
}
//Zip resources when finished
eventEmitter.on('doneDownloading' + folderName, function(){
saveZip(folderName);
});
eventEmitter.on('doneZipping' + folderName, function(){
//Send Success
var response = {
status : 200,
success : 'Updated Successfully',
zipFile : folderName + ".zip"
}
res.end(JSON.stringify(response));
//Delete resource directory
rimraf.sync(path.join(__dirname, folderName));
});
//Error if no resources to download
eventEmitter.on('noResources' + folderName, function(){
res.status(500).send({ error: 'No resources to download.' });
});
});
//Return Zip File
app.get('/getZipFile/:fileName.zip',function(request,response, next){
var rs = fs.createReadStream(path.resolve(__dirname, ZIP_DIRECTORY, request.params.fileName + '.zip'));
rs.on('error', function (error) {
var err = new Error();
err.status = 404;
next(err);
});
rs.on('open', function () {
var ct = "application/zip";
response.writeHead(200, { "Content-Type" : ct });
rs.pipe(response);
});
});
// handling 404 errors
app.use(function(err, req, res, next) {
if(err.status !== 404) {
return next();
}
res.send(err.message || 'Not found');
});
//Start Server
app.listen(PORT,function(){
console.log("Started on PORT " + PORT);
})