import { BootMixin } from '@loopback/boot' ; import { ApplicationConfig } from '@loopback/core' ; import { RestApplication } from '@loopback/rest' ; import { RestExplorerBindings , RestExplorerComponent , } from '@loopback/rest-explorer' ; import multer from 'multer' ; import path from 'path' ; import { FILE_UPLOAD_SERVICE , STORAGE_DIRECTORY } from './keys' ; import { MySequence } from './sequence' ; export { ApplicationConfig } ; export class FileUploadApplication extends BootMixin ( RestApplication ) { constructor ( options = { } ) { super ( options ) ; this . sequence ( MySequence ) ; this . static ( '/' , path . join ( __dirname , '../public' ) ) ; this . configure ( RestExplorerBindings . COMPONENT ) . to ( { path : '/explorer' , } ) ; this . component ( RestExplorerComponent ) ; this . configureFileUpload ( options . fileStorageDirectory ) ; this . projectRoot = __dirname ; this . bootOptions = { controllers : { dirs : [ 'controllers' ] , extensions : [ '.controller.js' ] , nested : true , } , } ; } /**
     * Configure `multer` options for file upload
     */ protected configureFileUpload ( destination ? ) { destination = destination ?? path . join ( __dirname , '../.sandbox' ) ; this . bind ( STORAGE_DIRECTORY ) . to ( destination ) ; const multerOptions = { storage : multer . diskStorage ( { destination , filename : ( req , file , cb ) => { cb ( null , file . originalname ) ; } , } ) , } ; this . configure ( FILE_UPLOAD_SERVICE ) . to ( multerOptions ) ; } }