2023-10-17 03:40:54 +02:00
|
|
|
import { extractPages } from "./functions/extractPages.js";
|
2023-10-18 01:20:31 +02:00
|
|
|
import { impose } from "./functions/impose.js";
|
2023-10-17 02:14:06 +02:00
|
|
|
import { mergePDFs } from "./functions/mergePDFs.js";
|
2023-10-17 03:40:54 +02:00
|
|
|
import { rotatePages } from "./functions/rotatePDF.js";
|
|
|
|
import { splitPDF } from "./functions/splitPDF.js";
|
2023-10-17 01:38:51 +02:00
|
|
|
import { organizeWaitOperations } from "./organizeWaitOperations.js";
|
|
|
|
|
2023-10-20 02:10:03 +02:00
|
|
|
// TODO: Make this run with feedback like the server side func.
|
2023-10-17 01:38:51 +02:00
|
|
|
export async function traverseOperations(operations, input) {
|
|
|
|
const waitOperations = organizeWaitOperations(operations);
|
2023-10-17 02:14:06 +02:00
|
|
|
const results = [];
|
2023-10-17 01:38:51 +02:00
|
|
|
await nextOperation(operations, input);
|
2023-10-17 02:14:06 +02:00
|
|
|
return results;
|
2023-10-17 01:38:51 +02:00
|
|
|
|
|
|
|
async function nextOperation(operations, input) {
|
|
|
|
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
|
|
|
|
console.log("operation done: " + input.fileName);
|
2023-10-17 02:14:06 +02:00
|
|
|
results.push(input);
|
2023-10-17 01:38:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < operations.length; i++) {
|
|
|
|
await computeOperation(operations[i], structuredClone(input)); // break references
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function computeOperation(operation, input) {
|
|
|
|
switch (operation.type) {
|
|
|
|
case "done":
|
|
|
|
console.log("Done operation will get called if all waits are done. Skipping for now.")
|
|
|
|
break;
|
|
|
|
case "wait":
|
|
|
|
const waitOperation = waitOperations[operation.values.id];
|
|
|
|
waitOperation.input.push(input);
|
|
|
|
waitOperation.waitCount--;
|
|
|
|
if(waitOperation.waitCount == 0) {
|
|
|
|
await nextOperation(waitOperation.doneOperation.operations, waitOperation.input);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "removeObjects":
|
2023-10-17 03:40:54 +02:00
|
|
|
console.warn("RemoveObjects not implemented yet.")
|
|
|
|
|
2023-10-17 01:38:51 +02:00
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
// TODO: modfiy input
|
|
|
|
input[i].fileName += "_removedObjects";
|
|
|
|
await nextOperation(operation.operations, input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO: modfiy input
|
|
|
|
input.fileName += "_removedObjects";
|
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "extract":
|
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
input[i].fileName += "_extractedPages";
|
2023-10-17 03:40:54 +02:00
|
|
|
input[i].buffer = await extractPages(input[i].buffer, operation.values["pagesToExtractArray"]);
|
2023-10-17 01:38:51 +02:00
|
|
|
await nextOperation(operation.operations, input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input.fileName += "_extractedPages";
|
2023-10-17 03:40:54 +02:00
|
|
|
input.buffer = await extractPages(input.buffer, operation.values["pagesToExtractArray"]);
|
2023-10-17 01:38:51 +02:00
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
break;
|
2023-10-17 03:40:54 +02:00
|
|
|
case "split":
|
|
|
|
// TODO: When a split goes into a wait function it might break the done condition, as it will count multiplpe times.
|
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
const splits = await splitPDF(input[i].buffer, operation.values["pagesToSplitAfterArray"]);
|
|
|
|
|
|
|
|
for (let j = 0; j < splits.length; j++) {
|
|
|
|
const split = {};
|
|
|
|
split.originalFileName = input[i].originalFileName;
|
|
|
|
split.fileName = input[i].fileName + "_split";
|
|
|
|
split.buffer = splits[j];
|
|
|
|
await nextOperation(operation.operations, split);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const splits = await splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
|
|
|
|
|
|
|
|
for (let j = 0; j < splits.length; j++) {
|
|
|
|
const split = {};
|
|
|
|
split.originalFileName = input.originalFileName;
|
|
|
|
split.fileName = input.fileName + "_split";
|
|
|
|
split.buffer = splits[j];
|
|
|
|
await nextOperation(operation.operations, split);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-10-17 01:38:51 +02:00
|
|
|
case "fillField":
|
2023-10-17 03:40:54 +02:00
|
|
|
console.warn("FillField not implemented yet.")
|
|
|
|
|
2023-10-17 01:38:51 +02:00
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
// TODO: modfiy input
|
|
|
|
input[i].fileName += "_filledField";
|
|
|
|
await nextOperation(operation.operations, input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO: modfiy input
|
|
|
|
input.fileName += "_filledField";
|
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "extractImages":
|
2023-10-17 03:40:54 +02:00
|
|
|
console.warn("ExtractImages not implemented yet.")
|
|
|
|
|
2023-10-17 01:38:51 +02:00
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
// TODO: modfiy input
|
|
|
|
input[i].fileName += "_extractedImages";
|
|
|
|
await nextOperation(operation.operations, input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO: modfiy input
|
|
|
|
input.fileName += "_extractedImages";
|
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "merge":
|
2023-10-17 02:14:06 +02:00
|
|
|
if(Array.isArray(input) && input.length > 1) {
|
|
|
|
const inputs = input;
|
2023-10-17 01:38:51 +02:00
|
|
|
input = {
|
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",
|
|
|
|
buffer: await mergePDFs(inputs.map(input => input.buffer))
|
2023-10-17 01:38:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-10-17 02:14:06 +02:00
|
|
|
// Only one input, no need to merge
|
2023-10-17 01:38:51 +02:00
|
|
|
input.fileName += "_merged";
|
|
|
|
}
|
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
break;
|
|
|
|
case "transform": {
|
2023-10-17 03:40:54 +02:00
|
|
|
console.warn("Transform not implemented yet.")
|
2023-10-17 01:38:51 +02:00
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
// TODO: modfiy input
|
|
|
|
input[i].fileName += "_transformed";
|
|
|
|
await nextOperation(operation.operations, input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO: modfiy input
|
|
|
|
input.fileName += "_transformed";
|
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-10-17 03:40:54 +02:00
|
|
|
case "extract":
|
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
input[i].fileName += "_extractedPages";
|
|
|
|
input[i].buffer = await extractPages(input[i].buffer, operation.values["pagesToExtractArray"]);
|
|
|
|
await nextOperation(operation.operations, input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input.fileName += "_extractedPages";
|
|
|
|
input.buffer = await extractPages(input.buffer, operation.values["pagesToExtractArray"]);
|
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "rotate":
|
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
input[i].fileName += "_turned";
|
|
|
|
input[i].buffer = await rotatePages(input[i].buffer, operation.values["rotation"]);
|
|
|
|
await nextOperation(operation.operations, input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input.fileName += "_turned";
|
|
|
|
input.buffer = await rotatePages(input.buffer, operation.values["rotation"]);
|
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
break;
|
2023-10-18 01:20:31 +02:00
|
|
|
case "impose":
|
|
|
|
if(Array.isArray(input)) {
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
input[i].fileName += "_imposed";
|
|
|
|
input[i].buffer = await impose(input[i].buffer, operation.values["nup"], operation.values["format"]);
|
|
|
|
await nextOperation(operation.operations, input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input.fileName += "_imposed";
|
|
|
|
input.buffer = await impose(input.buffer, operation.values["nup"], operation.values["format"]);
|
|
|
|
await nextOperation(operation.operations, input);
|
|
|
|
}
|
|
|
|
break;
|
2023-10-17 01:38:51 +02:00
|
|
|
default:
|
|
|
|
console.log("operation type unknown: ", operation.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|