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-17 16:23:57 +03:00
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
// Page Operations //
|
|
|
|
/////////////////////
|
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(),
|
2023-11-17 16:23:57 +03:00
|
|
|
pageIndexes: Joi.string().required(),
|
2023-11-16 02:24:10 +03:00
|
|
|
}).required());
|
2023-11-14 03:26:42 +03:00
|
|
|
|
2023-11-17 16:23:57 +03:00
|
|
|
//organise/arrange
|
|
|
|
|
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-17 16:23:57 +03:00
|
|
|
//Remove Pages
|
|
|
|
//impose
|
|
|
|
//Adjust page size/scale
|
|
|
|
//Auto Split Pages
|
|
|
|
//Adjust Colours/Contrast
|
|
|
|
//Crop
|
|
|
|
//Extract Pages
|
|
|
|
//PDF to Single large Page
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
// Convert //
|
|
|
|
/////////////////////
|
|
|
|
//Image to PDF
|
|
|
|
//Convert file to PDF
|
|
|
|
//URL to PDF
|
|
|
|
//HTML to PDF
|
|
|
|
//Markdown to PDF
|
|
|
|
//PDF to Image
|
|
|
|
//PDF to Word
|
|
|
|
//PDF to Presentation
|
|
|
|
//PDF to Text/RTF
|
|
|
|
//PDF to HTML
|
|
|
|
//PDF to PDF/A
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
// Security //
|
|
|
|
/////////////////////
|
|
|
|
//Add Password
|
|
|
|
//Remove Password
|
|
|
|
//Change Permissions
|
|
|
|
//Add Watermark
|
|
|
|
//Sign with Certificate
|
|
|
|
//Sanitize
|
|
|
|
//Auto Redact
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
// Miscellaneous //
|
|
|
|
/////////////////////
|
|
|
|
//OCR
|
|
|
|
//Add image
|
|
|
|
//Compress
|
|
|
|
//Extract Images
|
|
|
|
|
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-17 16:23:57 +03:00
|
|
|
//Detect/Split Scanned photos
|
|
|
|
//Sign
|
|
|
|
//Flatten
|
|
|
|
//Repair
|
|
|
|
//Remove Blank Pages
|
|
|
|
//Compare/Diff
|
|
|
|
//Add Page Numbers
|
|
|
|
//Auto Rename
|
|
|
|
//Get info
|
|
|
|
//Show JS
|
|
|
|
|
2023-11-11 18:35:33 +03:00
|
|
|
export default router;
|