// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved. // Node module: @loopback/example-file-transfer // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT 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: ApplicationConfig = {}) { super(options); // Set up the custom sequence this.sequence(MySequence); // Set up default home page this.static('/', path.join(__dirname, '../public')); // Customize @loopback/rest-explorer configuration here this.configure(RestExplorerBindings.COMPONENT).to({ path: '/explorer', }); this.component(RestExplorerComponent); // Configure file upload with multer options this.configureFileUpload(options.fileStorageDirectory); this.projectRoot = __dirname; // Customize @loopback/boot Booter Conventions here this.bootOptions = { controllers: { // Customize ControllerBooter Conventions here dirs: ['controllers'], extensions: ['.controller.js'], nested: true, }, }; } /** * Configure `multer` options for file upload */ protected configureFileUpload(destination?: string) { // Upload files to `dist/.sandbox` by default destination = destination ?? path.join(__dirname, '../.sandbox'); this.bind(STORAGE_DIRECTORY).to(destination); const multerOptions: multer.Options = { storage: multer.diskStorage({ destination, // Use the original file name as is filename: (req, file, cb) => { cb(null, file.originalname); }, }), }; // Configure the file upload service with multer options this.configure(FILE_UPLOAD_SERVICE).to(multerOptions); } }