2023-11-11 18:35:33 +03:00
|
|
|
|
|
|
|
import Operations from '../../utils/pdf-operations';
|
2023-11-16 02:24:10 +03:00
|
|
|
import { respondWithPdfFile, respondWithPdfFiles, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils';
|
2023-11-14 20:22:37 +01:00
|
|
|
import { PdfFile, PdfFileSchema } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile'
|
2023-11-11 18:35:33 +03:00
|
|
|
|
2023-11-14 03:26:42 +03:00
|
|
|
import express, { Request, Response, RequestHandler } from 'express';
|
2023-11-11 18:35:33 +03:00
|
|
|
const router = express.Router();
|
|
|
|
import multer from 'multer';
|
|
|
|
const upload = multer();
|
2023-11-16 02:24:10 +03:00
|
|
|
import Joi from 'joi';
|
2023-11-11 18:35:33 +03:00
|
|
|
|
2023-11-14 03:26:42 +03:00
|
|
|
function registerEndpoint(endpoint: string,
|
|
|
|
nameToAppend: string,
|
|
|
|
fileHandler: RequestHandler,
|
|
|
|
operationFunction: (params: any) => Promise<PdfFile|PdfFile[]>,
|
|
|
|
joiSchema: Joi.ObjectSchema<any>
|
|
|
|
): void {
|
|
|
|
router.post(endpoint, fileHandler, async function(req: Request, res: Response) {
|
|
|
|
const body = req.body;
|
|
|
|
if (req.file) {
|
2023-11-14 20:22:37 +01:00
|
|
|
body.file = PdfFile.fromMulterFile(req.file);
|
2023-11-14 03:26:42 +03:00
|
|
|
}
|
|
|
|
if (req.files) {
|
|
|
|
if (Array.isArray(req.files))
|
2023-11-14 20:22:37 +01:00
|
|
|
body.files = PdfFile.fromMulterFiles(req.files);
|
2023-11-14 03:26:42 +03:00
|
|
|
else {
|
|
|
|
const flattenedFiles = Object.values(req.files).flatMap(va => va);
|
2023-11-14 20:22:37 +01:00
|
|
|
body.files = PdfFile.fromMulterFiles(flattenedFiles);
|
2023-11-14 03:26:42 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 16:57:53 +03:00
|
|
|
|
2023-11-14 03:26:42 +03:00
|
|
|
console.log(req.body)
|
|
|
|
const { error, value } = joiSchema.validate(req.body);
|
|
|
|
if (error) {
|
|
|
|
res.status(400).send(error.details);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const processed = await operationFunction(value)
|
2023-11-16 02:24:10 +03:00
|
|
|
|
|
|
|
if (body.files && Array.isArray(processed)) { // MIMO
|
|
|
|
respondWithPdfFiles(res, processed, nameToAppend);
|
|
|
|
} else if (body.file && Array.isArray(processed)) { // SIMO
|
|
|
|
respondWithPdfFiles(res, processed, body.file.filename + nameToAppend);
|
|
|
|
} else if (body.files && !Array.isArray(processed)) { // MISO
|
|
|
|
respondWithPdfFile(res, processed);
|
|
|
|
} else if (body.file && !Array.isArray(processed)) { // SISO
|
2023-11-14 03:26:42 +03:00
|
|
|
respondWithPdfFile(res, processed);
|
|
|
|
}
|
2023-11-11 18:35:33 +03:00
|
|
|
});
|
2023-11-14 03:26:42 +03:00
|
|
|
}
|
2023-11-11 18:35:33 +03:00
|
|
|
|
2023-11-16 02:24:10 +03:00
|
|
|
registerEndpoint("/merge-pdfs", "", upload.any(), Operations.mergePDFs, Joi.object({
|
2023-11-14 03:26:42 +03:00
|
|
|
files: Joi.array().items(PdfFileSchema).required(),
|
2023-11-16 02:24:10 +03:00
|
|
|
}).required());
|
|
|
|
|
2023-11-17 16:11:06 +03:00
|
|
|
registerEndpoint("/split-pdf", "_split", upload.single("file"), Operations.splitPdfByIndex, Joi.object({
|
2023-11-16 02:24:10 +03:00
|
|
|
file: PdfFileSchema.required(),
|
|
|
|
pageNumbers: Joi.string().required(),
|
|
|
|
}).required());
|
2023-11-14 03:26:42 +03:00
|
|
|
|
2023-11-16 02:24:10 +03:00
|
|
|
registerEndpoint("/rotate-pdf", "", upload.single("file"), Operations.rotatePages, Joi.object({
|
2023-11-14 03:26:42 +03:00
|
|
|
file: PdfFileSchema.required(),
|
|
|
|
rotation: Joi.alternatives().try(Joi.number(), Joi.array().items(Joi.number())).required(),
|
2023-11-16 02:24:10 +03:00
|
|
|
}).required());
|
2023-11-14 03:26:42 +03:00
|
|
|
|
2023-11-16 02:24:10 +03:00
|
|
|
registerEndpoint("/update-metadata", "", upload.single("file"), Operations.updateMetadata, Joi.object({
|
2023-11-14 03:26:42 +03:00
|
|
|
file: PdfFileSchema.required(),
|
|
|
|
deleteAll: Joi.string(),
|
|
|
|
author: Joi.string(),
|
|
|
|
creationDate: Joi.string(),
|
|
|
|
creator: Joi.string(),
|
|
|
|
keywords: Joi.string(),
|
|
|
|
modificationDate: Joi.string(),
|
|
|
|
producer: Joi.string(),
|
|
|
|
subject: Joi.string(),
|
|
|
|
title: Joi.string(),
|
|
|
|
trapped: Joi.string(),
|
|
|
|
allRequestParams: Joi.object().pattern(Joi.string(), Joi.string()),
|
2023-11-16 02:24:10 +03:00
|
|
|
}).required());
|
2023-11-14 03:26:42 +03:00
|
|
|
|
2023-11-11 18:35:33 +03:00
|
|
|
export default router;
|