-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·171 lines (161 loc) · 4.19 KB
/
server.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
const grpc = require('@grpc/grpc-js')
const fs = require('fs')
const protoLoader = require('@grpc/proto-loader');
const puppeteer = require('puppeteer');
(async () => {
global.browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--device-scale-factor=2'
]
});
let PROTO_PATH = './proto_src/urltoimage/urltoimage.proto'
let conf = require('./conf')
let packageDefinition = protoLoader.loadSync(
PROTO_PATH,{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
);
let impl_proto = grpc.loadPackageDefinition(packageDefinition)
console.log(impl_proto);
async function Convert(call, callback) {
try{
let start = (new Date()).getTime();
//width & height
let width = 1200;
let height = 800;
let isMobile = false;
if (call.request.size) {
[width, height] = call.request.size.split('x').map(v => parseInt(v, 10));
}
let captureBeyondViewport = call.request.captureBeyondViewport;
//type
let type="jpeg";
if (call.request.type) {
if( call.request.type == "png" || call.request.type =="jpeg" || call.request.type=="jpg" || call.request.type=="pdf"){
type = call.request.type
}
}
//quality
let quality=80;
if (call.request.quality) {
quality = call.request.quality
}
if(quality<0 || quality>100){
quality = 80;
}
let now = new Date();
let dateStr = now.toISOString();
let filename = "/tmp/"+now.getTime()+"."+Math.random()+"."+type;
let page = await global.browser.newPage();
let params={
path: `${filename}`,
type: `${type}`,
fullPage: true,
captureBeyondViewport: captureBeyondViewport,
};
page.setViewport({
width,
height,
isMobile,
deviceScaleFactor: 2
})
if(type=="pdf"){
//https://pptr.dev/api/puppeteer.page.pdf
//https://pptr.dev/api/puppeteer.pdfoptions
let pdfOptions = {
};
for(var k in call.request.pdfoptions){
if(call.request.pdfoptions[k]){
pdfOptions[k] = call.request.pdfoptions[k];
}
}
pdfOptions.path = filename;
console.log(
"onRequest",
JSON.stringify({
type,
date: dateStr,
request:call.request,
pdfOptions
})
);
await page.goto(call.request.url, {waitUntil: 'networkidle2'});
await page.pdf(pdfOptions);
}else{
//质量
if(type!="png"){
params.quality= quality;
}
console.log(
"onRequest",
JSON.stringify({
type,
date: dateStr,
request:call.request,
})
);
await page.goto(call.request.url, {waitUntil: 'networkidle2'});
//透明
//https://stackoverflow.com/questions/55105958/puppeteer-saved-png-is-not-transparent
if((conf.screenshot && conf.screenshot.transparent) || process.env.TRANSPARENT || call.request.transparent){
await page.evaluate(() => document.body.style.background = 'transparent');
params.omitBackground=true;
}
//https://pptr.dev/api/puppeteer.page.screenshot
await page.screenshot(params);
}
await page.close();
let file = {
name :filename,
content :fs.readFileSync(filename),
};
fs.unlinkSync(filename)
let end = (new Date()).getTime();
callback(null,{ code: 0, message: "ok", files:[file]});
console.log(
"onResponse",
JSON.stringify({
date: dateStr,
duration:(end-start)/1000+"s",
request:call.request,
type:type,
quality:quality,
filename,
width,
height,
params
})
);
}catch(e){
callback(e,{ code: -1, message: "fail"} );
console.log(
JSON.stringify({
request:call.request,
exception:e
})
);
}
};
//prevent node.js process crash
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at:', reason.stack || reason)
});
let server = new grpc.Server({'grpc.max_send_message_length': 1024*1024*1024})
server.addService(impl_proto.liburltoimage.Urltoimage.service, {
Convert:Convert
})
server.bindAsync(conf.ip.server + ':' + conf.port, grpc.ServerCredentials.createInsecure(), (err , port) => {
if (err != null) {
return console.error(err);
}
console.log(`gRPC listening on ${ port }`);
server.start()
});
})();