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
|
|
|
*/
|
2023-11-10 21:23:18 +01: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 = [];
|
2023-10-22 01:11:17 +02:00
|
|
|
yield* nextOperation(operations, input);
|
2023-10-17 02:14:06 +02:00
|
|
|
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}
|
|
|
|
*/
|
2023-10-22 01:11:17 +02:00
|
|
|
async function * nextOperation(operations, input) {
|
2023-10-17 01:38:51 +02:00
|
|
|
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
|
2023-10-22 01:11:17 +02:00
|
|
|
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++) {
|
2023-10-22 01:11:17 +02:00
|
|
|
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}
|
|
|
|
*/
|
2023-10-22 01:11:17 +02:00
|
|
|
async function * computeOperation(operation, input) {
|
|
|
|
yield "Starting: " + operation.type;
|
2023-10-17 01:38:51 +02:00
|
|
|
switch (operation.type) {
|
2023-10-22 01:11:17 +02:00
|
|
|
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)) {
|
2023-10-22 01:11:17 +02:00
|
|
|
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-10-22 01:11:17 +02:00
|
|
|
waitOperation.input.push(input);
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
2023-10-17 03:40:54 +02:00
|
|
|
|
2023-10-22 01:11:17 +02:00
|
|
|
waitOperation.waitCount--;
|
|
|
|
if(waitOperation.waitCount == 0) {
|
|
|
|
yield* nextOperation(waitOperation.doneOperation.operations, waitOperation.input);
|
2023-10-17 03:40:54 +02:00
|
|
|
}
|
|
|
|
break;
|
2023-10-22 01:11:17 +02:00
|
|
|
case "extract":
|
|
|
|
yield* nToN(input, operation, async (input) => {
|
|
|
|
input.fileName += "_extractedPages";
|
2023-11-10 21:23:18 +01:00
|
|
|
input.buffer = await Operations.extractPages(input.buffer, operation.values["pagesToExtractArray"]);
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-17 01:38:51 +02:00
|
|
|
break;
|
2023-10-22 01:11:17 +02:00
|
|
|
case "impose":
|
|
|
|
yield* nToN(input, operation, async (input) => {
|
|
|
|
input.fileName += "_imposed";
|
2023-11-10 21:23:18 +01:00
|
|
|
input.buffer = await Operations.impose(input.buffer, operation.values["nup"], operation.values["format"]);
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-17 01:38:51 +02:00
|
|
|
break;
|
|
|
|
case "merge":
|
2023-10-22 01:11:17 +02:00
|
|
|
yield* nToOne(input, operation, async (inputs) => {
|
|
|
|
return {
|
2023-10-17 02:14:06 +02:00
|
|
|
originalFileName: inputs.map(input => input.originalFileName).join("_and_"),
|
|
|
|
fileName: inputs.map(input => input.fileName).join("_and_") + "_merged",
|
2023-11-10 21:23:18 +01:00
|
|
|
buffer: await Operations.mergePDFs(inputs.map(input => input.buffer))
|
2023-10-17 03:40:54 +02:00
|
|
|
}
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-17 03:40:54 +02:00
|
|
|
break;
|
|
|
|
case "rotate":
|
2023-10-22 01:11:17 +02:00
|
|
|
yield* nToN(input, operation, async (input) => {
|
2023-10-17 03:40:54 +02:00
|
|
|
input.fileName += "_turned";
|
2023-11-10 21:23:18 +01:00
|
|
|
input.buffer = await Operations.rotatePages(input.buffer, operation.values["rotation"]);
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-17 03:40:54 +02:00
|
|
|
break;
|
2023-10-22 01:11:17 +02:00
|
|
|
case "split":
|
|
|
|
// TODO: A split might break the done condition, it may count multiple times. Needs further testing!
|
|
|
|
yield* oneToN(input, operation, async (input) => {
|
2023-11-10 21:23:18 +01:00
|
|
|
const splitResult = await Operations.splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
|
2023-10-22 01:11:17 +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-18 01:20:31 +02:00
|
|
|
}
|
2023-10-28 19:30:12 +02:00
|
|
|
return splits;
|
2023-10-22 01:11:17 +02:00
|
|
|
});
|
2023-10-18 01:20:31 +02:00
|
|
|
break;
|
2023-10-22 18:30:45 +02:00
|
|
|
case "editMetadata":
|
|
|
|
yield* nToN(input, operation, async (input) => {
|
|
|
|
input.fileName += "_metadataEdited";
|
2023-11-10 21:23:18 +01:00
|
|
|
input.buffer = await Operations.editMetadata(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";
|
2023-11-10 21:23:18 +01:00
|
|
|
input.buffer = await Operations.organizePages(input.buffer, operation.values["operation"], operation.values["customOrderString"]);
|
2023-10-23 14:11:49 +02:00
|
|
|
});
|
|
|
|
break;
|
2023-10-24 16:09:10 +02:00
|
|
|
case "removeBlankPages":
|
|
|
|
yield* nToN(input, operation, async (input) => {
|
|
|
|
input.fileName += "_removedBlanks";
|
2023-11-10 21:23:18 +01:00
|
|
|
input.buffer = await Operations.removeBlankPages(input.buffer, operation.values["whiteThreashold"]);
|
2023-10-24 16:09:10 +02:00
|
|
|
});
|
|
|
|
break;
|
2023-10-27 02:56:13 +02:00
|
|
|
case "splitOn":
|
|
|
|
yield* oneToN(input, operation, async (input) => {
|
2023-11-10 21:23:18 +01:00
|
|
|
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:
|
2023-10-22 01:11:17 +02:00
|
|
|
throw new Error(`${operation.type} not implemented yet.`);
|
2023-10-17 01:38:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-10-22 01:11:17 +02:00
|
|
|
|
2023-11-07 00:11:14 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {PDF|PDF[]} input
|
|
|
|
* @param {JSON} operation
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
2023-10-22 01:11:17 +02:00
|
|
|
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.
|
2023-10-22 01:11:17 +02:00
|
|
|
|
|
|
|
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}
|
|
|
|
*/
|
2023-10-22 01:11:17 +02:00
|
|
|
async function * oneToN(input, operation, callback) {
|
|
|
|
if(Array.isArray(input)) {
|
2023-10-28 19:30:12 +02:00
|
|
|
let output = [];
|
2023-10-22 01:11:17 +02:00
|
|
|
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-22 01:11:17 +02:00
|
|
|
}
|
2023-10-28 19:30:12 +02:00
|
|
|
yield* nextOperation(operation.operations, output);
|
2023-10-22 01:11:17 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-10-28 19:30:12 +02:00
|
|
|
input = await callback(input);
|
2023-10-22 01:11:17 +02:00
|
|
|
yield* nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 00:11:14 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {PDF|PDF[]} input
|
|
|
|
* @param {JSON} operation
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
2023-10-22 01:11:17 +02:00
|
|
|
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
|
|
|
}
|