From 667583984fae0f2aa9ca9690e3df867e99497850 Mon Sep 17 00:00:00 2001 From: Saud Fatayerji Date: Wed, 15 Nov 2023 15:33:04 +0300 Subject: [PATCH] Fixed bug in file conversion on node, cleaned up warnings --- server-node/src/index.ts | 2 +- server-node/src/utils/libre-office-utils.ts | 15 +++++++++++---- shared-operations/src/wrappers/PdfFile.ts | 16 ++++++++-------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/server-node/src/index.ts b/server-node/src/index.ts index d16d7520..a154074d 100644 --- a/server-node/src/index.ts +++ b/server-node/src/index.ts @@ -4,7 +4,7 @@ const PORT = 8000; // server-node: backend api import api from './routes/api/api-controller'; -app.use("/api/", api); +app.use("/api", api); // serve app.listen(PORT, () => { diff --git a/server-node/src/utils/libre-office-utils.ts b/server-node/src/utils/libre-office-utils.ts index 736c56f6..3608d1b6 100644 --- a/server-node/src/utils/libre-office-utils.ts +++ b/server-node/src/utils/libre-office-utils.ts @@ -15,12 +15,19 @@ export async function fileToPdf(byteArray: Uint8Array, filename: string): Promis await writeBytesToFile(srcFile, byteArray); const messages = await runLibreOfficeCommand(randFolderName, ["--headless","--convert-to","pdf",srcFile,"--outdir",tempDir]); - const lastMessage = messages[messages.length-1] - const outputFilePath = lastMessage.split(" -> ")[1].split(".pdf")[0]+".pdf"; - const outputFileName = path.parse(outputFilePath).base; + + const files = fs.readdirSync(tempDir).filter(file => file.endsWith(".pdf")); + if (files.length > 1) { + console.warn("Ambiguous file to pdf outputs: Returning first result", files); + } else if (files.length == 0) { + throw new Error("File to pdf failed: no output files found. Messages: "+messages); + } + + const outputFileName = files[0]; + const outputFilePath = path.join(tempDir, outputFileName); const outputBytes = await readBytesFromFile(outputFilePath); - fs.rmdirSync(tempDir); + fs.rmdirSync(tempDir, {recursive: true}); return new PdfFile(outputFileName, outputBytes, RepresentationType.Uint8Array); } diff --git a/shared-operations/src/wrappers/PdfFile.ts b/shared-operations/src/wrappers/PdfFile.ts index edc84404..d85910ee 100644 --- a/shared-operations/src/wrappers/PdfFile.ts +++ b/shared-operations/src/wrappers/PdfFile.ts @@ -19,17 +19,17 @@ export class PdfFile { get uint8Array() : Promise { switch (this.representationType) { case RepresentationType.Uint8Array: - return new Promise((resolve, reject) => { + return new Promise((resolve) => { resolve(this.representation as Uint8Array); }); case RepresentationType.PDFLibDocument: - return new Promise(async (resolve, reject) => { + return new Promise(async (resolve) => { var uint8Array = await (this.representation as PDFLibDocument).save(); this.uint8Array = uint8Array; resolve(uint8Array); }); case RepresentationType.PDFJSDocument: - return new Promise(async (resolve, reject) => { + return new Promise(async (resolve) => { var uint8Array = await (this.representation as PDFJSDocument).getData(); this.uint8Array = uint8Array; resolve(uint8Array); @@ -47,11 +47,11 @@ export class PdfFile { get pdfLibDocument() : Promise { switch (this.representationType) { case RepresentationType.PDFLibDocument: - return new Promise((resolve, reject) => { + return new Promise((resolve) => { resolve(this.representation as PDFLibDocument); }); default: - return new Promise(async (resolve, reject) => { + return new Promise(async (resolve) => { var uint8Array = await this.uint8Array; var pdfLibDoc = await PDFLibDocument.load(uint8Array, { updateMetadata: false, @@ -69,11 +69,11 @@ export class PdfFile { get pdfJsDocument() : Promise { switch (this.representationType) { case RepresentationType.PDFJSDocument: - return new Promise((resolve, reject) => { + return new Promise((resolve) => { resolve(this.representation as PDFJSDocument); }); default: - return new Promise(async (resolve, reject) => { + return new Promise(async (resolve) => { const pdfjsDoc = await PDFJS.getDocument(await this.uint8Array).promise; this.pdfJsDocument = pdfjsDoc; resolve(pdfjsDoc); @@ -126,7 +126,7 @@ export class PdfFile { } } -export const PdfFileSchema = Joi.any().custom((value, helpers) => { +export const PdfFileSchema = Joi.any().custom((value) => { if (!(value instanceof PdfFile)) { throw new Error('value is not a PdfFile'); }