PDF -> PNG Convertor
* DockerFile Addditions + Helper Functions
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { Request, Response, NextFunction, Router } from 'express';
|
||||
import { Magic, MAGIC_MIME_TYPE } from 'mmmagic';
|
||||
import { findFile, returnError } from '../Helpers/ApiHelper';
|
||||
import { convertFileToPNG } from '../Models/ConversionModel';
|
||||
|
||||
const magic = new Magic(MAGIC_MIME_TYPE);
|
||||
|
||||
const convertController = async (req: Request, res: Response, next: NextFunction) => {
|
||||
|
||||
// Check if any files were sent over.
|
||||
if ( !req.files || req.files?.length <= 0 ) {
|
||||
return returnError(res, {
|
||||
"message": "No files have been provided, this api endpoint requres files to be able to process them",
|
||||
"error": "Empty files array"
|
||||
})
|
||||
}
|
||||
|
||||
// Get our file
|
||||
const inputFile = findFile('inputFile', req.files as Express.Multer.File[]);
|
||||
|
||||
// Check if we have an input File
|
||||
if ( inputFile == null ) {
|
||||
return returnError(res, {
|
||||
"message": "Broken file name in request, please try again.",
|
||||
"error": "Files are in the call, but we require the name inputFile to be sent over."
|
||||
})
|
||||
}
|
||||
|
||||
let inputFileType: string | null = null;
|
||||
|
||||
// Get the input file format
|
||||
await new Promise<string | string[]>((_r, _e) => {
|
||||
magic.detectFile(inputFile.path, (err, fileType) => {
|
||||
if ( err ) {
|
||||
return _e(err);
|
||||
} else {
|
||||
return _r(fileType);
|
||||
}
|
||||
})
|
||||
})
|
||||
.then( ftype => {
|
||||
console.log(ftype);
|
||||
|
||||
if ( typeof ftype != 'object' ) {
|
||||
inputFileType = ftype;
|
||||
} else {
|
||||
inputFileType = ftype[0];
|
||||
}
|
||||
|
||||
})
|
||||
.catch( err => {
|
||||
console.error(err);
|
||||
})
|
||||
|
||||
if ( !!!inputFileType ) {
|
||||
return returnError(res, {
|
||||
message: "Could not determine a valid input file type",
|
||||
error: "Could not determine a valid input file type",
|
||||
})
|
||||
}
|
||||
|
||||
// Check if we have the target Format
|
||||
const targetFormat = req.body.targetFormat
|
||||
|
||||
// Predefine the result
|
||||
let result: string | null = null;
|
||||
|
||||
// Check for the input/output formats
|
||||
if ( targetFormat == 'png' && inputFileType == 'application/pdf' ) {
|
||||
await convertFileToPNG(inputFile)
|
||||
.then( data => {
|
||||
result = data.toString(`base64`);
|
||||
})
|
||||
.catch( err => {
|
||||
console.error(err);
|
||||
})
|
||||
}
|
||||
|
||||
res.status(200).send({
|
||||
message: `Conversion completed`,
|
||||
result: result,
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
export { convertController }
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response, NextFunction, Router } from 'express';
|
||||
|
||||
const homepageController = ( req:Request, res:Response, next:NextFunction ) => {
|
||||
const homepageController = ( req: Request, res: Response, next: NextFunction ) => {
|
||||
|
||||
// Make sure that the browser isn't caching this
|
||||
res.setHeader(`Cache-Control`, `no-cache, must-revalidate`);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Response } from "express";
|
||||
|
||||
export function returnError(res: Response, content: any, status: number = 400) {
|
||||
res.status(status).send(content);
|
||||
}
|
||||
|
||||
export function findFile(fieldName: string, fileList: Express.Multer.File[]): Express.Multer.File | null {
|
||||
|
||||
// Go through all files
|
||||
for ( let file of fileList ) {
|
||||
// Check if the field name matches
|
||||
if ( file.fieldname == fieldName) {
|
||||
// Return the found file
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Poppler } from "node-poppler";
|
||||
import fs from "fs/promises";
|
||||
|
||||
|
||||
export function convertFileToPNG(file: Express.Multer.File): Promise<Buffer> {
|
||||
|
||||
return new Promise<Buffer>((_r, _e) => {
|
||||
|
||||
console.log(file);
|
||||
// Read the file
|
||||
fs.readFile(file.path)
|
||||
.then( fileBuffer => {
|
||||
let p = new Poppler(`/usr/bin`);
|
||||
p.pdfToCairo(fileBuffer, undefined, {
|
||||
pngFile: true,
|
||||
singleFile: true,
|
||||
})
|
||||
.then( buff => {
|
||||
|
||||
if ( buff instanceof Error ) {
|
||||
return _e(buff);
|
||||
}
|
||||
|
||||
_r(Buffer.from(buff));
|
||||
})
|
||||
.catch(err => {
|
||||
_e(err);
|
||||
})
|
||||
})
|
||||
.catch( err => {
|
||||
return _e(err);
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Imports
|
||||
import express from 'express';
|
||||
import multer from 'multer';
|
||||
import { convertController } from '../Controllers/Convert';
|
||||
|
||||
const upload = multer({ dest: '/tmp' });
|
||||
|
||||
// Get the router
|
||||
const router = express.Router();
|
||||
|
||||
router.get(`/`, (req, res, next) => {
|
||||
res.status(200).send({
|
||||
status: "Healthy",
|
||||
healthStatus: "100%",
|
||||
version: "1.0.0",
|
||||
});
|
||||
})
|
||||
|
||||
// Set the route
|
||||
router.post("/convert", upload.any(), convertController);
|
||||
|
||||
// Export the reouter
|
||||
export {router as apiRouter};
|
||||
@@ -4,6 +4,7 @@ import express from 'express';
|
||||
import config from './Config/config';
|
||||
import { engine } from 'express-handlebars';
|
||||
import { homepageRouter } from './Routes/Homepage';
|
||||
import { apiRouter } from './Routes/Api';
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -58,6 +59,8 @@ router.use((req, res, next) => {
|
||||
router.use(`/public`, express.static('./src/public/'));
|
||||
|
||||
// Handle the homepage
|
||||
router.use("/api", apiRouter);
|
||||
|
||||
router.get("/", homepageRouter);
|
||||
|
||||
{ // Error handling
|
||||
|
||||
Reference in New Issue
Block a user