Skip to content

Commit

Permalink
chore(example-file-upload): address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Mar 7, 2020
1 parent db00033 commit 01013af
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
38 changes: 20 additions & 18 deletions examples/file-upload/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {BootMixin} from '@loopback/boot';
import {ApplicationConfig} from '@loopback/core';
import {RestApplication, RestBindings} from '@loopback/rest';
import {RestApplication} from '@loopback/rest';
import {
RestExplorerBindings,
RestExplorerComponent,
Expand All @@ -32,23 +32,7 @@ export class FileUploadApplication extends BootMixin(RestApplication) {
this.component(RestExplorerComponent);

// Configure file upload with multer options
const multerOptions: multer.Options = {
storage: multer.diskStorage({
// Upload files to `.sandbox`
destination: path.join(__dirname, '../.sandbox'),
// Use the original file name as is
filename: (req, file, cb) => {
cb(null, file.originalname);
},
}),
};
this.configure(FILE_UPLOAD_SERVICE).to(multerOptions);

// Configure AJV to ignore `binary` format which is required for OpenAPI
// spec 3.0 for file uploads
this.bind(RestBindings.REQUEST_BODY_PARSER_OPTIONS).to({
validation: {unknownFormats: ['binary']},
});
this.configureFileUpload();

this.projectRoot = __dirname;
// Customize @loopback/boot Booter Conventions here
Expand All @@ -61,4 +45,22 @@ export class FileUploadApplication extends BootMixin(RestApplication) {
},
};
}

/**
* Configure `multer` options for file upload
*/
protected configureFileUpload() {
const multerOptions: multer.Options = {
storage: multer.diskStorage({
// Upload files to `.sandbox`
destination: path.join(__dirname, '../.sandbox'),
// 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);
}
}
12 changes: 9 additions & 3 deletions examples/file-upload/src/controllers/file-upload.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
Response,
RestBindings,
} from '@loopback/rest';
import {RequestHandler} from 'express-serve-static-core';
import {FILE_UPLOAD_SERVICE} from '../keys';
import {FileUploadHandler} from '../types';

/**
* A controller to handle file uploads using multipart/form-data media type
Expand All @@ -22,7 +22,9 @@ export class FileUploadController {
* Constructor
* @param handler - Inject an express request handler to deal with the request
*/
constructor(@inject(FILE_UPLOAD_SERVICE) private handler: RequestHandler) {}
constructor(
@inject(FILE_UPLOAD_SERVICE) private handler: FileUploadHandler,
) {}
@post('/file-upload', {
responses: {
200: {
Expand All @@ -33,7 +35,7 @@ export class FileUploadController {
},
},
},
description: '',
description: 'Files and fields',
},
},
})
Expand Down Expand Up @@ -78,6 +80,10 @@ export class FileUploadController {
});
}

/**
* Get files and fields for the request
* @param request - Http request
*/
private static getFilesAndFields(request: Request) {
const uploadedFiles = request.files;
const mapper = (f: globalThis.Express.Multer.File) => ({
Expand Down
7 changes: 5 additions & 2 deletions examples/file-upload/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// License text available at https://opensource.org/licenses/MIT

import {BindingKey} from '@loopback/core';
import {RequestHandler} from 'express-serve-static-core';
import {FileUploadHandler} from './types';

export const FILE_UPLOAD_SERVICE = BindingKey.create<RequestHandler>(
/**
* Binding key for the file upload service
*/
export const FILE_UPLOAD_SERVICE = BindingKey.create<FileUploadHandler>(
'services.FileUpload',
);
8 changes: 4 additions & 4 deletions examples/file-upload/src/services/file-upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
config,
ContextTags,
Provider,
} from '@loopback/core';
import {RequestHandler} from 'express-serve-static-core';
} from '@loopback/context';
import multer from 'multer';
import {FILE_UPLOAD_SERVICE} from '../keys';
import {FileUploadHandler} from '../types';

/**
* A provider to return an `Express` request handler from `multer` middleware
Expand All @@ -21,15 +21,15 @@ import {FILE_UPLOAD_SERVICE} from '../keys';
scope: BindingScope.TRANSIENT,
tags: {[ContextTags.KEY]: FILE_UPLOAD_SERVICE},
})
export class FileUploadProvider implements Provider<RequestHandler> {
export class FileUploadProvider implements Provider<FileUploadHandler> {
constructor(@config() private options: multer.Options = {}) {
if (!this.options.storage) {
// Default to in-memory storage
this.options.storage = multer.memoryStorage();
}
}

value(): RequestHandler {
value(): FileUploadHandler {
return multer(this.options).any();
}
}
8 changes: 8 additions & 0 deletions examples/file-upload/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/example-file-upload
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {RequestHandler} from 'express-serve-static-core';

export type FileUploadHandler = RequestHandler;

0 comments on commit 01013af

Please sign in to comment.