UNPKG

file-type-checker

Version:

Detect and validate file types by their signatures (✨magic numbers✨)

141 lines (140 loc) 5.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isWEBM = exports.isSWF = exports.isOGG = exports.isMP4 = exports.isMOV = exports.isMKV = exports.isM4V = exports.isFLV = exports.isAVI = void 0; const core_1 = require("../core"); const utils_1 = require("../utils"); /** * Determine if file content contains a valid 'avi' file signature * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * * @returns {boolean} True if found a signature of type 'avi' in file content, otherwise false */ function isAVI(file) { const fileChunk = (0, utils_1.getFileChunk)(file); return core_1.FileTypes.checkByFileType(fileChunk, "avi"); } exports.isAVI = isAVI; /** * Determine if file content contains a valid 'flv' file signature. * Since 'flv' and 'm4v' share the same signature - additional check required - check if file content contains a "flv" string in the first few bytes of the file * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * * @returns {boolean} True if found a signature of type 'flv' & "flv" string in file content, otherwise false */ function isFLV(file) { const fileChunk = (0, utils_1.getFileChunk)(file); const isFlvSignature = core_1.FileTypes.checkByFileType(fileChunk, "flv"); if (!isFlvSignature) return false; // Check if file content contains a "flv" string return (0, utils_1.isFlvStringIncluded)(fileChunk); } exports.isFLV = isFLV; /** * Determine if file content contains a valid 'm4v' file signature. * Since 'flv' and 'm4v' share the same signature - additional check required - check if file content contains a "ftyp" string in the first few bytes of the file * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * * @returns {boolean} True if found a signature of type 'm4v' & "ftyp" string in file content, otherwise false */ function isM4V(file) { const fileChunk = (0, utils_1.getFileChunk)(file); const isM4vSignature = core_1.FileTypes.checkByFileType(fileChunk, "m4v"); if (!isM4vSignature) return false; // Check if file content contains a "ftyp" string return (0, utils_1.isftypStringIncluded)(fileChunk); } exports.isM4V = isM4V; /** * Determine if file content contains a valid 'mkv' file signature. * Since 'mkv' and 'webm' share the same signature - additional check required - search for the presence of the "Segment" element in the mkv header * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * * @returns {boolean} True if found a signature of type 'mkv' & "ftyp" string in file content, otherwise false */ function isMKV(file) { const fileChunk = (0, utils_1.getFileChunk)(file, 64); // Check the first 64 bytes of the file const isMkvSignature = core_1.FileTypes.checkByFileType(fileChunk, "mkv"); if (!isMkvSignature) return false; // Search for the presence of the "Segment" element in the mkv header return (0, utils_1.findMatroskaDocTypeElements)(fileChunk) === "mkv"; } exports.isMKV = isMKV; /** * Determine if file content contains a valid 'mov' file signature * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * * @returns {boolean} True if found a signature of type 'mov' in file content, otherwise false */ function isMOV(file) { const fileChunk = (0, utils_1.getFileChunk)(file); return core_1.FileTypes.checkByFileType(fileChunk, "mov"); } exports.isMOV = isMOV; /** * Determine if file content contains a valid 'mp4' file signature. * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * @param options parameters for additional actions * * @returns {boolean} True if found a signature of type 'mp4' in file content, otherwise false */ function isMP4(file, options) { const fileChunk = (0, utils_1.getFileChunk)(file); const isMp4 = core_1.FileTypes.checkByFileType(fileChunk, "mp4"); if (!isMp4) { if (options === null || options === void 0 ? void 0 : options.excludeSimilarTypes) return false; return isM4V(fileChunk); // since 'm4v' is very similar to 'mp4' } return true; } exports.isMP4 = isMP4; /** * Determine if file content contains a valid 'ogg' file signature * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * * @returns {boolean} True if found a signature of type 'ogg' in file content, otherwise false */ function isOGG(file) { const fileChunk = (0, utils_1.getFileChunk)(file); return core_1.FileTypes.checkByFileType(fileChunk, "ogg"); } exports.isOGG = isOGG; /** * Determine if file content contains a valid 'swf' file signature * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * * @returns {boolean} True if found a signature of type 'swf' in file content, otherwise false */ function isSWF(file) { const fileChunk = (0, utils_1.getFileChunk)(file); return core_1.FileTypes.checkByFileType(fileChunk, "swf"); } exports.isSWF = isSWF; /** * Determine if file content contains a valid 'webm' file signature. * Since 'mkv' and 'webm' share the same signature - additional check required - search for the presence of the "DocType" element in the webm header * * @param file File content represents in Array<number> / ArrayBuffer / Uint8Array * * @returns {boolean} True if found a signature of type 'webm' & "ftyp" string in file content, otherwise false */ function isWEBM(file) { const fileChunk = (0, utils_1.getFileChunk)(file, 64); // Check the first 64 bytes of the file const isWebmSignature = core_1.FileTypes.checkByFileType(fileChunk, "webm"); if (!isWebmSignature) return false; // Search for the presence of the "DocType" element in the webm header return (0, utils_1.findMatroskaDocTypeElements)(fileChunk) === "webm"; } exports.isWEBM = isWEBM;