jobs. folder-job, file-change-trigger and some cleanup
This commit is contained in:
@@ -3,7 +3,7 @@ import { Response } from "express";
|
||||
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
|
||||
import Archiver from "archiver";
|
||||
|
||||
export async function respondWithFile(res: Response, uint8Array: Uint8Array, filename: string, mimeType: string): Promise<void> {
|
||||
async function respondWithFile(res: Response, uint8Array: Uint8Array, filename: string, mimeType: string): Promise<void> {
|
||||
res.writeHead(200, {
|
||||
"Content-Type": mimeType,
|
||||
"Content-disposition": `attachment; filename="${filename}"`,
|
||||
@@ -12,12 +12,12 @@ export async function respondWithFile(res: Response, uint8Array: Uint8Array, fil
|
||||
res.end(uint8Array);
|
||||
}
|
||||
|
||||
export async function respondWithPdfFile(res: Response, file: PdfFile): Promise<void> {
|
||||
async function respondWithPdfFile(res: Response, file: PdfFile): Promise<void> {
|
||||
const byteArray = await file.uint8Array;
|
||||
respondWithFile(res, byteArray, file.filename+".pdf", "application/pdf");
|
||||
}
|
||||
|
||||
export async function respondWithZip(res: Response, filename: string, files: {uint8Array: Uint8Array, filename: string}[]): Promise<void> {
|
||||
async function respondWithZip(res: Response, filename: string, files: {uint8Array: Uint8Array, filename: string}[]): Promise<void> {
|
||||
if (files.length == 0) {
|
||||
res.status(500).json({"warning": "The workflow had no outputs."});
|
||||
return;
|
||||
@@ -57,29 +57,4 @@ export async function respondWithPdfFiles(res: Response, pdfFiles: PdfFile[] | u
|
||||
const files = await Promise.all(promises);
|
||||
respondWithZip(res, filename, files);
|
||||
}
|
||||
}
|
||||
|
||||
export function response_mustHaveExactlyOneFile(res: Response): void {
|
||||
res.status(400).send([
|
||||
{
|
||||
"message": "file is required",
|
||||
"path": [
|
||||
"pdfFile"
|
||||
],
|
||||
"type": "file",
|
||||
"context": {
|
||||
"label": "pdfFile",
|
||||
"key": "pdfFile"
|
||||
}
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
export function response_dependencyNotConfigured(res: Response, dependencyName: string): void {
|
||||
res.status(400).send([
|
||||
{
|
||||
"message": `${dependencyName} is not configured correctly on the server.`,
|
||||
"type": "dependency_error",
|
||||
}
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import { exec, spawn } from "child_process";
|
||||
import { PdfFile, RepresentationType } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
|
||||
|
||||
export async function fileToPdf(byteArray: Uint8Array, filename: string): Promise<PdfFile> {
|
||||
const parentDir = path.join(os.tmpdir(), "StirlingPDF");
|
||||
fs.mkdirSync(parentDir, {recursive: true});
|
||||
const tempDir = fs.mkdtempSync(parentDir+"/");
|
||||
const srcFile = path.join(tempDir, filename);
|
||||
const randFolderName = path.parse(tempDir).base;
|
||||
|
||||
await writeBytesToFile(srcFile, byteArray);
|
||||
|
||||
const messages = await runLibreOfficeCommand(randFolderName, ["--headless","--convert-to","pdf",srcFile,"--outdir",tempDir]);
|
||||
|
||||
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, {recursive: true});
|
||||
|
||||
return new PdfFile(outputFileName, outputBytes, RepresentationType.Uint8Array);
|
||||
}
|
||||
|
||||
export function isLibreOfficeInstalled() {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec("libreoffice --version", (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
if (stderr) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
const result = stdout.match("LibreOffice ([0-9]+\.){4}.*");
|
||||
resolve(result ? true : false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function writeBytesToFile(filePath: string, bytes: Uint8Array): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.writeFile(filePath, bytes, function(err) {
|
||||
if(err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function readBytesFromFile(filePath: string): Promise<Uint8Array> {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(filePath, (err, data) => {
|
||||
if (err) {
|
||||
reject(new Error(`Error reading file: ${err.message}`));
|
||||
} else {
|
||||
const uint8Array = new Uint8Array(data);
|
||||
resolve(uint8Array);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function runLibreOfficeCommand(idKey: string, args: string[]): Promise<string[]> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const messageList: string[] = [];
|
||||
|
||||
const process = spawn("libreoffice", args);
|
||||
|
||||
process.stdout.on("data", (data) => {
|
||||
const dataStr = data.toString();
|
||||
console.log(`Progress ${idKey}:`, dataStr);
|
||||
messageList.push(dataStr);
|
||||
});
|
||||
|
||||
process.stderr.on("data", (data) => {
|
||||
console.error(`stderr ${idKey}:`, data.toString());
|
||||
});
|
||||
|
||||
process.on("exit", (code) => {
|
||||
if (code === 0) {
|
||||
resolve(messageList);
|
||||
} else {
|
||||
reject(new Error(`Command failed with exit code ${code}`));
|
||||
}
|
||||
});
|
||||
|
||||
process.on("error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user