Removed duplicate code of node backend
Frontend traverser needs to be updated
This commit is contained in:
@@ -1,21 +1,17 @@
|
||||
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 * as Functions from "./functions/index.js";
|
||||
import { organizeWaitOperations } from "./public/organizeWaitOperations.js";
|
||||
|
||||
export async function * traverseOperations(operations, input) {
|
||||
const waitOperations = organizeWaitOperations(operations);
|
||||
let results = [];
|
||||
yield* nextOperation(operations, input)
|
||||
yield* nextOperation(operations, input);
|
||||
console.log("Done2");
|
||||
return results;
|
||||
|
||||
async function * nextOperation(operations, input) {
|
||||
console.log(Array.isArray(operations) && operations.length == 0);
|
||||
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
|
||||
if(Array.isArray(input)) {
|
||||
console.log("operation done: " + input[0].fileName + "+");
|
||||
console.log("operation done: " + input[0].fileName + input.length > 1 ? "+" : "");
|
||||
results = results.concat(input);
|
||||
return;
|
||||
}
|
||||
@@ -34,6 +30,8 @@ export async function * traverseOperations(operations, input) {
|
||||
async function * computeOperation(operation, input) {
|
||||
yield "Starting: " + operation.type;
|
||||
switch (operation.type) {
|
||||
case "done": // Skip this, because it is a valid node.
|
||||
break;
|
||||
case "wait":
|
||||
const waitOperation = waitOperations[operation.values.id];
|
||||
|
||||
@@ -50,18 +48,37 @@ export async function * traverseOperations(operations, input) {
|
||||
}
|
||||
break;
|
||||
case "extract":
|
||||
yield * nToN(input, operation, async (input) => {
|
||||
yield* nToN(input, operation, async (input) => {
|
||||
input.fileName += "_extractedPages";
|
||||
input.buffer = await extractPages(input.buffer, operation.values["pagesToExtractArray"]);
|
||||
input.buffer = await Functions.extractPages(input.buffer, operation.values["pagesToExtractArray"]);
|
||||
});
|
||||
break;
|
||||
case "impose":
|
||||
yield* nToN(input, operation, async (input) => {
|
||||
input.fileName += "_imposed";
|
||||
input.buffer = await Functions.impose(input.buffer, operation.values["nup"], operation.values["format"]);
|
||||
});
|
||||
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 Functions.mergePDFs(inputs.map(input => input.buffer))
|
||||
}
|
||||
});
|
||||
break;
|
||||
case "rotate":
|
||||
yield* nToN(input, operation, async (input) => {
|
||||
input.fileName += "_turned";
|
||||
input.buffer = await Functions.rotatePages(input.buffer, operation.values["rotation"]);
|
||||
});
|
||||
|
||||
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 splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
|
||||
|
||||
yield* oneToN(input, operation, async (input) => {
|
||||
const splitResult = await Functions.splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
|
||||
|
||||
const splits = [];
|
||||
for (let j = 0; j < splitResult.length; j++) {
|
||||
splits.push({
|
||||
@@ -70,45 +87,23 @@ export async function * traverseOperations(operations, input) {
|
||||
buffer: splitResult[j]
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
input = splits;
|
||||
});
|
||||
break;
|
||||
case "merge":
|
||||
yield * nToOne(input, operation, async (input) => {
|
||||
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))
|
||||
}
|
||||
});
|
||||
break;
|
||||
case "rotate":
|
||||
yield * nToN(input, operation, async (input) => {
|
||||
input.fileName += "_turned";
|
||||
input.buffer = await rotatePages(input.buffer, operation.values["rotation"]);
|
||||
});
|
||||
break;
|
||||
case "impose":
|
||||
yield * nToN(input, operation, async (input) => {
|
||||
input.fileName += "_imposed";
|
||||
input.buffer = await impose(input.buffer, operation.values["nup"], operation.values["format"]);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
throw new Error(`${operation.type} not implemented yet.`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
async function * nToOne(input, operation, callback) {
|
||||
if(!Array.isArray(input)) {
|
||||
input = [input];
|
||||
async function * nToOne(inputs, operation, callback) {
|
||||
if(!Array.isArray(inputs)) {
|
||||
inputs = [inputs];
|
||||
}
|
||||
|
||||
await callback(input);
|
||||
yield* nextOperation(operation.operations, input);
|
||||
inputs = await callback(inputs);
|
||||
yield* nextOperation(operation.operations, inputs);
|
||||
}
|
||||
|
||||
async function * oneToN(input, operation, callback) {
|
||||
|
||||
Reference in New Issue
Block a user