mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-22 23:45:02 +00:00
Error handeling
(async requests, prevent server from crashing on user-error)
This commit is contained in:
parent
90f0ee0bc5
commit
498f287d57
@ -7,7 +7,17 @@ import { traverseOperations } from "@stirling-pdf/shared-operations/src/workflow
|
|||||||
import { PdfFile, RepresentationType } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile';
|
import { PdfFile, RepresentationType } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile';
|
||||||
import { respondWithPdfFiles } from '../../utils/endpoint-utils';
|
import { respondWithPdfFiles } from '../../utils/endpoint-utils';
|
||||||
|
|
||||||
const activeWorkflows: any = {};
|
interface Workflow {
|
||||||
|
eventStream?: express.Response<any, Record<string, any>>,
|
||||||
|
result?: PdfFile[],
|
||||||
|
finished: boolean,
|
||||||
|
createdAt: EpochTimeStamp,
|
||||||
|
finishedAt?: EpochTimeStamp,
|
||||||
|
error?: { type: number, error: string, stack?: string }
|
||||||
|
// TODO: When auth is implemented: owner
|
||||||
|
}
|
||||||
|
|
||||||
|
const activeWorkflows: Record<string, Workflow> = {};
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@ -47,10 +57,17 @@ router.post("/:workflowUuid?", [
|
|||||||
console.log("Download");
|
console.log("Download");
|
||||||
await respondWithPdfFiles(res, pdfResults, "workflow-results");
|
await respondWithPdfFiles(res, pdfResults, "workflow-results");
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
if(err.validationError)
|
if(err.validationError) {
|
||||||
|
// Bad Request
|
||||||
res.status(400).json({error: err});
|
res.status(400).json({error: err});
|
||||||
else
|
}
|
||||||
|
else if (err instanceof Error) {
|
||||||
|
console.error("Internal Server Error", err);
|
||||||
|
// Internal Server Error
|
||||||
|
res.status(500).json({error: err.message, stack: err.stack});
|
||||||
|
} else {
|
||||||
throw err;
|
throw err;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -62,10 +79,7 @@ router.post("/:workflowUuid?", [
|
|||||||
|
|
||||||
activeWorkflows[workflowID] = {
|
activeWorkflows[workflowID] = {
|
||||||
createdAt: Date.now(),
|
createdAt: Date.now(),
|
||||||
finished: false,
|
finished: false
|
||||||
eventStream: null,
|
|
||||||
result: null,
|
|
||||||
// TODO: When auth is implemented: owner
|
|
||||||
}
|
}
|
||||||
const activeWorkflow = activeWorkflows[workflowID];
|
const activeWorkflow = activeWorkflows[workflowID];
|
||||||
|
|
||||||
@ -77,13 +91,11 @@ router.post("/:workflowUuid?", [
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Handle when this throws errors
|
traverseOperations(workflow.operations, inputs, (state) => {
|
||||||
let pdfResults = await traverseOperations(workflow.operations, inputs, (state) => {
|
|
||||||
console.log("State: ", state);
|
console.log("State: ", state);
|
||||||
if(activeWorkflow.eventStream)
|
if(activeWorkflow.eventStream)
|
||||||
activeWorkflow.eventStream.write(`data: ${state}\n\n`);
|
activeWorkflow.eventStream.write(`data: ${state}\n\n`);
|
||||||
})
|
}).then(async (pdfResults) => {
|
||||||
|
|
||||||
if(activeWorkflow.eventStream) {
|
if(activeWorkflow.eventStream) {
|
||||||
activeWorkflow.eventStream.write(`data: processing done\n\n`);
|
activeWorkflow.eventStream.write(`data: processing done\n\n`);
|
||||||
activeWorkflow.eventStream.end();
|
activeWorkflow.eventStream.end();
|
||||||
@ -91,6 +103,34 @@ router.post("/:workflowUuid?", [
|
|||||||
|
|
||||||
activeWorkflow.result = pdfResults;
|
activeWorkflow.result = pdfResults;
|
||||||
activeWorkflow.finished = true;
|
activeWorkflow.finished = true;
|
||||||
|
activeWorkflow.finishedAt = Date.now();
|
||||||
|
}).catch((err) => {
|
||||||
|
if(err.validationError) {
|
||||||
|
activeWorkflow.error = {type: 500, error: err};
|
||||||
|
activeWorkflow.finished = true;
|
||||||
|
activeWorkflow.finishedAt = Date.now();
|
||||||
|
|
||||||
|
// Bad Request
|
||||||
|
if(activeWorkflow.eventStream) {
|
||||||
|
activeWorkflow.eventStream.write(`data: ${activeWorkflow.error}\n\n`);
|
||||||
|
activeWorkflow.eventStream.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (err instanceof Error) {
|
||||||
|
console.error("Internal Server Error", err);
|
||||||
|
activeWorkflow.error = {type: 400, error: err.message, stack: err.stack};
|
||||||
|
activeWorkflow.finished = true;
|
||||||
|
activeWorkflow.finishedAt = Date.now();
|
||||||
|
|
||||||
|
// Internal Server Error
|
||||||
|
if(activeWorkflow.eventStream) {
|
||||||
|
activeWorkflow.eventStream.write(`data: ${activeWorkflow.error}\n\n`);
|
||||||
|
activeWorkflow.eventStream.end();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
@ -107,7 +147,7 @@ router.get("/progress/:workflowUuid", (req: Request, res: Response) => {
|
|||||||
|
|
||||||
// Return current progress
|
// Return current progress
|
||||||
const workflow = activeWorkflows[req.params.workflowUuid];
|
const workflow = activeWorkflows[req.params.workflowUuid];
|
||||||
res.status(200).json({ createdAt: workflow.createdAt, finished: workflow.finished });
|
res.status(200).json({ createdAt: workflow.createdAt, finished: workflow.finished, finishedAt: workflow.finishedAt, error: workflow.error });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/progress-stream/:workflowUuid", (req: Request, res: Response) => {
|
router.get("/progress-stream/:workflowUuid", (req: Request, res: Response) => {
|
||||||
|
@ -21,7 +21,7 @@ export class Impose extends Operator {
|
|||||||
|
|
||||||
/** PDF-Imposition, PDF-N-Up: Put multiple pages of the input document into a single page of the output document. - see: {@link https://en.wikipedia.org/wiki/N-up} */
|
/** PDF-Imposition, PDF-N-Up: Put multiple pages of the input document into a single page of the output document. - see: {@link https://en.wikipedia.org/wiki/N-up} */
|
||||||
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
||||||
return this.nToN<PdfFile, PdfFile>(input, async (input, index, max) => {
|
return this.oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
|
||||||
// https://pdfcpu.io/generate/nup.html
|
// https://pdfcpu.io/generate/nup.html
|
||||||
const uint8Array = await pdfcpuWrapper.oneToOne(
|
const uint8Array = await pdfcpuWrapper.oneToOne(
|
||||||
[
|
[
|
||||||
@ -47,7 +47,7 @@ export class Impose extends Operator {
|
|||||||
progressCallback({ curFileProgress: 1, operationProgress: index/max })
|
progressCallback({ curFileProgress: 1, operationProgress: index/max })
|
||||||
|
|
||||||
console.log("ImposeResult: ", result);
|
console.log("ImposeResult: ", result);
|
||||||
return [result];
|
return result;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,10 +4,15 @@ export enum IOType {
|
|||||||
PDF, Image, Text // TODO: Extend with Document File Types
|
PDF, Image, Text // TODO: Extend with Document File Types
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ValidationResult {
|
||||||
|
valid: boolean,
|
||||||
|
reason?: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface Progress {
|
export interface Progress {
|
||||||
/** 0-1 */
|
/** A percentage between 0-1 describing the progress on the currently processed file */
|
||||||
curFileProgress: number,
|
curFileProgress: number,
|
||||||
/** 0-1 */
|
/** A percentage between 0-1 describing the progress on all input files / operations */
|
||||||
operationProgress: number,
|
operationProgress: number,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,27 +30,24 @@ export class Operator {
|
|||||||
this.actionValues = action.values;
|
this.actionValues = action.values;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Type callback state, it should give updates on the progress of the current operator
|
|
||||||
async run(input: any[], progressCallback: (progress: Progress) => void): Promise<any[]> {
|
async run(input: any[], progressCallback: (progress: Progress) => void): Promise<any[]> {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
validate(): { valid: boolean, reason?: string } {
|
validate(): ValidationResult {
|
||||||
if(!this.actionValues) {
|
if(!this.actionValues) {
|
||||||
return { valid: false, reason: "The Operators action values were empty."}
|
return { valid: false, reason: "The Operators action values were empty."}
|
||||||
}
|
}
|
||||||
return { valid: true };
|
return { valid: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** This function should be used if the Operation may take multiple files as inputs and only outputs one file */
|
||||||
protected async nToOne <I, O>(inputs: I[], callback: (input: I[]) => Promise<O>): Promise<O[]> {
|
protected async nToOne <I, O>(inputs: I[], callback: (input: I[]) => Promise<O>): Promise<O[]> {
|
||||||
return [await callback(inputs)];
|
return [await callback(inputs)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** This function should be used if the Operation takes one file as input and may output multiple files */
|
||||||
protected async oneToN <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O[]>): Promise<O[]> {
|
protected async oneToN <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O[]>): Promise<O[]> {
|
||||||
return this.nToN(inputs, callback); // nToN is able to handle single inputs now.
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async nToN <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O[]>): Promise<O[]> {
|
|
||||||
let output: O[] = []
|
let output: O[] = []
|
||||||
for (let i = 0; i < inputs.length; i++) {
|
for (let i = 0; i < inputs.length; i++) {
|
||||||
output = output.concat(await callback(inputs[i], i, inputs.length));
|
output = output.concat(await callback(inputs[i], i, inputs.length));
|
||||||
@ -53,4 +55,11 @@ export class Operator {
|
|||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** This function should be used if the Operation takes one file as input and outputs only one file */
|
||||||
|
protected async oneToOne <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O>): Promise<O[]> {
|
||||||
|
return this.oneToN(inputs, async (input, index, max) => {
|
||||||
|
return [await callback(input, index, max)]
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import { Operator } from "../functions";
|
import { Operator } from "../functions";
|
||||||
|
|
||||||
// TODO: Import other Operators
|
// TODO: Import other Operators (could make this dynamic?)
|
||||||
import { Impose } from "../functions/impose";
|
import { Impose } from "../functions/impose";
|
||||||
export const Operators = {
|
export const Operators = {
|
||||||
Impose: Impose
|
Impose: Impose
|
||||||
@ -24,3 +24,8 @@ export function getOperatorByName(name: string): typeof Operator {
|
|||||||
|
|
||||||
return foundClass;
|
return foundClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function listOperatorNames(): string[] {
|
||||||
|
// TODO: Implement this
|
||||||
|
return
|
||||||
|
}
|
@ -42,78 +42,13 @@ export async function traverseOperations(operations: Action[], input: PdfFile[],
|
|||||||
case "wait":
|
case "wait":
|
||||||
const waitOperation = waitOperations[(action as WaitAction).values.id];
|
const waitOperation = waitOperations[(action as WaitAction).values.id];
|
||||||
|
|
||||||
if(Array.isArray(input)) {
|
|
||||||
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
|
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
|
||||||
}
|
|
||||||
else {
|
|
||||||
waitOperation.input.push(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
waitOperation.waitCount--;
|
waitOperation.waitCount--;
|
||||||
if(waitOperation.waitCount == 0 && waitOperation.doneOperation.actions) {
|
if(waitOperation.waitCount == 0 && waitOperation.doneOperation.actions) {
|
||||||
await nextOperation(waitOperation.doneOperation.actions, waitOperation.input, progressCallback);
|
await nextOperation(waitOperation.doneOperation.actions, waitOperation.input, progressCallback);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/*case "extract":
|
|
||||||
yield* nToN(input, action, async (input) => {
|
|
||||||
const newPdf = await Operations.extractPages({file: input, pageIndexes: action.values["pageIndexes"]});
|
|
||||||
return newPdf;
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "impose":
|
|
||||||
let impose = new Impose(action);
|
|
||||||
input = await impose.run(input, progressCallback);
|
|
||||||
await nextOperation(action.actions, input, progressCallback);
|
|
||||||
break;
|
|
||||||
case "merge":
|
|
||||||
yield* nToOne(input, action, async (inputs) => {
|
|
||||||
const newPdf = await Operations.mergePDFs({files: inputs});
|
|
||||||
return newPdf;
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "removeBlankPages":
|
|
||||||
yield* nToN(input, action, async (input) => {
|
|
||||||
const newPdf = await Operations.removeBlankPages({file: input, whiteThreashold: action.values["whiteThreashold"]});
|
|
||||||
return newPdf;
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "rotate":
|
|
||||||
yield* nToN(input, action, async (input) => {
|
|
||||||
const newPdf = await Operations.rotatePages({file: input, rotation: action.values["rotation"]});
|
|
||||||
return newPdf;
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "sortPagesWithPreset":
|
|
||||||
yield* nToN(input, action, async (input) => {
|
|
||||||
const newPdf = await Operations.arrangePages({file: input, arrangementConfig: action.values["arrangementConfig"]});
|
|
||||||
return newPdf;
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "split":
|
|
||||||
// TODO: A split might break the done condition, it may count multiple times. Needs further testing!
|
|
||||||
yield* oneToN(input, action, async (input) => {
|
|
||||||
const splitResult = await Operations.splitPdfByIndex({file: input, pageIndexes: action.values["splitAfterPageArray"]});
|
|
||||||
for (let j = 0; j < splitResult.length; j++) {
|
|
||||||
splitResult[j].filename = splitResult[j].filename + "_split" + j;
|
|
||||||
}
|
|
||||||
return splitResult;
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "splitOn":
|
|
||||||
yield* oneToN(input, action, async (input) => {
|
|
||||||
const splitResult = await Operations.splitPagesByPreset({file: input, type: action.values["type"], whiteThreashold: action.values["whiteThreashold"]});
|
|
||||||
for (let j = 0; j < splitResult.length; j++) {
|
|
||||||
splitResult[j].filename = splitResult[j].filename + "_split" + j;
|
|
||||||
}
|
|
||||||
return splitResult;
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "updateMetadata":
|
|
||||||
yield* nToN(input, action, async (input) => {
|
|
||||||
const newPdf = await Operations.updateMetadata({file: input, ...action.values["metadata"]});
|
|
||||||
return newPdf;
|
|
||||||
});
|
|
||||||
break;*/
|
|
||||||
default:
|
default:
|
||||||
const operator = getOperatorByName(action.type);
|
const operator = getOperatorByName(action.type);
|
||||||
if(operator) {
|
if(operator) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user