-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (68 loc) · 2.94 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
87
88
89
90
91
92
var express = require('express');
var server = express();
var filesystem = require('fs');
var download = require('download');
var randomstring = require("randomstring");
var config = require('./config.js');
server.get('/', function (req, res) {
// First, we get all needed data from the query string.
var printername, urltoprint, secret, selectedprinter, copies;
try {
// We only want authorized people to print, so we check the IP address.
if (config.ips.indexOf(req.ip) < 0) {
console.log('\nUnauthorized IP: ' + req.ip);
res.send('UNAUTHORIZED');
return;
}
// We encode everything with base64 to prevent strange characters from corrupting the URL.
data = JSON.parse(new Buffer(req.query.data, 'base64').toString('ascii'));
secret = data.secret;
printername = data.printer;
urltoprint = data.url;
copies = data.copies;
// Check if a valid printer was selected.
if (printername == 'card') {
selectedprinter = config.printers.card;
} else if (printername == 'document') {
selectedprinter = config.printers.document;
} else {
console.log('\nInvalid printer supplied: ' + printername);
res.send('INVALID');
return;
}
// We only want authorized people to print, so we check the secret.
if (secret != config.secret) {
console.log('\nInvalid secret supplied: ' + secret);
res.send('UNAUTHORIZED');
return;
}
console.log('\nPrintinging PDF to ' + selectedprinter + ' (' + printername + ') from the following location:\n' + urltoprint);
// Save the file with a random filename.
var filename = process.cwd() + '\\downloads\\' + randomstring.generate(32) + '.file';
setTimeout(unLinkFile.bind(null, filename), 60000);
var stream = download(urltoprint).pipe(filesystem.createWriteStream(filename));
stream.on('finish', sendToPrinter.bind(null, filename, selectedprinter, copies, res, config.sumatra));
} catch (e) {
// Something went wrong. This could be due to not all parameters being supplied!
console.log('Generic error: ' + e.message);
res.send('ERROR_UNKNOWN');
return;
}
});
server.listen(config.port, config.host, function () {
console.log('\nThe print server is now listening on port ' + config.port + '.');
});
function unLinkFile(filename) {
try {
filesystem.unlinkSync(filename);
console.log("\nUnlinked " + filename + ".");
} catch (e) {
console.log("\nSomething went wrong unlinking " + filename + ".");
}
}
function sendToPrinter(filename, printer, copies, res, sumatra) {
console.log('Downloaded ' + filename + '.');
spawn = require('child_process').spawn;
spawn(sumatra, [filename, '-print-to', printer, '-print-settings', copies + 'x']);
res.send('OK');
}