-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
158 lines (120 loc) · 3.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
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
var express = require('express');
var path = require("path");
var bodyParser = require('body-parser');
var Pageres = require('pageres');
var fs = require('fs');
var resemble = require('node-resemble-js');
var emptyDir = require('empty-dir');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('differences'))
// Empty differences folder
var rmDir = function(dirPath, removeSelf) {
if (removeSelf === undefined)
removeSelf = true;
try { var files = fs.readdirSync(dirPath); }
catch(e) { return; }
if (files.length > 0)
for (var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath);
else
rmDir(filePath);
}
if (removeSelf)
fs.rmdirSync(dirPath);
};
// Get all file names from folder
var getFiles = function(dir, files_){
files_ = files_ || [];
var files = fs.readdirSync(dir);
for (var i in files){
var name = dir + '/' + files[i];
if (fs.statSync(name).isDirectory()){
getFiles(name, files_);
} else {
files_.push(files[i]);
}
}
return files_;
}
app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/index.html'));
});
app.post('/newIndex',function(req,res){
var pageres = new Pageres({delay: 1});
if(req.body.index){
req.body.index.forEach(function(index){
var name = index.name;
var url = index.url;
pageres.src(url, ['1920x1080'], {filename: name});
});
pageres.dest(__dirname+'/indexed/')
.run()
.then(function(){
console.log('Done');
res.send('Success');
});
}else{
res.send('Error');
}
});
app.post('/freshcopy',function(req,res){
rmDir(__dirname+'/freshcopy/', false);
var pageres = new Pageres({delay: 2});
if(req.body.index){
req.body.index.forEach(function(index){
var name = index.name;
var url = index.url;
pageres.src(url, ['1920x1080'], {filename: name});
});
pageres.dest(__dirname+'/freshcopy/')
.run()
.then(function(){
console.log('Done');
res.send('Success');
});
}else{
res.send('Error');
}
});
app.get('/compare', function(req,res){
// Compare the freshcopy vs old ones
var files = getFiles(__dirname+'/freshcopy/');
var promises = [];
rmDir(__dirname+'/differences/', false);
var getDiff = function(fileName, resolve){
var diff = resemble(__dirname+'/indexed/'+fileName).compareTo(__dirname+'/freshcopy/'+fileName).ignoreColors().onComplete(function(data){
var response = {
fileName : fileName,
missMatch : data.misMatchPercentage
};
if (data.misMatchPercentage >= 0.01) {
data.getDiffImage().pack().pipe(fs.createWriteStream(__dirname+'/differences/'+fileName));
response.diff = 'different';
}else{
response.diff = 'same';
}
resolve(response);
/*
{
misMatchPercentage : 100, // %
isSameDimensions: true, // or false
dimensionDifference: { width: 0, height: -1 }, // defined if dimensions are not the same
getImageDataUrl: function(){}
}
*/
});
};
files.forEach(function(fileName){
promises.push( new Promise(resolve => getDiff(fileName, resolve)) );
});
Promise.all(promises)
.then(data => {
console.log(data);
res.send(data);
});
});
app.listen(3000);