2023-11-11 18:35:33 +03:00
|
|
|
|
|
|
|
import Operations from '../../utils/pdf-operations';
|
2023-11-12 16:57:53 +03:00
|
|
|
import { respondWithPdfFile, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils';
|
2023-11-14 03:26:42 +03:00
|
|
|
import { PdfFile, PdfFileSchema, fromMulterFile, fromMulterFiles } 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-14 03:26:42 +03:00
|
|
|
import Joi, { array } 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) {
|
|
|
|
body.file = fromMulterFile(req.file);
|
|
|
|
}
|
|
|
|
if (req.files) {
|
|
|
|
if (Array.isArray(req.files))
|
|
|
|
body.files = fromMulterFiles(req.files);
|
|
|
|
else {
|
|
|
|
const flattenedFiles = Object.values(req.files).flatMap(va => va);
|
|
|
|
body.files = fromMulterFiles(flattenedFiles);
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
if (Array.isArray(processed)) {
|
|
|
|
// TODO zip multiple files
|
|
|
|
} else {
|
|
|
|
processed.filename = appendToFilename(processed.filename, nameToAppend);
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* appends a string before the last '.' of the given filename
|
|
|
|
*/
|
|
|
|
function appendToFilename(filename: string, str: string) {
|
|
|
|
return filename.replace(/(\.[^.]+)$/, str+'$1')
|
|
|
|
}
|
|
|
|
|
2023-11-14 03:26:42 +03:00
|
|
|
registerEndpoint("/merge-pdfs", "_merged", upload.single("file"), Operations.mergePDFs, Joi.object({
|
|
|
|
files: Joi.array().items(PdfFileSchema).required(),
|
|
|
|
}).required())
|
|
|
|
|
|
|
|
registerEndpoint("/rotate-pdf", "_rotated", upload.single("file"), Operations.rotatePages, Joi.object({
|
|
|
|
file: PdfFileSchema.required(),
|
|
|
|
rotation: Joi.alternatives().try(Joi.number(), Joi.array().items(Joi.number())).required(),
|
|
|
|
}).required())
|
|
|
|
|
|
|
|
registerEndpoint("/update-metadata", "_edited-metadata", upload.single("file"), Operations.updateMetadata, Joi.object({
|
|
|
|
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()),
|
|
|
|
}).required())
|
|
|
|
|
2023-11-11 18:35:33 +03:00
|
|
|
export default router;
|