Stirling-PDF/shared-operations/workflow/traverseOperations.js

206 lines
7.6 KiB
JavaScript
Raw Normal View History

2023-10-17 01:38:51 +02:00
import { organizeWaitOperations } from "./organizeWaitOperations.js";
2023-11-07 00:11:14 +01:00
/**
* @typedef PDF
* @property {string} originalFileName
* @property {string} fileName
* @property {Uint8Array} buffer
*/
2023-10-22 18:30:45 +02:00
/**
*
2023-11-07 00:11:14 +01:00
* @param {JSON} operations
* @param {PDF|PDF[]} input
* @returns {}
2023-10-22 18:30:45 +02:00
*/
export async function * traverseOperations(operations, input, Operations) {
2023-10-17 01:38:51 +02:00
const waitOperations = organizeWaitOperations(operations);
2023-11-07 00:11:14 +01:00
/** @type {PDF[]} */ let results = [];
yield* nextOperation(operations, input);
return results;
2023-10-17 01:38:51 +02:00
2023-11-07 00:11:14 +01:00
/**
*
* @param {JSON} operations
* @param {PDF|PDF[]} input
* @returns {undefined}
*/
async function * nextOperation(operations, input) {
2023-10-17 01:38:51 +02:00
if(Array.isArray(operations) && operations.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 < operations.length; i++) {
yield* computeOperation(operations[i], structuredClone(input));
2023-10-17 01:38:51 +02:00
}
}
2023-11-07 00:11:14 +01:00
/**
*
* @param {JSON} operation
* @param {PDF|PDF[]} input
* @returns {undefined}
*/
async function * computeOperation(operation, input) {
yield "Starting: " + operation.type;
2023-10-17 01:38:51 +02:00
switch (operation.type) {
case "done": // Skip this, because it is a valid node.
2023-10-17 01:38:51 +02:00
break;
case "wait":
const waitOperation = waitOperations[operation.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) {
yield* nextOperation(waitOperation.doneOperation.operations, waitOperation.input);
2023-10-17 03:40:54 +02:00
}
break;
case "extract":
yield* nToN(input, operation, async (input) => {
input.fileName += "_extractedPages";
input.buffer = await Operations.extractPages(input.buffer, operation.values["pagesToExtractArray"]);
});
2023-10-17 01:38:51 +02:00
break;
case "impose":
yield* nToN(input, operation, async (input) => {
input.fileName += "_imposed";
input.buffer = await Operations.impose(input.buffer, operation.values["nup"], operation.values["format"]);
});
2023-10-17 01:38:51 +02:00
break;
case "merge":
yield* nToOne(input, operation, async (inputs) => {
return {
originalFileName: inputs.map(input => input.originalFileName).join("_and_"),
fileName: inputs.map(input => input.fileName).join("_and_") + "_merged",
buffer: await Operations.mergePDFs(inputs.map(input => input.buffer))
2023-10-17 03:40:54 +02:00
}
});
2023-10-17 03:40:54 +02:00
break;
case "rotate":
yield* nToN(input, operation, async (input) => {
2023-10-17 03:40:54 +02:00
input.fileName += "_turned";
input.buffer = await Operations.rotatePages(input.buffer, operation.values["rotation"]);
});
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, operation, async (input) => {
const splitResult = await Operations.splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
const splits = [];
for (let j = 0; j < splitResult.length; j++) {
splits.push({
originalFileName: input.originalFileName,
fileName: input.fileName + "_split" + j,
buffer: splitResult[j]
})
2023-10-18 01:20:31 +02:00
}
2023-10-28 19:30:12 +02:00
return splits;
});
2023-10-18 01:20:31 +02:00
break;
case "updateMetadata":
2023-10-22 18:30:45 +02:00
yield* nToN(input, operation, async (input) => {
input.fileName += "_metadataEdited";
input.buffer = await Operations.updateMetadata(input.buffer, operation.values["metadata"]);
2023-10-22 18:30:45 +02:00
});
break;
2023-10-23 14:11:49 +02:00
case "organizePages":
yield* nToN(input, operation, async (input) => {
input.fileName += "_pagesOrganized";
input.buffer = await Operations.organizePages(input.buffer, operation.values["operation"], operation.values["customOrderString"]);
2023-10-23 14:11:49 +02:00
});
break;
case "removeBlankPages":
yield* nToN(input, operation, async (input) => {
input.fileName += "_removedBlanks";
input.buffer = await Operations.removeBlankPages(input.buffer, operation.values["whiteThreashold"]);
});
break;
2023-10-27 02:56:13 +02:00
case "splitOn":
yield* oneToN(input, operation, async (input) => {
const splitResult = await Operations.splitOn(input.buffer, operation.values["type"], operation.values["whiteThreashold"]);
2023-10-28 19:30:12 +02:00
const splits = [];
for (let j = 0; j < splitResult.length; j++) {
splits.push({
originalFileName: input.originalFileName,
fileName: input.fileName + "_split" + j,
buffer: splitResult[j]
})
}
return splits;
2023-10-27 02:56:13 +02:00
});
break;
2023-10-17 01:38:51 +02:00
default:
throw new Error(`${operation.type} not implemented yet.`);
2023-10-17 01:38:51 +02:00
break;
}
}
2023-11-07 00:11:14 +01:00
/**
*
* @param {PDF|PDF[]} input
* @param {JSON} operation
* @returns {undefined}
*/
async function * nToOne(inputs, operation, callback) {
2023-11-07 00:11:14 +01:00
inputs = Array.from(inputs); // Convert single values to array, keep arrays as is.
inputs = await callback(inputs);
yield* nextOperation(operation.operations, inputs);
}
2023-11-07 00:11:14 +01:00
/**
*
* @param {PDF|PDF[]} input
* @param {JSON} operation
* @returns {undefined}
*/
async function * oneToN(input, operation, callback) {
if(Array.isArray(input)) {
2023-10-28 19:30:12 +02:00
let output = [];
for (let i = 0; i < input.length; i++) {
2023-10-28 19:30:12 +02:00
output = output.concat(await callback(input[i]));
}
2023-10-28 19:30:12 +02:00
yield* nextOperation(operation.operations, output);
}
else {
2023-10-28 19:30:12 +02:00
input = await callback(input);
yield* nextOperation(operation.operations, input);
}
}
2023-11-07 00:11:14 +01:00
/**
*
* @param {PDF|PDF[]} input
* @param {JSON} operation
* @returns {undefined}
*/
async function * nToN(input, operation, callback) {
if(Array.isArray(input)) {
for (let i = 0; i < input.length; i++) {
await callback(input[i]);
}
yield* nextOperation(operation.operations, input);
}
else {
await callback(input);
yield* nextOperation(operation.operations, input);
}
}
2023-10-17 01:38:51 +02:00
}