-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (49 loc) · 1.53 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
const express = require('express')
const app = express()
const Busboy = require('busboy');
const protectPdf = require('./server/protect_pdf')
const contentDisposition = require('content-disposition')
const ps = require('./server/ps')
app.use(express.static('public'));
app.post('/protect_pdf', function (req, res) {
var busboy = new Busboy({
headers: req.headers,
limits: {
fields: 0,
fileSize: 20 * Math.pow(10, 6),
files: 1,
parts: 100
}
});
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
const psInfo = setInterval(function () {
ps()
}, 500)
console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
/*
file.on('data', function (data) {
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
});
*/
res.writeHead(200, {
'Content-Disposition': contentDisposition(filename),
'Content-Type': 'application/pdf'
});
const stream = protectPdf(file)
stream.pipe(res)
stream.on('end', function () {
clearInterval(psInfo)
})
file.on('end', function () {
console.log('File [' + fieldname + '] Finished');
});
});
busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
console.log('Field [' + fieldname + ']: value: ' + val);
});
busboy.on('finish', function () {
console.log('Done parsing form!');
});
req.pipe(busboy);
})
app.listen(process.env.PORT || 8080)