Merge branch 'stirling-pdf-rewrite' into version-2
This commit is contained in:
63
server-node/functions.js
Normal file
63
server-node/functions.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import PDFLib from 'pdf-lib';
|
||||
import PDFJS from "pdfjs-dist";
|
||||
import jsQR from "jsqr";
|
||||
|
||||
delete global.crypto; // TODO: I hate to do this, but the new node version forces me to, if anyone finds a better solution, please tell me!
|
||||
import * as pdfcpuWraopper from "../shared-operations/wasm/pdfcpu/pdfcpu-wrapper-node.js";
|
||||
import OpenCV from 'opencv-wasm';
|
||||
|
||||
import { extractPages as dependantExtractPages } from "../shared-operations/functions/extractPages.js";
|
||||
import { impose as dependantImpose } from '../shared-operations/functions/impose.js';
|
||||
import { mergePDFs as dependantMergePDFs } from '../shared-operations/functions/mergePDFs.js';
|
||||
import { rotatePages as dependantRotatePages } from '../shared-operations/functions/rotatePages.js';
|
||||
import { scaleContent as dependantScaleContent} from '../shared-operations/functions/scaleContent.js';
|
||||
import { scalePage as dependantScalePage } from '../shared-operations/functions/scalePage.js';
|
||||
import { splitPDF as dependantSplitPDF } from '../shared-operations/functions/splitPDF.js';
|
||||
import { editMetadata as dependantEditMetadata } from '../shared-operations/functions/editMetadata.js';
|
||||
import { organizePages as dependantOrganizePages } from '../shared-operations/functions/organizePages.js';
|
||||
import { removeBlankPages as dependantRemoveBlankPages} from '../shared-operations/functions/removeBlankPages.js';
|
||||
import { splitOn as dependantSplitOn } from "../shared-operations/functions/splitOn.js";
|
||||
|
||||
export async function extractPages(snapshot, pagesToExtractArray) {
|
||||
return dependantExtractPages(snapshot, pagesToExtractArray, PDFLib);
|
||||
}
|
||||
|
||||
export async function impose(snapshot, nup, format) {
|
||||
return dependantImpose(snapshot, nup, format, pdfcpuWraopper);
|
||||
}
|
||||
|
||||
export async function mergePDFs(snapshots) {
|
||||
return dependantMergePDFs(snapshots, PDFLib);
|
||||
}
|
||||
|
||||
export async function rotatePages(snapshot, rotation) {
|
||||
return dependantRotatePages(snapshot, rotation, PDFLib);
|
||||
}
|
||||
|
||||
export async function scaleContent(snapshot, scaleFactor) {
|
||||
return dependantScaleContent(snapshot, scaleFactor, PDFLib);
|
||||
}
|
||||
|
||||
export async function scalePage(snapshot, pageSize) {
|
||||
return dependantScalePage(snapshot, pageSize, PDFLib);
|
||||
}
|
||||
|
||||
export async function splitPDF(snapshot, splitAfterPageArray) {
|
||||
return dependantSplitPDF(snapshot, splitAfterPageArray, PDFLib);
|
||||
}
|
||||
|
||||
export async function editMetadata(snapshot, metadata) {
|
||||
return dependantEditMetadata(snapshot, metadata, PDFLib);
|
||||
}
|
||||
|
||||
export async function organizePages(snapshot, operation, customOrderString) {
|
||||
return dependantOrganizePages(snapshot, operation, customOrderString, PDFLib);
|
||||
}
|
||||
|
||||
export async function removeBlankPages(snapshot, whiteThreashold) {
|
||||
return dependantRemoveBlankPages(snapshot, whiteThreashold, PDFJS, OpenCV, PDFLib);
|
||||
}
|
||||
|
||||
export async function splitOn(snapshot, type, whiteThreashold) {
|
||||
return dependantSplitOn(snapshot, type, whiteThreashold, PDFJS, OpenCV, PDFLib, jsQR);
|
||||
}
|
||||
@@ -16,6 +16,15 @@ app.get('/', function (req, res, next) { // TODO: Use EJS?
|
||||
app.use("/api/operations", operations);
|
||||
//app.use("/api/workflow", workflow);
|
||||
|
||||
// server-node: backend api
|
||||
import api from './server-node/routes/api/index.js';
|
||||
app.use("/api/", api);
|
||||
|
||||
// client-vanilla: frontend
|
||||
app.use(express.static('./client-vanilla'));
|
||||
app.use(express.static('./shared-operations'));
|
||||
|
||||
// serve
|
||||
app.listen(PORT, function (err) {
|
||||
if (err) console.log(err);
|
||||
console.log(`http://localhost:${PORT}`);
|
||||
|
||||
@@ -110,7 +110,6 @@ export const splitOnly = {
|
||||
]
|
||||
}
|
||||
|
||||
// Split a document up into multiple documents
|
||||
export const rotateOnly = {
|
||||
outputOptions: {
|
||||
zip: false
|
||||
@@ -124,7 +123,6 @@ export const rotateOnly = {
|
||||
]
|
||||
}
|
||||
|
||||
// Split a document up into multiple documents
|
||||
export const imposeOnly = {
|
||||
outputOptions: {
|
||||
zip: false
|
||||
@@ -136,4 +134,32 @@ export const imposeOnly = {
|
||||
operations: []
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
export const removeBlankPagesOnly = {
|
||||
outputOptions: {
|
||||
zip: false
|
||||
},
|
||||
operations: [
|
||||
{
|
||||
type: "removeBlankPages",
|
||||
values: { "whiteThreashold": 10 },
|
||||
operations: []
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export const splitOnQR = {
|
||||
outputOptions: {
|
||||
zip: false
|
||||
},
|
||||
operations: [
|
||||
{
|
||||
type: "splitOn",
|
||||
values: {
|
||||
type: "QR_CODE"
|
||||
},
|
||||
operations: []
|
||||
}
|
||||
]
|
||||
}
|
||||
15
server-node/routes/api/index.js
Normal file
15
server-node/routes/api/index.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import express from 'express';
|
||||
import workflow from './workflow.js';
|
||||
import fileUpload from 'express-fileupload';
|
||||
|
||||
const router = express.Router();
|
||||
router.use(fileUpload());
|
||||
|
||||
router.get("/", function (req, res, next) {
|
||||
// TODO: Implement root api endpoint
|
||||
res.status(501).json({"Error": "Unfinished Endpoint. This sould probably send some api docs?"});
|
||||
});
|
||||
|
||||
router.use("/workflow", workflow);
|
||||
|
||||
export default router;
|
||||
@@ -5,8 +5,8 @@ import Archiver from 'archiver';
|
||||
import multer from 'multer'
|
||||
const upload = multer();
|
||||
|
||||
import * as Functions from "../../src/pdf-operations.js";
|
||||
import { traverseOperations } from "../../public/traverseOperations.js";
|
||||
import * as Functions from "../../functions.js";
|
||||
import { traverseOperations } from "../../../shared-operations/traverseOperations.js";
|
||||
|
||||
const activeWorkflows = {};
|
||||
|
||||
@@ -83,7 +83,7 @@ router.post("/:workflowUuid?", [
|
||||
}
|
||||
});
|
||||
|
||||
const traverse = traverseOperations(workflow.operations, inputs);
|
||||
const traverse = traverseOperations(workflow.operations, inputs, Functions);
|
||||
|
||||
let pdfResults;
|
||||
let iteration;
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
|
||||
export function organizeWaitOperations(operations) {
|
||||
|
||||
// Initialize an object to store the counts and associated "done" operations
|
||||
const waitCounts = {};
|
||||
const doneOperations = {};
|
||||
|
||||
// Function to count "type: wait" operations and associate "done" operations per id
|
||||
function countWaitOperationsAndDone(operations) {
|
||||
for (const operation of operations) {
|
||||
if (operation.type === "wait") {
|
||||
const id = operation.values.id;
|
||||
if (id in waitCounts) {
|
||||
waitCounts[id]++;
|
||||
} else {
|
||||
waitCounts[id] = 1;
|
||||
}
|
||||
}
|
||||
if (operation.type === "done") {
|
||||
const id = operation.values.id;
|
||||
doneOperations[id] = operation;
|
||||
}
|
||||
if (operation.operations) {
|
||||
countWaitOperationsAndDone(operation.operations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start counting and associating from the root operations
|
||||
countWaitOperationsAndDone(operations);
|
||||
|
||||
// Combine counts and associated "done" operations
|
||||
const result = {};
|
||||
for (const id in waitCounts) {
|
||||
result[id] = {
|
||||
waitCount: waitCounts[id],
|
||||
doneOperation: doneOperations[id],
|
||||
input: []
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,152 +0,0 @@
|
||||
import { organizeWaitOperations } from "./organizeWaitOperations.js";
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} operations
|
||||
* @param {*} input
|
||||
* @param {import('../src/pdf-operations.js')} Functions
|
||||
* @returns
|
||||
*/
|
||||
export async function * traverseOperations(operations, input, Functions) {
|
||||
const waitOperations = organizeWaitOperations(operations);
|
||||
let results = [];
|
||||
yield* nextOperation(operations, input);
|
||||
console.log("Done2");
|
||||
return results;
|
||||
|
||||
async function * nextOperation(operations, input) {
|
||||
if(Array.isArray(operations) && operations.length == 0) { // isEmpty
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < operations.length; i++) {
|
||||
yield* computeOperation(operations[i], structuredClone(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];
|
||||
|
||||
if(Array.isArray(input)) {
|
||||
waitOperation.input.concat(input); // TODO: May have unexpected concequences. Needs further testing!
|
||||
}
|
||||
else {
|
||||
waitOperation.input.push(input);
|
||||
}
|
||||
|
||||
waitOperation.waitCount--;
|
||||
if(waitOperation.waitCount == 0) {
|
||||
yield* nextOperation(waitOperation.doneOperation.operations, waitOperation.input);
|
||||
}
|
||||
break;
|
||||
case "extract":
|
||||
yield* nToN(input, operation, async (input) => {
|
||||
input.fileName += "_extractedPages";
|
||||
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 Functions.splitPDF(input.buffer, operation.values["pagesToSplitAfterArray"]);
|
||||
|
||||
const splits = [];
|
||||
for (let j = 0; j < splitResult.length; j++) {
|
||||
splits.push({
|
||||
originalFileName: input.originalFileName,
|
||||
fileName: input.fileName + "_split" + j,
|
||||
buffer: splitResult[j]
|
||||
})
|
||||
}
|
||||
|
||||
input = splits;
|
||||
});
|
||||
break;
|
||||
case "editMetadata":
|
||||
yield* nToN(input, operation, async (input) => {
|
||||
input.fileName += "_metadataEdited";
|
||||
input.buffer = await Functions.editMetadata(input.buffer, operation.values["metadata"]);
|
||||
});
|
||||
break;
|
||||
case "organizePages":
|
||||
yield* nToN(input, operation, async (input) => {
|
||||
input.fileName += "_pagesOrganized";
|
||||
input.buffer = await Functions.organizePages(input.buffer, operation.values["operation"], operation.values["customOrderString"]);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
throw new Error(`${operation.type} not implemented yet.`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
async function * nToOne(inputs, operation, callback) {
|
||||
if(!Array.isArray(inputs)) {
|
||||
inputs = [inputs];
|
||||
}
|
||||
|
||||
inputs = await callback(inputs);
|
||||
yield* nextOperation(operation.operations, inputs);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user