2024-01-04 20:17:54 -05:00
|
|
|
import express, { Request, Response } from "express";
|
2023-11-21 01:18:32 +01:00
|
|
|
const router = express.Router();
|
2024-01-04 20:17:54 -05:00
|
|
|
import multer from "multer";
|
2023-11-21 01:18:32 +01:00
|
|
|
const upload = multer();
|
2024-02-23 23:48:03 +01:00
|
|
|
import { getOperatorByName } from "@stirling-pdf/shared-operations/src/workflow/operatorAccessor";
|
2024-01-04 20:17:54 -05:00
|
|
|
import { Operator } from "@stirling-pdf/shared-operations/src/functions";
|
2023-11-21 01:18:32 +01:00
|
|
|
|
2024-01-04 20:17:54 -05:00
|
|
|
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
|
|
|
|
import { respondWithPdfFiles } from "../../utils/endpoint-utils";
|
|
|
|
import { Action } from "@stirling-pdf/shared-operations/declarations/Action";
|
|
|
|
import { JoiPDFFileSchema } from "@stirling-pdf/shared-operations/src/wrappers/PdfFileJoi";
|
2023-11-21 01:18:32 +01:00
|
|
|
|
2024-01-04 20:17:54 -05:00
|
|
|
router.post("/:func", upload.array("file"), async function(req: Request, res: Response) {
|
2024-02-23 23:48:03 +01:00
|
|
|
await handleEndpoint(req, res);
|
2023-11-21 01:18:32 +01:00
|
|
|
});
|
|
|
|
|
2024-01-04 20:17:54 -05:00
|
|
|
router.post("/:dir/:func", upload.array("file"), async function(req: Request, res: Response) {
|
2024-02-23 23:48:03 +01:00
|
|
|
await handleEndpoint(req, res);
|
2023-11-21 01:18:32 +01:00
|
|
|
});
|
|
|
|
|
2024-02-23 23:48:03 +01:00
|
|
|
async function handleEndpoint(req: Request, res: Response) {
|
2023-11-21 01:18:32 +01:00
|
|
|
if(!req.files || req.files.length == 0) {
|
2024-01-04 20:17:54 -05:00
|
|
|
res.status(400).json({error: "no input file(s) were provided"});
|
2023-11-21 01:18:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-21 22:52:05 +01:00
|
|
|
const validationResults = JoiPDFFileSchema.validate(req.files);
|
|
|
|
if(validationResults.error) {
|
|
|
|
res.status(400).json({error: "PDF validation failed", details: validationResults.error.message});
|
|
|
|
return;
|
2023-11-21 01:18:32 +01:00
|
|
|
}
|
2023-12-21 22:52:05 +01:00
|
|
|
const pdfFiles: PdfFile[] = validationResults.value;
|
2023-11-21 01:18:32 +01:00
|
|
|
|
2024-02-23 23:48:03 +01:00
|
|
|
const operator = await getOperatorByName(req.params.func);
|
|
|
|
|
2023-11-21 01:18:32 +01:00
|
|
|
if(operator) {
|
2023-12-21 16:42:00 +01:00
|
|
|
const action: Action = {type: req.params.func, values: req.body};
|
|
|
|
|
|
|
|
const validationResults = operator.schema.validate({input: pdfFiles, values: action.values});
|
|
|
|
|
|
|
|
if(validationResults.error) {
|
2023-12-21 23:04:16 +01:00
|
|
|
res.status(400).json({error: "Value validation failed", details: validationResults.error.message});
|
2023-11-21 01:18:32 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-12-21 16:42:00 +01:00
|
|
|
action.values = validationResults.value.values;
|
|
|
|
const operation = new operator(action);
|
|
|
|
|
|
|
|
operation.run(validationResults.value.input, (progress) => {}).then(pdfFiles => {
|
|
|
|
respondWithPdfFiles(res, pdfFiles, req.params.func + "_result");
|
2024-01-04 20:17:54 -05:00
|
|
|
});
|
2023-11-21 01:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2024-01-04 20:17:54 -05:00
|
|
|
res.status(400).json({error: `the operator of type ${req.params.func} does not exist`});
|
2023-11-21 01:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default router;
|