This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (56 loc) · 2.27 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
const request = require("request");
module.exports = class {
constructor(address, log) {
this.address = address;
this.debugLog = log;
this.validFormats = ["image/jpeg", "application/pdf"];
this.webStatusForm = {
"ws_operation": 1,
"ws_scanid": 0,
"ws_type": 0,
"ws_format": 0,
"ws_size": 0,
"ws_scan_method": 0
}
}
scan(opt, cb) {
this.req("POST", "/wsStatus.htm", { form: this.webStatusForm }, (response, body) => {
if(response.statusCode !== 200 || response.headers["content-type"] !== "text/html;charset=utf-8" || body.match(/<FORM name=wsstatus method=post ACTION=\"\/wsStatus\.htm\">/) == null) {
cb(null, { code: "WS_STATUS" })
}
else {
this.req("GET", "/scan/image1.jpg?id=0&time=" + Date.now() + "&type=" + opt.type + "&fmt=" + opt.fmt + "&size=" + opt.size, { encoding: "binary" }, (response, body) => {
if(!this.validFormats.includes(response.headers["content-type"])) cb(null, { code: "GET_SCAN" })
else cb(body, false);
});
}
});
}
preview(opt, cb) {
this.req("POST", "/wsStatus.htm", { form: this.webStatusForm }, (response, body) => {
if(response.statusCode !== 200 || response.headers["content-type"] !== "text/html;charset=utf-8" || body.match(/<FORM name=wsstatus method=post ACTION=\"\/wsStatus\.htm\">/) == null) {
cb(null, { code: "WS_STATUS" })
}
else {
this.req("GET", "/scan/image.pdf?id=0&prev=1&time=" + Date.now() + "&type=" + opt.type, { encoding: "binary" }, (response, body) => {
if(response.headers["content-type"] !== "image/jpeg") cb(null, { code: "GET_PREVIEW" })
else cb(body, false);
});
}
});
}
req(method, url, data, cb) {
this.log(method + " - " + "https://" + this.address + url + " - " + JSON.stringify(data));
var options = { method: method, url: "https://" + this.address + url };
for (var attrname in data) { options[attrname] = data[attrname]; };
request(options, (error, response, body) => {
if (!error) cb(response, body);
else {
this.log(error)
}
})
}
log(msg) {
if(this.debugLog == 1) console.log("[HP-Webscan] > " + msg);
}
}