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

181 lines
7.9 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 { OperationsType } from "../../src/index";
import { PdfFile } from "../wrappers/PdfFile";
2023-10-17 01:38:51 +02:00
export async function * traverseOperations(operations: Action[], input: PdfFile[] | PdfFile, Operations: OperationsType): AsyncGenerator<string, PdfFile[], void> {
2023-10-17 01:38:51 +02:00
const waitOperations = organizeWaitOperations(operations);
let results: PdfFile[] = [];
yield* nextOperation(operations, input);
return results;
2023-10-17 01:38:51 +02:00
async function * nextOperation(actions: Action[], input: PdfFile[] | PdfFile): AsyncGenerator<string, void, void> {
if(Array.isArray(actions) && actions.length == 0) { // isEmpty
if(Array.isArray(input)) {
console.log("operation done: " + input[0].filename + (input.length > 1 ? "+" : ""));
results = results.concat(input);
return;
}
else {
console.log("operation done: " + input.filename);
results.push(input);
return;
}
2023-10-17 01:38:51 +02:00
}
for (let i = 0; i < actions.length; i++) {
yield* computeOperation(actions[i], structuredClone(input));
2023-10-17 01:38:51 +02:00
}
}
async function * computeOperation(action: Action, input: PdfFile|PdfFile[]): AsyncGenerator<string, void, void> {
yield "Starting: " + action.type;
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) {
yield* nextOperation(waitOperation.doneOperation.actions, waitOperation.input);
2023-10-17 03:40:54 +02:00
}
break;
case "extract":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.selectPages({file: input, pagesToExtractArray: action.values["pagesToExtractArray"]});
newPdf.filename += "_extractedPages";
return newPdf;
});
2023-10-17 01:38:51 +02:00
break;
case "impose":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.impose({file: input, nup: action.values["nup"], format: action.values["format"]});
newPdf.filename += "_imposed";
return newPdf;
});
2023-10-17 01:38:51 +02:00
break;
case "merge":
yield* nToOne(input, action, async (inputs) => {
const newPdf = await Operations.mergePDFs({files: inputs});
newPdf.filename = inputs.map(input => input.filename).join("_and_") + "_merged";
return newPdf;
});
2023-10-17 03:40:54 +02:00
break;
case "rotate":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.rotatePages({file: input, rotation: action.values["rotation"]});
newPdf.filename += "_turned";
return newPdf;
});
2023-10-17 03:40:54 +02:00
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.splitPDF({file: input, splitAfterPageArray: action.values["splitAfterPageArray"]});
for (let j = 0; j < splitResult.length; j++) {
splitResult[j].filename = splitResult[j].filename + "_split" + j;
2023-10-18 01:20:31 +02:00
}
return splitResult;
});
2023-10-18 01:20:31 +02:00
break;
case "updateMetadata":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.updateMetadata({file: input, ...action.values["metadata"]});
newPdf.filename += "_metadataEdited";
return newPdf;
2023-10-22 18:30:45 +02:00
});
break;
case "sortPagesWithPreset":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.sortPagesWithPreset({file: input, sortPreset: action.values["sortPreset"], fancyPageSelector: action.values["fancyPageSelector"]});
newPdf.filename += "_pagesOrganized";
return newPdf;
2023-10-23 14:11:49 +02:00
});
break;
case "removeBlankPages":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.removeBlankPages({file: input, whiteThreashold: action.values["whiteThreashold"]});
newPdf.filename += "_removedBlanks";
return newPdf;
});
break;
2023-10-27 02:56:13 +02:00
case "splitOn":
yield* oneToN(input, action, async (input) => {
const splitResult = await Operations.splitOn({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-10-17 01:38:51 +02:00
default:
throw new Error(`${action.type} not implemented yet.`);
2023-10-17 01:38:51 +02:00
break;
}
}
2023-11-07 00:11:14 +01:00
/**
*
* @param {PdfFile|PdfFile[]} input
* @param {JSON} action
2023-11-07 00:11:14 +01:00
* @returns {undefined}
*/
async function * nToOne(inputs: PdfFile|PdfFile[], action: Action, callback: (pdf: PdfFile[]) => Promise<PdfFile>): AsyncGenerator<string, void, void> {
const input = Array.isArray(inputs) ? inputs : [inputs]; // Convert single values to array, keep arrays as is.
const newInputs = await callback(input);
if (action.actions) {
yield* nextOperation(action.actions, newInputs);
}
}
2023-11-07 00:11:14 +01:00
/**
*
* @param {PdfFile|PdfFile[]} input
* @param {JSON} action
2023-11-07 00:11:14 +01:00
* @returns {undefined}
*/
async function * oneToN(input: PdfFile|PdfFile[], action: Action, callback: (pdf: PdfFile) => Promise<PdfFile[]>): AsyncGenerator<string, void, void> {
if(Array.isArray(input)) {
let output: PdfFile[] = [];
for (let i = 0; i < input.length; i++) {
2023-10-28 19:30:12 +02:00
output = output.concat(await callback(input[i]));
}
if (action.actions) {
yield* nextOperation(action.actions, output);
}
}
else {
const nextInput = await callback(input);
if (action.actions) {
yield* nextOperation(action.actions, nextInput);
}
}
}
async function * nToN(input: PdfFile|PdfFile[], action: Action, callback: (pdf: PdfFile) => Promise<PdfFile>): AsyncGenerator<string, void, void> {
if(Array.isArray(input)) {
const nextInputs: PdfFile[] = []
for (let i = 0; i < input.length; i++) {
nextInputs.concat(await callback(input[i]));
}
if (action.actions) {
yield* nextOperation(action.actions, nextInputs);
}
}
else {
const nextInput = await callback(input);
if (action.actions) {
yield* nextOperation(action.actions, nextInput);
}
}
}
2023-10-17 01:38:51 +02:00
}