Files
Stirling-PDF/shared-operations/src/workflow/traverseOperations.ts

128 lines
6.1 KiB
TypeScript
Raw Normal View History

import { organizeWaitOperations } from "./organizeWaitOperations";
2023-11-14 17:34:50 +01:00
import { Action, WaitAction } from "../../declarations/Action";
import { PdfFile } from "../wrappers/PdfFile";
import { Progress } from "../functions";
2023-11-20 22:12:03 +01:00
import { validateOperations } from "./validateOperations";
import { getOperatorByName } from "./getOperatorByName";
2023-10-17 01:38:51 +02:00
export async function traverseOperations(operations: Action[], input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
2023-11-20 22:12:03 +01:00
const validationResult = validateOperations(operations)
if(!validationResult.valid) {
throw Error(validationResult.reason);
}
2023-10-17 01:38:51 +02:00
const waitOperations = organizeWaitOperations(operations);
let results: PdfFile[] = [];
await nextOperation(operations, input, progressCallback);
return results;
2023-10-17 01:38:51 +02:00
async function nextOperation(actions: Action[] | undefined, input: PdfFile[], progressCallback: (state: Progress) => void): Promise<void> {
console.log("Next Operation");
if(actions === undefined || (Array.isArray(actions) && actions.length == 0)) { // isEmpty
console.log("Last Operation");
if(Array.isArray(input)) {
console.log("ArrayOut: ", input);
console.log("operation done: " + input[0].filename + (input.length > 1 ? "+" : ""));
results = results.concat(input);
return;
}
2023-10-17 01:38:51 +02:00
}
for (let i = 0; i < actions.length; i++) {
await computeOperation(actions[i], Object.assign([], input), progressCallback); // structuredClone-like for ts TODO: test if this really works
2023-10-17 01:38:51 +02:00
}
}
async function computeOperation(action: Action, input: PdfFile[], progressCallback: (state: Progress) => void): Promise<void> {
console.log("Input: ", input);
switch (action.type) {
case "done": // Skip this, because it is a valid node.
2023-10-17 01:38:51 +02:00
break;
case "wait":
2023-11-14 17:34:50 +01:00
const waitOperation = waitOperations[(action as WaitAction).values.id];
2023-10-17 03:40:54 +02:00
2023-10-17 01:38:51 +02:00
if(Array.isArray(input)) {
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
2023-10-17 01:38:51 +02:00
}
else {
waitOperation.input.push(input);
2023-10-17 01:38:51 +02:00
}
2023-10-17 03:40:54 +02:00
waitOperation.waitCount--;
if(waitOperation.waitCount == 0 && waitOperation.doneOperation.actions) {
await nextOperation(waitOperation.doneOperation.actions, waitOperation.input, progressCallback);
2023-10-17 03:40:54 +02:00
}
break;
/*case "extract":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.extractPages({file: input, pageIndexes: action.values["pageIndexes"]});
return newPdf;
});
2023-11-20 22:12:03 +01:00
break;
case "impose":
let impose = new Impose(action);
2023-11-20 22:12:03 +01:00
input = await impose.run(input, progressCallback);
await nextOperation(action.actions, input, progressCallback);
2023-10-17 01:38:51 +02:00
break;
2023-11-20 22:12:03 +01:00
case "merge":
yield* nToOne(input, action, async (inputs) => {
const newPdf = await Operations.mergePDFs({files: inputs});
return newPdf;
});
2023-10-17 03:40:54 +02:00
break;
2023-11-17 00:45:37 +03:00
case "removeBlankPages":
yield* nToN(input, action, async (input) => {
2023-11-17 00:45:37 +03:00
const newPdf = await Operations.removeBlankPages({file: input, whiteThreashold: action.values["whiteThreashold"]});
return newPdf;
});
2023-10-17 03:40:54 +02:00
break;
2023-11-17 00:45:37 +03:00
case "rotate":
yield* nToN(input, action, async (input) => {
2023-11-17 00:45:37 +03:00
const newPdf = await Operations.rotatePages({file: input, rotation: action.values["rotation"]});
return newPdf;
2023-10-22 18:30:45 +02:00
});
break;
case "sortPagesWithPreset":
yield* nToN(input, action, async (input) => {
2023-11-17 17:13:42 +03:00
const newPdf = await Operations.arrangePages({file: input, arrangementConfig: action.values["arrangementConfig"]});
return newPdf;
2023-10-23 14:11:49 +02:00
});
break;
2023-11-17 00:45:37 +03:00
case "split":
// TODO: A split might break the done condition, it may count multiple times. Needs further testing!
yield* oneToN(input, action, async (input) => {
2023-11-17 16:11:06 +03:00
const splitResult = await Operations.splitPdfByIndex({file: input, pageIndexes: action.values["splitAfterPageArray"]});
2023-11-17 00:45:37 +03:00
for (let j = 0; j < splitResult.length; j++) {
splitResult[j].filename = splitResult[j].filename + "_split" + j;
}
return splitResult;
});
break;
2023-10-27 02:56:13 +02:00
case "splitOn":
yield* oneToN(input, action, async (input) => {
2023-11-17 16:11:06 +03:00
const splitResult = await Operations.splitPagesByPreset({file: input, type: action.values["type"], whiteThreashold: action.values["whiteThreashold"]});
2023-10-28 19:30:12 +02:00
for (let j = 0; j < splitResult.length; j++) {
splitResult[j].filename = splitResult[j].filename + "_split" + j;
2023-10-28 19:30:12 +02:00
}
return splitResult;
2023-10-27 02:56:13 +02:00
});
break;
2023-11-17 00:45:37 +03:00
case "updateMetadata":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.updateMetadata({file: input, ...action.values["metadata"]});
return newPdf;
});
break;*/
2023-10-17 01:38:51 +02:00
default:
2023-11-20 22:12:03 +01:00
const operator = getOperatorByName(action.type);
if(operator) {
let opteration = new operator(action);
input = await opteration.run(input, progressCallback);
await nextOperation(action.actions, input, progressCallback);
}
else
throw new Error(`${action.type} not implemented yet.`);
2023-10-17 01:38:51 +02:00
}
}
}