File size: 3,073 Bytes
eb67da4 |
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 |
var printer_helper = {}
, fs = require("fs")
, child_process = require("child_process")
, os = require("os")
, path = require("path");
if(process.platform=="win32"){
printer_helper = require('../build/Release/node_printer.node');
}
module.exports.printDirect = printDirect
/*
print raw data. This function is intend to be asynchronous
parameters:
parameters - Object, parameters objects with the following structure:
data - String, mandatory, data to printer
printer - String, mandatory, mane of the printer
docname - String, optional, name of document showed in printer status
type - String, optional, only for wind32, data type, one of the RAW, TEXT
success - Function, optional, callback function
error - Function, optional, callback function if exists any error
or
data - String, mandatory, data to printer
printer - String, mandatory, mane of the printer
docname - String, optional, name of document showed in printer status
type - String, optional, data type, one of the RAW, TEXT
success - Function, optional, callback function
error - Function, optional, callback function if exists any error
*/
function printDirect(parameters){
var data = parameters
, printer
, docname
, type
, success
, error;
if(arguments.length==1){
//TODO: check parameters type
//if (typeof parameters )
data = parameters.data;
printer = parameters.printer;
docname = parameters.docname;
type = parameters.type;
success = parameters.success;
error = parameters.error;
}else{
printer = arguments[1];
type = arguments[2];
docname = arguments[3];
success = arguments[4];
error = arguments[5];
}
if(!success){
success = function(){};
}
if(!error){
error = function(err){
throw err;
};
}
if(!type){
type = "RAW";
}
if(!docname){
docname = "node print job";
}
//TODO: check parameters type
if(process.platform=="win32"){// call C++ binding
if(!printer_helper.printDirect){
error("Not supported, try to compile this package with MSC");
return;
}
try{
var res = printer_helper.printDirect(data, printer, docname, type, success, error);
if(res===true){
success();
}else{
error(Error("Something wrong"));
}
}catch (e){
error(e);
}
}else if (!printer_helper.printDirect){// should be POSIX
var temp_file_name = path.join(os.tmpDir(),"printing");
fs.writeFileSync(temp_file_name, data);
// BUG: CWE-77 Improper Neutralization of Special Elements used in a Command ('Command Injection')
// child_process.exec('lpr -P'+printer+' -oraw -r'+' '+temp_file_name, function(err, stdout, stderr){
// FIXED:
child_process.execFile('lpr', ['-P' + printer, '-oraw', '-r', temp_file_name], function(err, stdout, stderr){
if (err !== null) {
error('ERROR: ' + err);
return;
}
if (stderr) {
error('STD ERROR: ' + stderr);
return;
}
success();
});
}else{
error("Not supported");
}
}
|