Merge branch 'version-2' of https://github.com/Frooodle/Stirling-PDF into version-2

This commit is contained in:
Saud Fatayerji 2023-11-13 02:52:01 +03:00
commit eab9607f8a
31 changed files with 99 additions and 129 deletions

View File

@ -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": "",

View File

@ -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<Uint8Array>;
}
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);
}

View File

@ -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

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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<void> {
res.writeHead(200, {

View File

@ -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";

View File

@ -0,0 +1,5 @@
export interface Operation {
values: {id:any};
type: string;
operations?: Operation[];
}

View File

@ -0,0 +1,5 @@
export interface PDF {
originalFileName: string;
fileName: string;
buffer: Uint8Array;
}

View File

@ -1,9 +1,4 @@
export interface Operation {
values: {id:any};
type: string;
operations?: Operation[];
}
import { Operation } from "../../declarations/Operation";
export function organizeWaitOperations(operations: Operation[]) {

View File

@ -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.