Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import credentials from "./credentials.json" with { type: "json" };
- import fs from 'node:fs/promises';
- import path from 'node:path';
- import { PdfApi } from "asposepdfcloud";
- const attachments = {
- getAllAttachments: async function () {
- const LOCAL_PATH = "C:\\Samples\\";
- const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
- const STORAGE_FILE_NAME = "sample_attachment.pdf";
- try {
- const pdfApi = new PdfApi(credentials.id, credentials.key);
- const pdfData = await fs.readFile(LOCAL_FILE_NAME);
- await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);
- const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);
- if (result.body.code === 200 && result.body.attachments) {
- if (!Array.isArray(result.body.attachments.links) || result.body.attachments.links.length === 0) {
- console.error("Unexpected error: No attachments to download.");
- return;
- }
- const downloadPromises = result.body.attachments.links.map(
- async (attachment, index) => {
- try {
- const response = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, index);
- //TODO: Check this
- const filePath = path.join(LOCAL_PATH, response.body.attachment.name);
- await fs.writeFile(filePath, response.body);
- } catch (error) {
- console.error(`Failed to download attachment ${index}:`, error);
- }
- });
- await Promise.all(downloadPromises);
- } else {
- console.error("Failed to retrieve attachments. Status:", result.statusCode);
- }
- } catch (error) {
- console.error("Error processing PDF attachments:", error);
- }
- },
- getSingleAttachment: async function () {
- const LOCAL_PATH = "C:\\Samples\\";
- const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
- const STORAGE_FILE_NAME = "sample_attachment.pdf";
- try {
- const pdfApi = new PdfApi(credentials.id, credentials.key);
- const pdfData = await fs.readFile(LOCAL_FILE_NAME);
- await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);
- const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);
- if (result.body.code === 200 && result.body.attachments) {
- if (!Array.isArray(result.body.attachments.list) || result.body.attachments.list.length === 0) {
- console.error("Unexpected error: No attachments to download.");
- return;
- }
- try {
- const response = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, 1);
- //TODO: Check this
- const filePath = path.join(LOCAL_PATH, response.body.attachment.name);
- await fs.writeFile(filePath, response.body);
- } catch (error) {
- console.error(`Failed to download attachment ${index}:`, error);
- }
- } else {
- console.error("Failed to retrieve attachments. Status:", result.statusCode);
- }
- } catch (error) {
- console.error("Error processing PDF attachments:", error);
- }
- },
- addAttachment: async function () {
- const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
- const STORAGE_FILE_NAME = "sample_attachment.pdf";
- const LOCAL_ATTACHMENT_FILE_NAME = "C:\\Samples\\Attachments\\file_example_MP3_700KB.mp3";
- const STORAGE_ATTACHMENT_FILE_NAME = "file_example_MP3_700KB.mp3";
- const RESULT_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
- try {
- const pdfApi = new PdfApi(credentials.id, credentials.key);
- let buffer = await fs.readFile(LOCAL_FILE_NAME);
- await pdfApi.uploadFile(LOCAL_ATTACHMENT_FILE_NAME, buffer);
- buffer = await fs.readFile(LOCAL_ATTACHMENT_FILE_NAME);
- await pdfApi.uploadFile(STORAGE_ATTACHMENT_FILE_NAME, buffer);
- const attachment = {
- name: STORAGE_ATTACHMENT_FILE_NAME,
- path: STORAGE_ATTACHMENT_FILE_NAME,
- description: "An example of MP3 file",
- mimeType: "audio/mpeg"
- }
- const appendResult = await pdfApi.postAddDocumentAttachment(STORAGE_FILE_NAME, attachment, null, null);
- if (appendResult.body.code == 200) {
- console.log("Status:", appendResult.body.status);
- const downloadResult = await pdfApi.downloadFile(STORAGE_FILE_NAME);
- await fs.writeFile(RESULT_FILE_NAME, downloadResult.body);
- }
- else
- console.log("Unexpected error : can't download attachments");
- } catch (error) {
- console.error(error.message);
- }
- }
- }
- export default attachments;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement