UNPKG

@adonisjs/bodyparser

Version:

AdonisJs body parser to read and parse HTTP request bodies

110 lines (109 loc) 3.06 kB
"use strict"; /* * @adonisjs/bodyparser * * (c) Harminder Virk <virk@adonisjs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ExtensionValidator = void 0; /** * Validates the file extension */ class ExtensionValidator { constructor(file) { this.file = file; this.allowedExtensions = []; this.validated = false; } /** * Update the expected file extensions */ get extensions() { return this.allowedExtensions; } set extensions(extnames) { if (this.allowedExtensions && this.allowedExtensions.length) { throw new Error('Cannot update allowed extension names after file has been validated'); } this.validated = false; this.allowedExtensions = extnames; } /** * Report error to the file */ reportError() { /** * File is invalid, so report the error */ const suffix = this.allowedExtensions.length === 1 ? 'is' : 'are'; const message = [ `Invalid file extension ${this.file.extname}.`, `Only ${this.allowedExtensions.join(', ')} ${suffix} allowed`, ].join(' '); this.file.errors.push({ fieldName: this.file.fieldName, clientName: this.file.clientName, message: message, type: 'extname', }); } /** * Validating the file in the streaming mode. During this mode * we defer the validation, until we get the file extname. */ validateWhenGettingStreamed() { if (!this.file.extname) { return; } this.validated = true; /** * Valid extension type */ if (this.allowedExtensions.includes(this.file.extname)) { return; } this.reportError(); } /** * Validate the file extension after it has been streamed */ validateAfterConsumed() { this.validated = true; /** * Valid extension type */ if (this.allowedExtensions.includes(this.file.extname || '')) { return; } this.reportError(); } /** * Validate the file */ validate() { /** * Do not validate if already validated */ if (this.validated) { return; } /** * Do not run validations, when constraints on the extension are not set */ if (!Array.isArray(this.allowedExtensions) || this.allowedExtensions.length === 0) { this.validated = true; return; } if (this.file.state === 'streaming') { this.validateWhenGettingStreamed(); return; } if (this.file.state === 'consumed') { this.validateAfterConsumed(); } } } exports.ExtensionValidator = ExtensionValidator;