-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
86 lines (79 loc) · 2.35 KB
/
index.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
var ScreenshotManager = require('./screenshotManager'),
os = require('os'),
express = require('express'),
bodyParser = require('body-parser'),
exphbs = require('express-handlebars'),
log4j = require('./log');
var app = express();
//查看http://wiki.jikexueyuan.com/project/express-mongodb-setup-blog/handlebars.html
app.engine('hbs', exphbs({
extname: '.hbs'
}));
// app.engine('hbs', exphbs({defaultLayout: 'main', extname: '.hbs'}));
app.set('view engine', 'hbs');
app.use(require('morgan')('combined'));
app.use(log4j.logMiddle());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use('/tmp', express.static(__dirname + '/tmp'));
app.get('/', function(req, res) {
if (req.query.type === 'base64') {
res.render('base64.hbs');
} else {
res.render('home');
}
});
var getSetting = function(query) {
var result = {
viewportSize: {},
shotConfig: {}
};
if (query.width) {
result.viewportSize.width = query.width;
}
if (query.height) {
result.viewportSize.height = query.height;
}
if (query.format) {
result.shotConfig.format = query.format;
}
if (query.quality) {
result.shotConfig.quality = query.quality;
}
if (query.zoomFactor) {
result.zoomFactor = query.zoomFactor;
}
return result;
};
app.get('/capture', function(req, res) {
var query = req.query,
setting = getSetting(query);
screenshotManager.getScreenshot().capture(null, query.url, setting).then(function(filepath) {
if(os.platform() === 'win32') {
res.redirect('/tmp/' + filepath.split('\\').pop());
} else {
res.redirect('/tmp/' + filepath.split('/').pop());
}
}).catch(function(e) {
res.send(e);
});
});
app.get('/capture/base64', function(req, res) {
var query = req.query,
setting = getSetting(query);
screenshotManager.getScreenshot().capture('base64', query.url, setting).then(function(data) {
res.render('capture', {
format: req.query.format,
content: data
});
}).catch(function(e) {
res.send(e);
});
});
var screenshotManager = new ScreenshotManager(3).on('inited', function() {
app.listen(3000).on('connect', function() {
log4j.getLogger().info('启动');
});
});