Stirling-PDF/traverseOperations.js

139 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-10-19 19:46:23 +02:00
import { extractPages } from "./functions/extractPages.js";
import { impose } from "./functions/impose.js";
import { mergePDFs } from "./functions/mergePDFs.js";
import { rotatePages } from "./functions/rotatePDF.js";
import { splitPDF } from "./functions/splitPDF.js";
import { organizeWaitOperations } from "./public/organizeWaitOperations.js";
2023-10-19 21:23:58 +02:00
export async function * traverseOperations(operations, input) {
2023-10-19 19:46:23 +02:00
const waitOperations = organizeWaitOperations(operations);
2023-10-20 02:27:16 +02:00
let results = [];
yield* nextOperation(operations, input)
2023-10-19 19:46:23 +02:00
return results;
2023-10-19 21:23:58 +02:00
async function * nextOperation(operations, input) {
2023-10-20 02:51:58 +02:00
console.log(Array.isArray(operations) && operations.length == 0);
2023-10-19 19:46:23 +02:00
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
2023-10-20 02:27:16 +02:00
if(Array.isArray(input)) {
console.log("operation done: " + input[0].fileName + "+");
results = results.concat(input);
return;
}
else {
console.log("operation done: " + input.fileName);
results.push(input);
return;
}
2023-10-19 19:46:23 +02:00
}
for (let i = 0; i < operations.length; i++) {
yield* computeOperation(operations[i], structuredClone(input));
2023-10-19 19:46:23 +02:00
}
}
2023-10-19 21:23:58 +02:00
async function * computeOperation(operation, input) {
2023-10-20 02:51:58 +02:00
yield "Starting: " + operation.type;
2023-10-19 19:46:23 +02:00
switch (operation.type) {
case "wait":
const waitOperation = waitOperations[operation.values.id];
2023-10-20 02:10:03 +02:00
if(Array.isArray(input)) {
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
2023-10-20 02:10:03 +02:00
}
else {
waitOperation.input.push(input);
}
2023-10-19 19:46:23 +02:00
waitOperation.waitCount--;
if(waitOperation.waitCount == 0) {
yield* nextOperation(waitOperation.doneOperation.operations, waitOperation.input);
2023-10-19 19:46:23 +02:00
}
break;
case "extract":
yield * nToN(input, operation, async (input) => {
2023-10-19 19:46:23 +02:00
input.fileName += "_extractedPages";
input.buffer = await extractPages(input.buffer, operation.values["pagesToExtractArray"]);
});
2023-10-19 19:46:23 +02:00
break;
case "split":
// TODO: A split might break the done condition, it may count multiple times. Needs further testing!
2023-10-20 02:27:16 +02:00
yield * oneToN(input, operation, async (input) => {
2023-10-20 02:27:16 +02:00
const splitResult = await splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
2023-10-19 19:46:23 +02:00
2023-10-20 02:27:16 +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]
2023-10-20 02:27:16 +02:00
})
}
2023-10-19 19:46:23 +02:00
input = splits;
});
2023-10-19 19:46:23 +02:00
break;
case "merge":
yield * nToOne(input, operation, async (input) => {
2023-10-19 19:46:23 +02:00
const inputs = input;
input = {
originalFileName: inputs.map(input => input.originalFileName).join("_and_"),
fileName: inputs.map(input => input.fileName).join("_and_") + "_merged",
buffer: await mergePDFs(inputs.map(input => input.buffer))
}
});
2023-10-19 19:46:23 +02:00
break;
case "rotate":
yield * nToN(input, operation, async (input) => {
2023-10-19 19:46:23 +02:00
input.fileName += "_turned";
input.buffer = await rotatePages(input.buffer, operation.values["rotation"]);
});
2023-10-19 19:46:23 +02:00
break;
case "impose":
yield * nToN(input, operation, async (input) => {
2023-10-19 19:46:23 +02:00
input.fileName += "_imposed";
input.buffer = await impose(input.buffer, operation.values["nup"], operation.values["format"]);
});
2023-10-19 19:46:23 +02:00
break;
default:
throw new Error(`${operation.type} not implemented yet.`);
2023-10-19 19:46:23 +02:00
break;
}
}
async function * nToOne(input, operation, callback) {
if(!Array.isArray(input)) {
input = [input];
}
await callback(input);
yield* nextOperation(operation.operations, input);
}
async function * oneToN(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);
}
}
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-19 19:46:23 +02:00
}