Fixed bug in file conversion on node, cleaned up warnings

This commit is contained in:
Saud Fatayerji 2023-11-15 15:33:04 +03:00
parent 57415bea85
commit 667583984f
3 changed files with 20 additions and 13 deletions

View File

@ -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, () => {

View File

@ -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);
}

View File

@ -19,17 +19,17 @@ export class PdfFile {
get uint8Array() : Promise<Uint8Array> {
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<PDFLibDocument> {
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<PDFJSDocument> {
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');
}