Migrated more functions to use PdfFile
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
import Operations from '../../utils/pdf-operations';
|
||||
import { respondWithBinaryPdf, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils';
|
||||
import { respondWithPdfFile, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils';
|
||||
import { PdfFile, fromMulterFile } from '@stirling-pdf/shared-operations/wrappers/PdfFile'
|
||||
|
||||
import express, { Request, Response } from 'express';
|
||||
const router = express.Router();
|
||||
@@ -8,6 +9,36 @@ import multer from 'multer';
|
||||
const upload = multer();
|
||||
import Joi from 'joi';
|
||||
|
||||
router.post('/merge-pdfs', 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()),
|
||||
}).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 arrayFile = fromMulterFile(req.file);
|
||||
const processed = await Operations.updateMetadata(arrayFile, value)
|
||||
const newFilename = appendToFilename(req.file.originalname, '_edited-metadata');
|
||||
respondWithPdfFile(res, processed);
|
||||
});
|
||||
|
||||
router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request, res: Response) {
|
||||
const schema = Joi.object({
|
||||
angle: Joi.number().required()
|
||||
@@ -22,9 +53,10 @@ router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request
|
||||
return;
|
||||
}
|
||||
|
||||
const rotated = await Operations.rotatePages(req.file.buffer, value.angle)
|
||||
const newFilename = appendToFilename(req.file.originalname, '_rotated');
|
||||
respondWithBinaryPdf(res, rotated, newFilename);
|
||||
const arrayFile = fromMulterFile(req.file);
|
||||
const rotated = await Operations.rotatePages(arrayFile, value.angle)
|
||||
rotated.filename = appendToFilename(arrayFile.filename, '_rotated');
|
||||
respondWithPdfFile(res, rotated);
|
||||
});
|
||||
|
||||
router.post('/update-metadata', upload.single("pdfFile"), async function(req: Request, res: Response) {
|
||||
@@ -51,9 +83,10 @@ router.post('/update-metadata', upload.single("pdfFile"), async function(req: Re
|
||||
return;
|
||||
}
|
||||
|
||||
const processed = await Operations.updateMetadata(req.file.buffer, value)
|
||||
const newFilename = appendToFilename(req.file.originalname, '_edited-metadata');
|
||||
respondWithBinaryPdf(res, processed, newFilename);
|
||||
const arrayFile = fromMulterFile(req.file);
|
||||
const processed = await Operations.updateMetadata(arrayFile, value)
|
||||
processed.filename = appendToFilename(arrayFile.filename, '_edited-metadata');
|
||||
respondWithPdfFile(res, processed);
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
|
||||
import express, { Request, Response } from 'express';
|
||||
import { Response } from 'express';
|
||||
import { PdfFile } from '@stirling-pdf/shared-operations/wrappers/PdfFile'
|
||||
|
||||
export function respondWithBinaryPdf(res: Response, buffer: Uint8Array, filename: string) {
|
||||
export async function respondWithPdfFile(res: Response, file: PdfFile): Promise<void> {
|
||||
const byteFile = await file.convertToByteArrayFile();
|
||||
res.writeHead(200, {
|
||||
'Content-Type': "application/pdf",
|
||||
'Content-disposition': 'attachment;filename=' + filename,
|
||||
'Content-Length': buffer.length
|
||||
'Content-disposition': 'attachment;filename=' + byteFile.filename,
|
||||
'Content-Length': byteFile.byteArray?.length
|
||||
});
|
||||
res.end(buffer)
|
||||
res.end(byteFile.byteArray)
|
||||
}
|
||||
|
||||
export function response_mustHaveExactlyOneFile(res: Response): void {
|
||||
|
||||
Reference in New Issue
Block a user