UNPKG

validate-image-type

Version:

Check the image file of a Buffer/Uint8Array that matched expected image MIME-type.

117 lines 4.66 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateMIMEType = exports.validateBufferMIMEType = void 0; const assert_1 = __importDefault(require("assert")); const fs = __importStar(require("fs/promises")); const image_type_1 = require("./image-type"); const isBinary_1 = require("./isBinary"); /** * Detect the image type of Buffer or Uint8Array * This check is based on https://github.com/sindresorhus/image-type */ async function validateBufferMIMEType(buffer, options) { const mimeTypes = options.allowMimeTypes; assert_1.default.ok(Array.isArray(mimeTypes) && mimeTypes.every((mimeType) => mimeType.includes("/")), `Should be set an array of mimeType. e.g.) ['image/jpeg']`); const allowSVG = mimeTypes.includes("image/svg+xml"); if (allowSVG) { const { default: isSvg } = await import("is-svg"); if (isSvg(String(buffer))) { return { ok: true, error: undefined }; } } const imageTypeMime = await (0, image_type_1.imageType)(buffer); if (!imageTypeMime) { return { ok: false, error: new Error(`This buffer is not supported image. allowMimeTypes: ${JSON.stringify(mimeTypes)}` + (options.originalFilename ? `, filename: ${options.originalFilename}` : "")) }; } const isAllowed = mimeTypes.includes(imageTypeMime); if (!isAllowed) { return { ok: false, error: new Error(`This buffer is disallowed image MimeType: ${imageTypeMime}, allowMimeTypes: ${JSON.stringify(mimeTypes)}` + (options.originalFilename ? `,filename: ${options.originalFilename}` : "")) }; } return { ok: true, error: undefined }; } exports.validateBufferMIMEType = validateBufferMIMEType; /** * Detect the image type of filePath * @example * * ``` * const validationResult = validateMIMEType("test.png", { * allowMimeTypes: ['image/jpeg', 'image/gif', 'image/png', 'image/svg+xml'] * }); * if(validationResult) { * console.error(validationResult) * } * ``` */ async function validateMIMEType(filePath, options) { const { readChunk } = await import("read-chunk"); // Use head buffer for performance reason const buffer = await readChunk(filePath, { startPosition: 0, length: isBinary_1.BINARY_READ_LENGTH }); if (!(0, isBinary_1.isBinary)(buffer)) { const mimeTypes = options.allowMimeTypes; // Handle SVG as special case // https://github.com/sindresorhus/is-svg const allowSVG = mimeTypes.includes("image/svg+xml"); if (allowSVG) { const { default: isSvg } = await import("is-svg"); // if the content is not binary, read all content and check it // Note: Require 128 bytes at least one const content = await fs.readFile(filePath, "utf-8"); if (!isSvg(content)) { return { ok: false, error: new Error(`This file is not svg. allowMimeTypes: ${JSON.stringify(mimeTypes)}` + (options.originalFilename ? `, filename: ${options.originalFilename}` : "")) }; } return { ok: true }; } } return validateBufferMIMEType(buffer, options); } exports.validateMIMEType = validateMIMEType; //# sourceMappingURL=validate-image-type.js.map