import { BaseMiddleware , ConfigurationService } from "@triptyk/nfw-core" ; import { NextFunction , Request , Response } from "express" ; import * as Jimp from "jimp" ; import { autoInjectable , injectable } from "tsyringe" ; import { ImageMimeTypes } from "../enums/mime-type.enum" ; export type DocumentResizeMiddlewareArgs = any ; @ injectable ( ) @ autoInjectable ( ) export class DocumentResizeMiddleware extends BaseMiddleware { public constructor ( private configurationService ) { super ( ) ; } public use ( req , res , next ) { const { jimp } = this . configurationService . config ; try { if ( jimp . isActive && Object . values ( ImageMimeTypes ) . includes ( req . file . mimetype as any ) ) { const { destination , path , filename } = req . file ; Jimp . read ( path ) . then ( ( image ) => { const xsImage = image . clone ( ) ; const mdImage = image . clone ( ) ; const xlImage = image . clone ( ) ; xsImage . resize ( jimp . xs , Jimp . AUTO ) . writeAsync ( `${ destination }/xs/${ filename }` ) ; mdImage . resize ( jimp . md , Jimp . AUTO ) . writeAsync ( `${ destination }/md/${ filename }` ) ; xlImage . resize ( jimp . xl , Jimp . AUTO ) . writeAsync ( `${ destination }/xl/${ filename }` ) ; } ) ; } return next ( ) ; } catch ( e ) { return next ( e ) ; } } }