-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.js
More file actions
64 lines (51 loc) · 1.81 KB
/
encrypt.js
File metadata and controls
64 lines (51 loc) · 1.81 KB
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
'use strict';
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const { PDFNet } = require('@pdftron/pdfnet-node');
const { basename, extname } = require('path');
module.exports.handle = async ({Records: records}) => {
let response = null;
const main = async () => {
try {
await Promise.all(records.map(async record => {
const key = record.s3.object.key;
const file = await s3.getObject({
Bucket: process.env.bucket,
Key: key,
}).promise();
const arr = new Uint8Array(file.Body);
const doc = await PDFNet.PDFDoc.createFromBuffer(arr);
const newHandler = await PDFNet.SecurityHandler.createDefault();
newHandler.changeUserPasswordUString(process.env.hash);
newHandler.setPermission(PDFNet.SecurityHandler.Permission.e_print, false);
newHandler.setPermission(PDFNet.SecurityHandler.Permission.e_extract_content, false);
doc.setSecurityHandler(newHandler);
const encrypted = await doc.saveMemoryBuffer(arr.length);
var base64data = new Buffer.from(encrypted, 'binary');
await s3.putObject({
Body: base64data,
Bucket: process.env.bucket,
ContentType: 'application/pdf',
Key: `encrypted/${basename(key, extname(key))}.pdf`
}).promise()
}));
} catch (err) {
console.log(err)
response = {
statusCode: 500,
body: JSON.stringify(err)
}
}
response = {
statusCode: 301,
body: JSON.stringify("File encrypted")
}
}
await PDFNet.runWithCleanup(main, process.env.pdftronkey).catch(function (error) {
response = {
statusCode: 500,
body: JSON.stringify(error),
}
}).then(function () { PDFNet.shutdown(); });
return response;
};