Renamed editMetadata to match V1 naming. Added Operations controller

This commit is contained in:
Saud Fatayerji
2023-11-11 18:35:33 +03:00
parent 2cb2116940
commit fd6b0740fd
8 changed files with 341 additions and 29 deletions

View File

@@ -17,6 +17,7 @@
"archiver": "^6.0.1",
"express": "^4.18.2",
"express-fileupload": "^1.4.2",
"joi": "^17.11.0",
"jsqr": "^1.4.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1",

View File

@@ -1,16 +1,16 @@
import express, { Request, Response } from 'express';
import fileUpload from 'express-fileupload';
import workflow from './workflow-controller';
import operations from './operations-controller';
const router = express.Router();
router.use(fileUpload());
router.get("/", (req: Request, res: Response) => {
// TODO: Implement root api endpoint
res.status(501).json({"Error": "Unfinished Endpoint. This sould probably send some api docs?"});
});
router.use("/operations", operations);
router.use("/workflow", workflow);
export default router;

View File

@@ -0,0 +1,66 @@
import Operations from '../../utils/pdf-operations';
import { respondWithBinaryPdf, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils';
import express, { Request, Response } from 'express';
const router = express.Router();
import multer from 'multer';
const upload = multer();
import Joi from 'joi';
router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request, res: Response) {
const schema = Joi.object({
angle: Joi.number().required()
});
const { error, value } = schema.validate(req.body);
if (error) {
res.status(400).send(error.details);
return;
}
if (!req.file) {
response_mustHaveExactlyOneFile(res);
return;
}
const rotated = await Operations.rotatePages(req.file.buffer, value.angle)
const newFilename = appendToFilename(req.file.originalname, '_rotated');
respondWithBinaryPdf(res, rotated, newFilename);
});
router.post('/update-metadata', upload.single("pdfFile"), async function(req: Request, res: Response) {
const schema = Joi.object({
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()),
});
const { error, value } = schema.validate(req.body);
if (error) {
res.status(400).send(error.details);
return;
}
if (!req.file) {
response_mustHaveExactlyOneFile(res);
return;
}
const processed = await Operations.updateMetadata(req.file.buffer, value.angle)
const newFilename = appendToFilename(req.file.originalname, '_edited-metadata');
respondWithBinaryPdf(res, processed, newFilename);
});
/**
* appends a string before the last '.' of the given filename
*/
function appendToFilename(filename: string, str: string) {
return filename.replace(/(\.[^.]+)$/, str+'$1')
}
export default router;

View File

@@ -0,0 +1,27 @@
import express, { Request, Response } from 'express';
export function respondWithBinaryPdf(res: Response, buffer: Uint8Array, filename: string) {
res.writeHead(200, {
'Content-Type': "application/pdf",
'Content-disposition': 'attachment;filename=' + filename,
'Content-Length': buffer.length
});
res.end(buffer)
}
export function response_mustHaveExactlyOneFile(res: Response): void {
res.status(400).send([
{
"message": "file is required",
"path": [
"pdfFile"
],
"type": "file",
"context": {
"label": "pdfFile",
"key": "pdfFile"
}
}
]);
}