file-type-checker
Version:
Detect and validate file types by their signatures (✨magic numbers✨)
194 lines (193 loc) • 7.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isWEBP = exports.isPSD = exports.isPPM = exports.isPNG = exports.isPGM = exports.isPBM = exports.isJPEG = exports.isICO = exports.isHEIC = exports.isGIF = exports.isEXR = exports.isCR2 = exports.isBPG = exports.isBMP = exports.isAVIF = void 0;
const core_1 = require("../core");
const utils_1 = require("../utils");
/**
* Determine if file content contains a valid 'avif' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'avif' in file content, otherwise false
*/
function isAVIF(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
const isAVIF = core_1.FileTypes.checkByFileType(fileChunk, "avif");
if (!isAVIF)
return false;
// Search for the presence of the "ftypavif" at bytes 5-12
return (0, utils_1.isAvifStringIncluded)(fileChunk);
}
exports.isAVIF = isAVIF;
/**
* Determine if file content contains a valid 'bmp' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'bmp' in file content, otherwise false
*/
function isBMP(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "bmp");
}
exports.isBMP = isBMP;
/**
* Determine if file content contains a valid 'bpg' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'bpg' in file content, otherwise false
*/
function isBPG(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "bpg");
}
exports.isBPG = isBPG;
/**
* Determine if file content contains a valid 'cr2' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'cr2' in file content, otherwise false
*/
function isCR2(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "cr2");
}
exports.isCR2 = isCR2;
/**
* Determine if file content contains a valid 'exr' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'exr' in file content, otherwise false
*/
function isEXR(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "exr");
}
exports.isEXR = isEXR;
/**
* Determine if file content contains a valid 'gif' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'gif' in file content, otherwise false
*/
function isGIF(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "gif");
}
exports.isGIF = isGIF;
/**
* Determine if file content contains a valid 'heic' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'heic' in file content, otherwise false
*/
function isHEIC(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
const isHEIC = core_1.FileTypes.checkByFileType(fileChunk, "avif");
if (!isHEIC)
return false;
// Determine if a file chunk contains a HEIC file box
return (0, utils_1.isHeicSignatureIncluded)(fileChunk);
}
exports.isHEIC = isHEIC;
/**
* Determine if file content contains a valid 'ico' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'ico' in file content, otherwise false
*/
function isICO(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "ico");
}
exports.isICO = isICO;
/**
* Determine if file content contains a valid 'jpeg' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'jpeg' in file content, otherwise false
*/
function isJPEG(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "jpeg");
}
exports.isJPEG = isJPEG;
/**
* Determine if file content contains a valid 'pbm' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'pbm' in file content, otherwise false
*/
function isPBM(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "pbm");
}
exports.isPBM = isPBM;
/**
* Determine if file content contains a valid 'pgm' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'pgm' in file content, otherwise false
*/
function isPGM(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "pgm");
}
exports.isPGM = isPGM;
/**
* Determine if file content contains a valid 'png' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'png' in file content, otherwise false
*/
function isPNG(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "png");
}
exports.isPNG = isPNG;
/**
* Determine if file content contains a valid 'ppm' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'ppm' in file content, otherwise false
*/
function isPPM(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "ppm");
}
exports.isPPM = isPPM;
/**
* Determine if file content contains a valid 'psd' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'psd' in file content, otherwise false
*/
function isPSD(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "psd");
}
exports.isPSD = isPSD;
/**
* Determine if file content contains a valid 'webp' file signature
*
* @param file File content represents in Array<number> / ArrayBuffer / Uint8Array
*
* @returns {boolean} True if found a signature of type 'webp' in file content, otherwise false
*/
function isWEBP(file) {
const fileChunk = (0, utils_1.getFileChunk)(file);
return core_1.FileTypes.checkByFileType(fileChunk, "webp");
}
exports.isWEBP = isWEBP;