-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
137 lines (116 loc) · 3.67 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
'use strict';
const bodyParser = require('body-parser'),
express = require('express'),
fs = require('fs'),
puppeteer = require('puppeteer'),
temp = require('temp').track(true);
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
process.on('SIGINT', function() {
console.log('\nShutting down server.');
process.exit();
});
const bodyParserOptions = {
inflate: true,
limit: '20mb',
type: '*/*'
};
app.use(bodyParser.raw(bodyParserOptions))
app.get('/', (req, res) => {
let url;
url = req.query.url;
if (undefined !== url) {
return getPdf(url).then(pdfPath => sendFile(res, pdfPath));
}
handleNoData(res);
});
app.post('/', (req, res) => {
let url, body;
url = req.query.url;
body = req.body;
if (undefined !== url) {
return getPdf(url).then(pdfPath => sendFile(res, pdfPath));
}
try {
if (Buffer.byteLength(body) <= 0) {
body = null;
}
} catch (e) {
body = null;
}
if (null !== body) {
return getPdf(body, true).then(pdfPath => sendFile(res, pdfPath));
}
handleNoData(res);
})
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
function handleNoData(res) {
res.status(400);
res.send('You should send some data. Either pass an URL in the ?url query param, or send a POST request with an HTML body in your request (Content-Type header required).');
}
function sendFile(res, pdfPath) {
fs.readFile(pdfPath, null, (err, data) => {
if (!err) {
res.set('Content-Type', 'application/pdf');
res.send(data);
fs.unlink(pdfPath, () => {
});
return;
}
console.log(err);
});
}
async function getPdf(urlOrBody, fromBody = false) {
let id, tempBodyFile = null;
if (fromBody) {
await temp.open('ptr-', (err, info) => {
if (!err) {
fs.write(info.fd, urlOrBody, (err) => {
console.log(err);
});
tempBodyFile = info.path + '.html';
fs.renameSync(info.path, tempBodyFile);
urlOrBody = 'file://' + tempBodyFile;
} else {
urlOrBody = null;
}
});
console.log('Got a body');
id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
} else {
console.log('Got an URL : ' + urlOrBody);
id = urlOrBody.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gim);
}
const pdfPath = '/tmp/ptr-' + id + '.pdf';
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
const page = await browser.newPage();
await page.goto(urlOrBody, {waitUntil: 'networkidle2'});
await page.pdf({viewport: {width: 800, height: 600}, format: 'a4', printBackground: true, path: pdfPath});
await browser.close();
if (null !== tempBodyFile) {
fs.unlinkSync(tempBodyFile);
}
/* Test de suppression automatique des temps: useless si on reboot le container de manière périodique
(() => {
fs.readdir('/tmp', function(err, filenames) {
if (err) {
onError(err);
return;
}
filenames.forEach(function(filename) {
fs.readFile(dirname + filename, 'utf-8', function(err, content) {
if (err) {
onError(err);
return;
}
onFileContent(filename, content);
});
});
});
})();*/
return pdfPath;
}