-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencryption-service.ts
53 lines (40 loc) · 2.04 KB
/
encryption-service.ts
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
import { FileSystemService, LambdaFileSystemService } from './file-system-service';
export interface EncryptionService {
encryptPdf(data: Buffer, userPassword: string, ownerPassword: string): Promise<Buffer>;
}
import childProcess = require('child_process');
export class QpdfEncryptionService implements EncryptionService {
private fileSystemService: FileSystemService;
constructor(fileSystemService: FileSystemService = new LambdaFileSystemService()) {
this.fileSystemService = fileSystemService;
}
public async encryptPdf(data: Buffer, userPassword: string, ownerPassword: string): Promise<Buffer> {
process.env.PATH = `${process.env.PATH}:${process.env.LAMBDA_TASK_ROOT}/bin`;
process.env.LD_LIBRARY_PATH = `${process.env.LAMBDA_TASK_ROOT}/sharedlib`;
await this.fileSystemService.save(data, `/tmp/report.pdf`);
const encryptedFilePath = await this.encrypt(`/tmp/report.pdf`, `/tmp/encrypted-report.pdf`, userPassword, ownerPassword);
const encryptedBuffer = await this.fileSystemService.read(encryptedFilePath);
return encryptedBuffer;
}
private encrypt(pathOfFileToEncrypt: string, pathOfEncryptedFile: string, userPassword: string, ownerPassword: string): Promise<string> {
return new Promise((resolve, reject) => {
const qpdf = childProcess.spawn('qpdf',
['--encrypt', userPassword, ownerPassword, '256', '--modify=none', '--', pathOfFileToEncrypt, pathOfEncryptedFile]);
qpdf.stdout.on('data', data => {
console.log(`stdout: ${data}`);
});
qpdf.stderr.on('data', data => {
console.log(`stderr: ${data}`);
reject(data);
});
qpdf.on('close', code => {
console.log(`child process exited with code ${code}`);
if (code === 0) {
resolve(pathOfEncryptedFile);
} else {
reject(`Exit code ${code}`);
}
});
});
}
}