diff --git a/server-node/package.json b/server-node/package.json index 96718f0a..1c755037 100644 --- a/server-node/package.json +++ b/server-node/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "npx tsc", "start": "node dist/index.js", - "dev": "nodemon --watch './**/*.ts' --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm' ./index.ts" + "dev": "nodemon --watch './**/*.ts' --exec node --experimental-specifier-resolution=node --loader ts-node/esm ./src/index.ts" }, "keywords": [], "author": "", diff --git a/server-node/src/declarations/shared-operations.d.ts b/server-node/src/declarations/shared-operations.d.ts deleted file mode 100644 index d4a783c1..00000000 --- a/server-node/src/declarations/shared-operations.d.ts +++ /dev/null @@ -1,13 +0,0 @@ - -declare module '@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-node.js' { - export async function oneToOne(wasmArray: any, snapshot: any): Promise; -} - -declare module '@stirling-pdf/shared-operations/workflow/traverseOperations.js' { - export type PDF = { - originalFileName: string; - fileName: string; - buffer: Uint8Array; - } - export async function * traverseOperations(operations: any, input: PDF|PDF[], Operations: any); -} diff --git a/server-node/index.ts b/server-node/src/index.ts similarity index 80% rename from server-node/index.ts rename to server-node/src/index.ts index a69d646b..d16d7520 100644 --- a/server-node/index.ts +++ b/server-node/src/index.ts @@ -3,7 +3,7 @@ const app = express(); const PORT = 8000; // server-node: backend api -import api from './src/routes/api/api-controller'; +import api from './routes/api/api-controller'; app.use("/api/", api); // serve diff --git a/server-node/src/routes/api/api-controller.ts b/server-node/src/routes/api/api-controller.ts index d119b530..f832e235 100644 --- a/server-node/src/routes/api/api-controller.ts +++ b/server-node/src/routes/api/api-controller.ts @@ -1,6 +1,6 @@ import express, { Request, Response } from 'express'; -//import workflow from './workflow-controller'; +import workflow from './workflow-controller'; import operations from './operations-controller'; import conversions from './conversions-controller'; @@ -13,6 +13,6 @@ router.get("/", (req: Request, res: Response) => { router.use("/operations", operations); router.use("/conversions", conversions); -//router.use("/workflow", workflow); +router.use("/workflow", workflow); export default router; \ No newline at end of file diff --git a/server-node/src/routes/api/operations-controller.ts b/server-node/src/routes/api/operations-controller.ts index 2b3e580d..598b45dd 100644 --- a/server-node/src/routes/api/operations-controller.ts +++ b/server-node/src/routes/api/operations-controller.ts @@ -1,7 +1,7 @@ import Operations from '../../utils/pdf-operations'; import { respondWithPdfFile, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils'; -import { PdfFile, fromMulterFile } from '@stirling-pdf/shared-operations/wrappers/PdfFile' +import { PdfFile, fromMulterFile } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile' import express, { Request, Response } from 'express'; const router = express.Router(); diff --git a/server-node/src/routes/api/workflow-controller.ts b/server-node/src/routes/api/workflow-controller.ts index f29f5dba..810455f1 100644 --- a/server-node/src/routes/api/workflow-controller.ts +++ b/server-node/src/routes/api/workflow-controller.ts @@ -5,100 +5,101 @@ import Archiver from 'archiver'; import multer from 'multer' const upload = multer(); -//import Operations from "../../utils/pdf-operations"; -//import { traverseOperations } from "@stirling-pdf/shared-operations/workflow/traverseOperations.js"; +import Operations from "../../utils/pdf-operations"; +import { traverseOperations } from "@stirling-pdf/shared-operations/src/workflow/traverseOperations"; const activeWorkflows: any = {}; const router = express.Router(); -/* + router.post("/:workflowUuid?", [ - upload.any(), + upload.array("files"), async (req: Request, res: Response) => { - if(req.files == null) { + // TODO: Maybe replace with another validator + if(req.files?.length == 0) { res.status(400).json({"error": "No files were uploaded."}); return; } - - const filesArr = Array.isArray(req.files.files) ? req.files.files : [req.files.files]; - + + // TODO: Validate input further (json may be invalid or not be in workflow format) const workflow = JSON.parse(req.body.workflow); - // TODO: Validate input further (json may fail or not be a valid workflow) - const inputs = await Promise.all(filesArr.map(async file => { + const inputs = await Promise.all((req.files as Express.Multer.File[]).map(async file => { + console.log(file); return { - originalFileName: file.name.replace(/\.[^/.]+$/, ""), - fileName: file.name.replace(/\.[^/.]+$/, ""), - buffer: new Uint8Array(await file.data) + originalFileName: file.originalname.replace(/\.[^/.]+$/, ""), + fileName: file.originalname.replace(/\.[^/.]+$/, ""), + buffer: new Uint8Array(await file.buffer) } })); - // Allow option to do it synchronously and just make a long request - if(req.body.async === "false") { - console.log("Don't do async"); + // TODO: Enable if traverse & organize migration is done. + // // Allow option to do it synchronously and just make a long request + // if(req.body.async === "false") { + // console.log("Don't do async"); - const traverse = traverseOperations(workflow.operations, inputs, Operations); + // const traverse = traverseOperations(workflow.operations, inputs, Operations); - let pdfResults; - let iteration; - while (true) { - iteration = await traverse.next(); - if (iteration.done) { - pdfResults = iteration.value; - console.log("Done"); - break; - } - console.log(iteration.value); - } + // let pdfResults; + // let iteration; + // while (true) { + // iteration = await traverse.next(); + // if (iteration.done) { + // pdfResults = iteration.value; + // console.log("Done"); + // break; + // } + // console.log(iteration.value); + // } - console.log("Download"); - downloadHandler(res, pdfResults); - } - else { - console.log("Start Aync Workflow"); - // TODO: UUID collision checks - let workflowID = req.params.workflowUuid - if(!workflowID) - workflowID = generateWorkflowID(); + // console.log("Download"); + // downloadHandler(res, pdfResults); + // } + // else { + // console.log("Start Aync Workflow"); + // // TODO: UUID collision checks + // let workflowID = req.params.workflowUuid + // if(!workflowID) + // workflowID = generateWorkflowID(); - activeWorkflows[workflowID] = { - createdAt: Date.now(), - finished: false, - eventStream: null, - result: null, - // TODO: When auth is implemented: owner - } - const activeWorkflow = activeWorkflows[workflowID]; + // activeWorkflows[workflowID] = { + // createdAt: Date.now(), + // finished: false, + // eventStream: null, + // result: null, + // // TODO: When auth is implemented: owner + // } + // const activeWorkflow = activeWorkflows[workflowID]; - res.status(200).json({ - "workflowID": workflowID, - "data-recieved": { - "fileCount": filesArr.length, - "workflow": workflow - } - }); + // res.status(200).json({ + // "workflowID": workflowID, + // "data-recieved": { + // "fileCount": filesArr.length, + // "workflow": workflow + // } + // }); - const traverse = traverseOperations(workflow.operations, inputs, Operations); + // const traverse = traverseOperations(workflow.operations, inputs, Operations); - let pdfResults; - let iteration; - while (true) { - iteration = await traverse.next(); - if (iteration.done) { - pdfResults = iteration.value; - if(activeWorkflow.eventStream) { - activeWorkflow.eventStream.write(`data: processing done\n\n`); - activeWorkflow.eventStream.end(); - } - break; - } - if(activeWorkflow.eventStream) - activeWorkflow.eventStream.write(`data: ${iteration.value}\n\n`); - } + // let pdfResults; + // let iteration; + // while (true) { + // iteration = await traverse.next(); + // if (iteration.done) { + // pdfResults = iteration.value; + // if(activeWorkflow.eventStream) { + // activeWorkflow.eventStream.write(`data: processing done\n\n`); + // activeWorkflow.eventStream.end(); + // } + // break; + // } + // if(activeWorkflow.eventStream) + // activeWorkflow.eventStream.write(`data: ${iteration.value}\n\n`); + // } - activeWorkflow.result = pdfResults; - activeWorkflow.finished = true; - } + // activeWorkflow.result = pdfResults; + // activeWorkflow.finished = true; + // } } ]); @@ -159,7 +160,7 @@ router.get("/result/:workflowUuid", (req: Request, res: Response) => { * If workflow isn't done return error * Send file, TODO: if there are multiple outputs return as zip * If download is done, delete results / allow deletion within the next 5-60 mins - * + */ const workflow = activeWorkflows[req.params.workflowUuid]; if(!workflow.finished) { res.status(202).json({ message: "Workflow hasn't finished yet. Check progress or connect to progress-steam to get notified when its done." }); @@ -227,5 +228,5 @@ function downloadHandler(res: Response, pdfResults: any) { readStream.pipe(res); } } -*/ + export default router; \ No newline at end of file diff --git a/server-node/src/utils/endpoint-utils.ts b/server-node/src/utils/endpoint-utils.ts index 53ac17ab..fce4cffc 100644 --- a/server-node/src/utils/endpoint-utils.ts +++ b/server-node/src/utils/endpoint-utils.ts @@ -1,6 +1,6 @@ import { Response } from 'express'; -import { PdfFile } from '@stirling-pdf/shared-operations/wrappers/PdfFile' +import { PdfFile } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile' export async function respondWithFile(res: Response, bytes: Uint8Array, name: string, mimeType: string): Promise { res.writeHead(200, { diff --git a/server-node/src/utils/pdf-operations.ts b/server-node/src/utils/pdf-operations.ts index 28831b8e..79341dd1 100644 --- a/server-node/src/utils/pdf-operations.ts +++ b/server-node/src/utils/pdf-operations.ts @@ -1,5 +1,5 @@ -import SharedOperations from "@stirling-pdf/shared-operations"; +import SharedOperations from "@stirling-pdf/shared-operations/src"; // Import injected libraries here! //import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-node.js"; diff --git a/shared-operations/declarations/Operation.d.ts b/shared-operations/declarations/Operation.d.ts new file mode 100644 index 00000000..2af654b7 --- /dev/null +++ b/shared-operations/declarations/Operation.d.ts @@ -0,0 +1,5 @@ +export interface Operation { + values: {id:any}; + type: string; + operations?: Operation[]; +} \ No newline at end of file diff --git a/shared-operations/declarations/PDF.d.ts b/shared-operations/declarations/PDF.d.ts new file mode 100644 index 00000000..0e6b1da0 --- /dev/null +++ b/shared-operations/declarations/PDF.d.ts @@ -0,0 +1,5 @@ +export interface PDF { + originalFileName: string; + fileName: string; + buffer: Uint8Array; +} \ No newline at end of file diff --git a/shared-operations/functions/common/detectEmptyPages.ts b/shared-operations/src/functions/common/detectEmptyPages.ts similarity index 100% rename from shared-operations/functions/common/detectEmptyPages.ts rename to shared-operations/src/functions/common/detectEmptyPages.ts diff --git a/shared-operations/functions/common/getImagesOnPage.ts b/shared-operations/src/functions/common/getImagesOnPage.ts similarity index 100% rename from shared-operations/functions/common/getImagesOnPage.ts rename to shared-operations/src/functions/common/getImagesOnPage.ts diff --git a/shared-operations/functions/common/pdf-utils.ts b/shared-operations/src/functions/common/pdf-utils.ts similarity index 100% rename from shared-operations/functions/common/pdf-utils.ts rename to shared-operations/src/functions/common/pdf-utils.ts diff --git a/shared-operations/functions/impose.ts b/shared-operations/src/functions/impose.ts similarity index 100% rename from shared-operations/functions/impose.ts rename to shared-operations/src/functions/impose.ts diff --git a/shared-operations/functions/mergePDFs.ts b/shared-operations/src/functions/mergePDFs.ts similarity index 100% rename from shared-operations/functions/mergePDFs.ts rename to shared-operations/src/functions/mergePDFs.ts diff --git a/shared-operations/functions/rotatePages.ts b/shared-operations/src/functions/rotatePages.ts similarity index 100% rename from shared-operations/functions/rotatePages.ts rename to shared-operations/src/functions/rotatePages.ts diff --git a/shared-operations/functions/scaleContent.ts b/shared-operations/src/functions/scaleContent.ts similarity index 100% rename from shared-operations/functions/scaleContent.ts rename to shared-operations/src/functions/scaleContent.ts diff --git a/shared-operations/functions/scalePage.ts b/shared-operations/src/functions/scalePage.ts similarity index 100% rename from shared-operations/functions/scalePage.ts rename to shared-operations/src/functions/scalePage.ts diff --git a/shared-operations/functions/splitOn.ts b/shared-operations/src/functions/splitOn.ts similarity index 100% rename from shared-operations/functions/splitOn.ts rename to shared-operations/src/functions/splitOn.ts diff --git a/shared-operations/functions/splitPDF.ts b/shared-operations/src/functions/splitPDF.ts similarity index 100% rename from shared-operations/functions/splitPDF.ts rename to shared-operations/src/functions/splitPDF.ts diff --git a/shared-operations/functions/subDocumentFunctions.ts b/shared-operations/src/functions/subDocumentFunctions.ts similarity index 100% rename from shared-operations/functions/subDocumentFunctions.ts rename to shared-operations/src/functions/subDocumentFunctions.ts diff --git a/shared-operations/functions/updateMetadata.ts b/shared-operations/src/functions/updateMetadata.ts similarity index 100% rename from shared-operations/functions/updateMetadata.ts rename to shared-operations/src/functions/updateMetadata.ts diff --git a/shared-operations/index.ts b/shared-operations/src/index.ts similarity index 100% rename from shared-operations/index.ts rename to shared-operations/src/index.ts diff --git a/shared-operations/wasm/browserfs.min.js b/shared-operations/src/wasm/browserfs.min.js similarity index 100% rename from shared-operations/wasm/browserfs.min.js rename to shared-operations/src/wasm/browserfs.min.js diff --git a/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-browser.js b/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-browser.js similarity index 100% rename from shared-operations/wasm/pdfcpu/pdfcpu-wrapper-browser.js rename to shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-browser.js diff --git a/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-node.js b/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js similarity index 100% rename from shared-operations/wasm/pdfcpu/pdfcpu-wrapper-node.js rename to shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js diff --git a/shared-operations/wasm/pdfcpu/pdfcpu.wasm b/shared-operations/src/wasm/pdfcpu/pdfcpu.wasm similarity index 100% rename from shared-operations/wasm/pdfcpu/pdfcpu.wasm rename to shared-operations/src/wasm/pdfcpu/pdfcpu.wasm diff --git a/shared-operations/wasm/pdfcpu/wasm_exec.js b/shared-operations/src/wasm/pdfcpu/wasm_exec.js similarity index 100% rename from shared-operations/wasm/pdfcpu/wasm_exec.js rename to shared-operations/src/wasm/pdfcpu/wasm_exec.js diff --git a/shared-operations/workflow/organizeWaitOperations.ts b/shared-operations/src/workflow/organizeWaitOperations.ts similarity index 93% rename from shared-operations/workflow/organizeWaitOperations.ts rename to shared-operations/src/workflow/organizeWaitOperations.ts index 7d128818..d0e9756f 100644 --- a/shared-operations/workflow/organizeWaitOperations.ts +++ b/shared-operations/src/workflow/organizeWaitOperations.ts @@ -1,9 +1,4 @@ - -export interface Operation { - values: {id:any}; - type: string; - operations?: Operation[]; -} +import { Operation } from "../../declarations/Operation"; export function organizeWaitOperations(operations: Operation[]) { diff --git a/shared-operations/workflow/traverseOperations.js b/shared-operations/src/workflow/traverseOperations.ts similarity index 90% rename from shared-operations/workflow/traverseOperations.js rename to shared-operations/src/workflow/traverseOperations.ts index 78402818..8f25f390 100644 --- a/shared-operations/workflow/traverseOperations.js +++ b/shared-operations/src/workflow/traverseOperations.ts @@ -1,34 +1,17 @@ import { organizeWaitOperations } from "./organizeWaitOperations.js"; +import { Operation } from "../../declarations/Operation.js"; +import { PDF } from "../../declarations/PDF.js"; -/** - * @typedef PDF - * @property {string} originalFileName - * @property {string} fileName - * @property {Uint8Array} buffer - */ - -/** - * - * @param {JSON} operations - * @param {PDF|PDF[]} input - * @returns {} - */ -export async function * traverseOperations(operations, input, Operations) { +export async function * traverseOperations(operations: Operation[], input: PDF[] | PDF, Operations: AllOperations) { const waitOperations = organizeWaitOperations(operations); - /** @type {PDF[]} */ let results = []; + let results: PDF[] = []; yield* nextOperation(operations, input); return results; - /** - * - * @param {JSON} operations - * @param {PDF|PDF[]} input - * @returns {undefined} - */ - async function * nextOperation(operations, input) { + async function * nextOperation(operations: Operation[], input: PDF[] | PDF) { if(Array.isArray(operations) && operations.length == 0) { // isEmpty if(Array.isArray(input)) { - console.log("operation done: " + input[0].fileName + input.length > 1 ? "+" : ""); + console.log("operation done: " + input[0].fileName + (input.length > 1 ? "+" : "")); results = results.concat(input); return; } @@ -44,13 +27,7 @@ export async function * traverseOperations(operations, input, Operations) { } } - /** - * - * @param {JSON} operation - * @param {PDF|PDF[]} input - * @returns {undefined} - */ - async function * computeOperation(operation, input) { + async function * computeOperation(operation: Operation, input: PDF|PDF[]) { yield "Starting: " + operation.type; switch (operation.type) { case "done": // Skip this, because it is a valid node. diff --git a/shared-operations/wrappers/PdfFile.ts b/shared-operations/src/wrappers/PdfFile.ts similarity index 100% rename from shared-operations/wrappers/PdfFile.ts rename to shared-operations/src/wrappers/PdfFile.ts