-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaryshot.js
executable file
·118 lines (102 loc) · 2.95 KB
/
scaryshot.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
#!/usr/bin/env node
var cp = require('child_process')
var os = require('os')
var fs = require('fs')
var http = require('http')
var url = require('url')
var qs = require('querystring')
var port = process.argv[2] || 3000
var join = require('path').join
var tmpdir = os.tmpdir() + '/'
var tmpid = Date.now()
var indexHtml = fs.readFileSync(join(__dirname, '/index.html')).toString()
var mimes = {
pdf: 'application/pdf',
png: 'image/png',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
gif: 'image/gif',
}
function error(res, code, message) {
res.writeHead(code, {'Content-Type': 'application/json'})
res.end(JSON.stringify({
status: code,
message: message,
}))
}
function make(opts, res) {
if (!opts || (!opts.url && !opts.html)) {
return error(res, 400, 'The "url" or "html" parameter is required')
}
var type = opts.type || 'pdf'
if (!mimes[type]) {
return error(res, 400, 'Type "' + type + '" is not supported. The following types are supported: ' + Object.keys(mimes))
}
var name = opts.name || 'output'
var path = tmpdir + 'phantom-' + (tmpid++) + '.' + type
if (opts.url && !/^http/.test(opts.url)) {
opts.url = 'http://' + opts.url
}
var env = JSON.stringify({
PHANTOM_URL: opts.url || '',
PHANTOM_TYPE: type,
PHANTOM_TMP_NAME: path,
PHANTOM_HTML: opts.html || '',
PHANTOM_DELAY: opts.delay || 0,
PHANTOM_HEADER: opts.header || '',
PHANTOM_FOOTER: opts.footer || '',
PHANTOM_ORIENTATION: opts.orientation || '',
})
var child = cp.spawn('phantomjs',
[join(__dirname, '/render.js')],
{stdio: ['pipe', process.stdout, process.stderr]}
)
child.stdin.write(env)
child.stdin.end()
child.on('error', function() {
child.kill()
error(res, 500, 'Unable to render webpage')
})
child.on('exit', function() {
if (res.headersSent) return // sent an error
res.writeHead(200, {
'Content-Type': mimes[type],
'Content-Disposition': 'attachment; filename=' + name + '.' + type,
})
fs.createReadStream(path)
.on('error', function() {
res.end()
})
.on('end', function() {
fs.unlink(path, function(er) { // clean up tmp file
if (er) return console.error(er)
else console.log('unlinked', path)
})
})
.pipe(res)
})
}
var server = module.exports = http.createServer(function(req, res) {
var parsed = url.parse(req.url, true)
if (req.method == 'GET') return route(parsed.query)
var data = ''
req.setEncoding('utf8')
req.on('data', function(chunk) { data += chunk })
req.on('end', function() { route(qs.parse(data)) })
req.on('error', console.error)
function route(query) {
switch (parsed.pathname) {
case '/generate':
case '/generate/':
make(query, res)
break
default:
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(indexHtml)
}
}
})
if (!module.parent) {
server.listen(port)
console.log('Scaryshot listening on port', port)
}