Skip to content

Commit b269484

Browse files
PDFCLOUD-4783: added use cases for Encrypt/Decrypt
1 parent 6bd6b60 commit b269484

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../src/api/api.js";
5+
6+
const configParams = {
7+
LOCAL_FOLDER: "C:\\Samples\\",
8+
PDF_DOCUMENT_NAME: "sample_encrypted.pdf",
9+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
10+
DOCUMENT_PASSWORD: "Owner-Password"
11+
};
12+
13+
const pdfApi = new PdfApi(credentials.id, credentials.key);
14+
15+
const pdfEncoder = {
16+
async uploadDocument () {
17+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
18+
const pdfFileData = await fs.readFile(fileNamePath);
19+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData)
20+
.then(() => console.log("File: '" + configParams.PDF_DOCUMENT_NAME +"' successfully uploaded."));
21+
},
22+
23+
async downloadResult() {
24+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
25+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
26+
await fs.writeFile(filePath, changedPdfData.body);
27+
console.log("Downloaded: " + filePath);
28+
},
29+
30+
async decrypt_document() {
31+
const password_encoded = btoa(configParams.DOCUMENT_PASSWORD)
32+
33+
const response = await pdfApi.postDecryptDocumentInStorage(configParams.PDF_DOCUMENT_NAME, password_encoded);
34+
35+
if (response.body.code == 200)
36+
console.log("decrypt_document(): Document #'" + configParams.PDF_DOCUMENT_NAME + "' successfully decrypted.")
37+
else
38+
throw new Error("decrypt_document(): Failed to decrypt document #'" + configParams.PDF_DOCUMENT_NAME + "'. Response code: {" + response.code + "}")
39+
},
40+
41+
}
42+
43+
async function main() {
44+
try {
45+
await pdfEncoder.uploadDocument();
46+
await pdfEncoder.decrypt_document();
47+
await pdfEncoder.downloadResult();
48+
} catch (error) {
49+
console.error("Error:", error.message);
50+
}
51+
}
52+
53+
main();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../src/api/api.js";
5+
import { CryptoAlgorithm } from "../../src/models/cryptoAlgorithm.js";
6+
7+
const configParams = {
8+
LOCAL_FOLDER: "C:\\Samples\\",
9+
PDF_DOCUMENT_NAME: "sample.pdf",
10+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
11+
ENCRYPT_ALGORITHM: CryptoAlgorithm.AESx256,
12+
USER_PASSWORD: "User-Password",
13+
OWNER_PASSWORD: "Owner-Password",
14+
};
15+
16+
const pdfApi = new PdfApi(credentials.id, credentials.key);
17+
18+
const pdfEncoder = {
19+
async uploadDocument () {
20+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
21+
const pdfFileData = await fs.readFile(fileNamePath);
22+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData)
23+
.then(() => console.log("File: '" + configParams.PDF_DOCUMENT_NAME +"' successfully uploaded."));
24+
},
25+
26+
async downloadResult() {
27+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
28+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
29+
await fs.writeFile(filePath, changedPdfData.body);
30+
console.log("Downloaded: " + filePath);
31+
},
32+
33+
async encrypt_document() {
34+
const user_password_encoded = btoa(configParams.USER_PASSWORD)
35+
36+
const owner_password_encoded = btoa(configParams.OWNER_PASSWORD)
37+
38+
const response = await pdfApi.postEncryptDocumentInStorage(configParams.PDF_DOCUMENT_NAME, user_password_encoded, owner_password_encoded, configParams.ENCRYPT_ALGORITHM);
39+
40+
if (response.body.code == 200)
41+
console.log("encrypt_document(): Document #'" + configParams.PDF_DOCUMENT_NAME + "' successfully encrypted.")
42+
else
43+
throw new Error("encrypt_document(): Failed to encrypt document #'" + configParams.PDF_DOCUMENT_NAME + "'. Response code: {" + response.code + "}")
44+
},
45+
46+
}
47+
48+
async function main() {
49+
try {
50+
await pdfEncoder.uploadDocument();
51+
await pdfEncoder.encrypt_document();
52+
await pdfEncoder.downloadResult();
53+
} catch (error) {
54+
console.error("Error:", error.message);
55+
}
56+
}
57+
58+
main();

0 commit comments

Comments
 (0)