mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-05-01 01:16:43 +02:00
dramatically Improved traversing code quality
This commit is contained in:
parent
f402a36042
commit
6248aa6a32
@ -78,19 +78,21 @@ export async function oneToOne(wasmArray, snapshot) {
|
|||||||
|
|
||||||
await checkExistsWithTimeout("/output.pdf", 1000);
|
await checkExistsWithTimeout("/output.pdf", 1000);
|
||||||
console.log("Write started...");
|
console.log("Write started...");
|
||||||
|
|
||||||
|
// TODO: [Important] This fails for large PDFs. Need to a way to check if file write is definitely done.
|
||||||
// We need to wait for the file write in memfs to finish in node for some reason
|
// We need to wait for the file write in memfs to finish in node for some reason
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
resolve();
|
resolve();
|
||||||
}, 100);
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
console.log("Could be done?");
|
||||||
|
|
||||||
fs.unlinkSync("input.pdf");
|
fs.unlinkSync("input.pdf");
|
||||||
|
|
||||||
const data = fs.readFileSync("/output.pdf");
|
const data = fs.readFileSync("/output.pdf");
|
||||||
if(data.length == 0) {
|
if(data.length == 0) {
|
||||||
throw Error("File Size 0 that should not happen");
|
throw Error("File Size 0 that should not happen. The write probably didn't finish in time.");
|
||||||
}
|
}
|
||||||
fs.unlinkSync("output.pdf");
|
fs.unlinkSync("output.pdf");
|
||||||
console.log("Your File ist Ready!");
|
console.log("Your File ist Ready!");
|
||||||
@ -98,15 +100,15 @@ export async function oneToOne(wasmArray, snapshot) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function manyToOne() {
|
export async function manyToOne() {
|
||||||
//TODO: Do this of neccesary for some operations
|
//TODO: Do this if necessary for some pdfcpu operations
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function oneToMany() {
|
export async function oneToMany() {
|
||||||
//TODO: Do this of neccesary for some operations
|
//TODO: Do this if necessary for some pdfcpu operations
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function manyToMany() {
|
export async function manyToMany() {
|
||||||
//TODO: Do this of neccesary for some operations
|
//TODO: Do this if necessary for some pdfcpu operations
|
||||||
}
|
}
|
||||||
|
|
||||||
// THX: https://stackoverflow.com/questions/26165725/nodejs-check-file-exists-if-not-wait-till-it-exist
|
// THX: https://stackoverflow.com/questions/26165725/nodejs-check-file-exists-if-not-wait-till-it-exist
|
||||||
|
@ -11,7 +11,6 @@ export async function * traverseOperations(operations, input) {
|
|||||||
yield* nextOperation(operations, input)
|
yield* nextOperation(operations, input)
|
||||||
return results;
|
return results;
|
||||||
|
|
||||||
// TODO: Pult all nextOperation() in the for await, like for "extract"
|
|
||||||
async function * nextOperation(operations, input) {
|
async function * nextOperation(operations, input) {
|
||||||
console.log(Array.isArray(operations) && operations.length == 0);
|
console.log(Array.isArray(operations) && operations.length == 0);
|
||||||
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
|
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
|
||||||
@ -35,81 +34,32 @@ export async function * traverseOperations(operations, input) {
|
|||||||
async function * computeOperation(operation, input) {
|
async function * computeOperation(operation, input) {
|
||||||
yield "Starting: " + operation.type;
|
yield "Starting: " + operation.type;
|
||||||
switch (operation.type) {
|
switch (operation.type) {
|
||||||
case "done":
|
|
||||||
console.log("Done operation will get called if all waits are done. Skipping for now.")
|
|
||||||
break;
|
|
||||||
case "wait":
|
case "wait":
|
||||||
const waitOperation = waitOperations[operation.values.id];
|
const waitOperation = waitOperations[operation.values.id];
|
||||||
|
|
||||||
if(Array.isArray(input)) {
|
if(Array.isArray(input)) {
|
||||||
// waitOperation.input.concat(input); // May have unexpected concequences. Better throw an error for now.
|
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
|
||||||
throw new Error("Wait recieved an array as input. I don't know if this can happen, but if it does happen, I will investigate. Please share your workflow (:");
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
waitOperation.input.push(input);
|
waitOperation.input.push(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for all elements of previous split to finish
|
|
||||||
if(input.splitCount && input.splitCount > 0) {
|
|
||||||
input.splitCount--;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
waitOperation.waitCount--;
|
waitOperation.waitCount--;
|
||||||
if(waitOperation.waitCount == 0) {
|
if(waitOperation.waitCount == 0) {
|
||||||
yield* nextOperation(waitOperation.doneOperation.operations, waitOperation.input);
|
yield* nextOperation(waitOperation.doneOperation.operations, waitOperation.input);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "removeObjects":
|
|
||||||
console.warn("RemoveObjects not implemented yet.")
|
|
||||||
|
|
||||||
if(Array.isArray(input)) {
|
|
||||||
for (let i = 0; i < input.length; i++) {
|
|
||||||
// TODO: modfiy input
|
|
||||||
input[i].fileName += "_removedObjects";
|
|
||||||
yield* nextOperation(operation.operations, input[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// TODO: modfiy input
|
|
||||||
input.fileName += "_removedObjects";
|
|
||||||
yield* nextOperation(operation.operations, input);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "extract":
|
case "extract":
|
||||||
if(Array.isArray(input)) {
|
yield * nToN(input, operation, async (input) => {
|
||||||
for (let i = 0; i < input.length; i++) {
|
|
||||||
input[i].fileName += "_extractedPages";
|
|
||||||
input[i].buffer = await extractPages(input[i].buffer, operation.values["pagesToExtractArray"]);
|
|
||||||
yield* nextOperation(operation.operations, input[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
input.fileName += "_extractedPages";
|
input.fileName += "_extractedPages";
|
||||||
input.buffer = await extractPages(input.buffer, operation.values["pagesToExtractArray"]);
|
input.buffer = await extractPages(input.buffer, operation.values["pagesToExtractArray"]);
|
||||||
yield* nextOperation(operation.operations, input);
|
});
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "split":
|
case "split":
|
||||||
// TODO: When a split goes into another split function and then into a wait function it might break the done condition, as it will count multiple times.
|
// TODO: A split might break the done condition, it may count multiple times. Needs further testing!
|
||||||
if(Array.isArray(input)) {
|
|
||||||
for (let i = 0; i < input.length; i++) {
|
|
||||||
const splitResult = await splitPDF(input[i].buffer, operation.values["pagesToSplitAfterArray"]);
|
|
||||||
|
|
||||||
const splits = [];
|
yield * oneToN(input, operation, async (input) => {
|
||||||
for (let j = 0; j < splitResult.length; j++) {
|
|
||||||
splits.push({
|
|
||||||
originalFileName: input[i].originalFileName,
|
|
||||||
fileName: input[i].fileName + "_split" + j,
|
|
||||||
buffer: splitResult[j],
|
|
||||||
splitCount: splitResult.length
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
yield* nextOperation(operation.operations, splits);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
const splitResult = await splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
|
const splitResult = await splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
|
||||||
|
|
||||||
const splits = [];
|
const splits = [];
|
||||||
@ -117,122 +67,73 @@ export async function * traverseOperations(operations, input) {
|
|||||||
splits.push({
|
splits.push({
|
||||||
originalFileName: input.originalFileName,
|
originalFileName: input.originalFileName,
|
||||||
fileName: input.fileName + "_split" + j,
|
fileName: input.fileName + "_split" + j,
|
||||||
buffer: splitResult[j],
|
buffer: splitResult[j]
|
||||||
splitCount: splitResult.length
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
yield* nextOperation(operation.operations, splits);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "fillField":
|
|
||||||
console.warn("FillField not implemented yet.")
|
|
||||||
|
|
||||||
if(Array.isArray(input)) {
|
input = splits;
|
||||||
for (let i = 0; i < input.length; i++) {
|
});
|
||||||
// TODO: modfiy input
|
|
||||||
input[i].fileName += "_filledField";
|
|
||||||
yield* nextOperation(operation.operations, input[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// TODO: modfiy input
|
|
||||||
input.fileName += "_filledField";
|
|
||||||
yield* nextOperation(operation.operations, input);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "extractImages":
|
|
||||||
console.warn("ExtractImages not implemented yet.")
|
|
||||||
|
|
||||||
if(Array.isArray(input)) {
|
|
||||||
for (let i = 0; i < input.length; i++) {
|
|
||||||
// TODO: modfiy input
|
|
||||||
input[i].fileName += "_extractedImages";
|
|
||||||
yield* nextOperation(operation.operations, input[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// TODO: modfiy input
|
|
||||||
input.fileName += "_extractedImages";
|
|
||||||
yield* nextOperation(operation.operations, input);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "merge":
|
case "merge":
|
||||||
if(Array.isArray(input) && input.length > 1) {
|
yield * nToOne(input, operation, async (input) => {
|
||||||
const inputs = input;
|
const inputs = input;
|
||||||
input = {
|
input = {
|
||||||
originalFileName: inputs.map(input => input.originalFileName).join("_and_"),
|
originalFileName: inputs.map(input => input.originalFileName).join("_and_"),
|
||||||
fileName: inputs.map(input => input.fileName).join("_and_") + "_merged",
|
fileName: inputs.map(input => input.fileName).join("_and_") + "_merged",
|
||||||
buffer: await mergePDFs(inputs.map(input => input.buffer))
|
buffer: await mergePDFs(inputs.map(input => input.buffer))
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
else {
|
|
||||||
// Only one input, no need to merge
|
|
||||||
input.fileName += "_merged";
|
|
||||||
}
|
|
||||||
yield* nextOperation(operation.operations, input);
|
|
||||||
break;
|
|
||||||
case "transform": {
|
|
||||||
console.warn("Transform not implemented yet.")
|
|
||||||
if(Array.isArray(input)) {
|
|
||||||
for (let i = 0; i < input.length; i++) {
|
|
||||||
// TODO: modfiy input
|
|
||||||
input[i].fileName += "_transformed";
|
|
||||||
yield* nextOperation(operation.operations, input[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// TODO: modfiy input
|
|
||||||
input.fileName += "_transformed";
|
|
||||||
yield* nextOperation(operation.operations, input);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
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"]);
|
|
||||||
yield* nextOperation(operation.operations, input[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
input.fileName += "_extractedPages";
|
|
||||||
input.buffer = await extractPages(input.buffer, operation.values["pagesToExtractArray"]);
|
|
||||||
yield* nextOperation(operation.operations, input);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "rotate":
|
case "rotate":
|
||||||
if(Array.isArray(input)) {
|
yield * nToN(input, operation, async (input) => {
|
||||||
for (let i = 0; i < input.length; i++) {
|
|
||||||
input[i].fileName += "_turned";
|
|
||||||
input[i].buffer = await rotatePages(input[i].buffer, operation.values["rotation"]);
|
|
||||||
yield* nextOperation(operation.operations, input[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
input.fileName += "_turned";
|
input.fileName += "_turned";
|
||||||
input.buffer = await rotatePages(input.buffer, operation.values["rotation"]);
|
input.buffer = await rotatePages(input.buffer, operation.values["rotation"]);
|
||||||
yield* nextOperation(operation.operations, input);
|
});
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "impose":
|
case "impose":
|
||||||
if(Array.isArray(input)) {
|
yield * nToN(input, operation, async (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"]);
|
|
||||||
yield* nextOperation(operation.operations, input[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
input.fileName += "_imposed";
|
input.fileName += "_imposed";
|
||||||
input.buffer = await impose(input.buffer, operation.values["nup"], operation.values["format"]);
|
input.buffer = await impose(input.buffer, operation.values["nup"], operation.values["format"]);
|
||||||
yield* nextOperation(operation.operations, input);
|
});
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("operation type unknown: ", operation.type);
|
throw new Error(`${operation.type} not implemented yet.`);
|
||||||
break;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user