File Type Checker
Detect and validate file types by their signatures (✨magic numbers✨)
- Supports a wide range of file types.
- Detects the type of a given file using a detection mechanism.
- Validates files against their requested types using signatures.
- Provides flexibility for the file data type: choose from
Array<number>
, ArrayBuffer
, or Uint8Array
. - Written in TypeScript for full typing support.
- Works on both server and client sides.
import fileTypeChecker from "file-type-checker";
fileTypeChecker.detectFile(file);
fileTypeChecker.validateFileType(file, ["png", "gif", "jpeg"]);
fileTypeChecker.isPNG(file);
Installation
Using npm:
npm i file-type-checker
Quick Overview
Detection sample:
Validation sample:
Resources
Supported Files
Type |
---|
7z |
aac |
amr |
avi |
bmp |
bpg |
blend |
cr2 |
elf |
exr |
flac |
flv |
gif |
heic |
ico |
indd |
jpeg |
lzh |
m4a |
m4v |
mkv |
mov |
mp3 |
mp4 |
ogg |
pdf |
pbm |
pgm |
png |
ppm |
psd |
ps |
rar |
rtf |
sqlite |
stl |
swf |
ttf |
wav |
webm |
webp |
zip |
Note
- Only file signatures are checked; additional validation may be necessary.
- To ensure optimal performance all files should be at least 64 bytes in size.
- To ensure optimal performance with ZIP files, it is recommended to utilize the 'chunkSize' optional parameter and pass the first 30000 bytes.
- This package assumes little-endian byte order; adjust code for big-endian.
Example Usage
Browser (React
, Angular
, Vanilla JS
, etc.):
Validate file signature against a list if file types (React app example):
import fileTypeChecker from "file-type-checker";
const handleFileInputChange = (event) => {
try {
const file = event.target.files[0];
const reader = new FileReader();
const types = ["jpeg", "png", "gif"];
reader.onload = () => {
const isImage = fileTypeChecker.validateFileType(reader.result, types);
console.log(isImage);
};
reader.readAsArrayBuffer(file);
} catch (err) {
console.error("Error: ", err.message);
}
};
return (
<div>
<input type="file" onChange={handleFileInputChange} />
</div>
);
import fileTypeChecker from "file-type-checker";
const handleFileInputChange = (event) => {
try {
const file = event.target.files[0];
const reader = new FileReader();
const types = ["mp4", "avi", "mov"];
reader.onload = () => {
const isVideo = fileTypeChecker.validateFileType(
reader.result,
types,
{ excludeSimilarTypes: true }
);
console.log(isVideo);
};
reader.readAsArrayBuffer(file);
} catch (err) {
console.error("Error: ", err.message);
}
};
return (
<div>
<input type="file" onChange={handleFileInputChange} />
</div>
);
Validate file signature against a single file type (React app example):
import fileTypeChecker from "file-type-checker";
const handleFileInputChange = (event) => {
try {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = () => {
const isPNG = fileTypeChecker.isPNG(reader.result);
console.log(isPNG);
};
reader.readAsArrayBuffer(file);
} catch (err) {
console.error("Error validating file type: ", err.message);
}
};
return (
<div>
<input type="file" onChange={handleFileInputChange} />
</div>
);
Detect file by its signature (React app example):
import fileTypeChecker from "file-type-checker";
const handleFileInputChange = (event) => {
try {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = () => {
const detectedFile = fileTypeChecker.detectFile(reader.result);
console.log(detectedFile) >
{
extension: "png",
mimeType: "image/png",
description:
"PNG (Portable Network Graphics) is a lossless image compression format that supports a wide range of color depths and transparency and is widely used for high-quality graphics.",
signature: {
sequence: ["89", "50", "4e", "47", "d", "a", "1a", "a"],
},
};
};
reader.readAsArrayBuffer(file);
} catch (err) {
console.error("Error: ", err.message);
}
};
return (
<div>
<input type="file" onChange={handleFileInputChange} />
</div>
);
Node.js:
Validate file signature against a list of file types:
import fileTypeChecker from "file-type-checker";
import fs from "fs";
const file = fs.readFileSync("/path/to/my/file.png").buffer;
const types = ["jpeg", "png", "gif"];
const isImage = fileTypeChecker.validateFileType(
file,
types,
{ chunkSize: 32 }
);
console.log(isImage);
Validate file signature against a single file type:
import fileTypeChecker from "file-type-checker";
import fs from "fs";
const file = fs.readFileSync("/path/to/my/file.png").buffer;
const isPNG = fileTypeChecker.isPNG(file);
console.log(isPNG);
Detect file by its signature:
import fileTypeChecker from "file-type-checker";
import fs from "fs";
const file = fs.readFileSync("/path/to/my/file.mp4").buffer;
const detectedFIle = fileTypeChecker.detectFile(file);
console.log(detectedFile)
> {
"extension": "mp4",
"mimeType": "video/mp4",
"description": "A multimedia container format widely used for storing audio, video, and other data, and is known for its high compression efficiency and compatibility with many devices",
"signature": {
"sequence": ["66","74","79","70","69","73","6f","6d"],
"description" (optional): "ISO Base Media file (MPEG-4) v1",
"offset" (optional): 4
}
}
API
detectFile(file, options?)
Detect the file type of a given file.
import fileTypeChecker from "file-type-checker";
const detectedFile = fileTypeChecker.detectFile(file);
> {
"extension": "png",
"mimeType": "image/png",
"description": "PNG (Portable Network Graphics) is a lossless image compression format that supports a wide range of color depths and transparency and is widely used for high-quality graphics.",
"signature": {
"sequence": ["89","50","4e","47","d","a","1a","a"]
}
}
Parameters:
file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.options
(optional) : object
- An object that contains additional actions to perfoem on the file:
chunkSize
(optional) : number
- Specifies the size of the file chunk to analyze, starting from the beginning of the file. For ZIP files, it is recommended to set this to 30,000 bytes. The default value is 64.
Returns:
object
- An object with information about a file, including its file extension, MIME type, and file signature. The returned object has the following properties:
extension
- A string that represents file extension (e.g., "png").mimeType
- A string that represents the MIME type of the file (e.g., "image/png").description
- A string that provides a short description of the file.signature
- An object that contains information about the file signature, with the following properties:
sequence
- An array of hexadecimal strings that represents the bytes in the file signature.description
(optional) - A string that provides a short description of the file signature.offset
(optional) - A number that indicates the offset of the file signature.skippedBytes
(optional) - An array of numbers that indicates the number of bytes that were skipped.compatibleExtensions
(optional): An array of strings that indicates file compatible extensions.
undefined
- If no file has found.
validateFileType(file, types, options?)
Validates the requested file signature against a list of accepted file types.
import fileTypeChecker from "file-type-checker";
const isImage = fileTypeChecker.validateFileType(file, ["jpeg", "png", "gif"]);
console.log(isImage);
Parameters:
file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.types
: Array<string>
- A list of accepted file types (from our supported files).options
(optional) : object
- An object that contains additional actions to perfoem on the file:
chunkSize
(optional) : number
- Specifies the size of the file chunk to analyze, starting from the beginning of the file. For compressed files, it is recommended to set this to 30,000 bytes. The default value is 64.excludeSimilarTypes
(optional) : boolean
- Specifies whether to ignore signatures of similar file types during validation. When validating a mp4
file, the m4v
signature will be ignored. When validating a m4a
file, the aac
signature will be ignored. The default value is false.
Returns a boolean
indicating whether the file is valid.
Validator functions for a single file type:
All supported files have validator functions that determine if a given file matched the requested type signature.
import fileTypeChecker from "file-type-checker";
const is7z = fileTypeChecker.is7z(file);
const isAAC = fileTypeChecker.isAAC(file);
const isAMR = fileTypeChecker.isAMR(file);
const isAVI = fileTypeChecker.isAVI(file);
const isBMP = fileTypeChecker.isBMP(file);
const isBPG = fileTypeChecker.isBPG(file);
const isBLEND = fileTypeChecker.isBLEND(file);
const isCR2 = fileTypeChecker.isCR2(file);
const isELF = fileTypeChecker.isELF(file);
const isEXR = fileTypeChecker.isEXR(file);
const isFLAC = fileTypeChecker.isFLAC(file);
const isFLV = fileTypeChecker.isFLV(file);
const isGIF = fileTypeChecker.isGIF(file);
const isHEIC = fileTypeChecker.isHEIC(file);
const isICO = fileTypeChecker.isICO(file);
const isINDD = fileTypeChecker.isINDD(file);
const isJPEG = fileTypeChecker.isJPEG(file);
const isLZH = fileTypeChecker.isLZH(file);
const isM4A = fileTypeChecker.isM4A(file);
const isM4V = fileTypeChecker.isM4V(file);
const isMKV = fileTypeChecker.isMKV(file);
const isMOV = fileTypeChecker.isMOV(file);
const isMP3 = fileTypeChecker.isMP3(file);
const isMP4 = fileTypeChecker.isMP4(file);
const isOGG = fileTypeChecker.isOGG(file);
const isORC = fileTypeChecker.isORC(file);
const isPARQUET = fileTypeChecker.isPARQUET(file);
const isPDF = fileTypeChecker.isPDF(file);
const isPBM = fileTypeChecker.isPBM(file);
const isPGM = fileTypeChecker.isPGM(file);
const isPNG = fileTypeChecker.isPNG(file);
const isPPM = fileTypeChecker.isPPM(file);
const isPSD = fileTypeChecker.isPSD(file);
const isPS = fileTypeChecker.isPS(file);
const isRAR = fileTypeChecker.isRAR(file);
const isRTF = fileTypeChecker.isRTF(file);
const isSQLite = fileTypeChecker.isSQLite(file);
const isSTL = fileTypeChecker.isSTL(file);
const isSWF = fileTypeChecker.isSWF(file);
const isTTF = fileTypeChecker.isTTF(file);
const isWAV = fileTypeChecker.isWAV(file);
const isWEBM = fileTypeChecker.isWEBM(file);
const isWEBP = fileTypeChecker.isWEBP(file);
const isZIP = fileTypeChecker.isZIP(file);
Image:
fileTypeChecker.isBMP(file)
Checks whether a file is a BMP image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid BMP image.
fileTypeChecker.isBPG(file)
Checks whether a file is a BPG image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid BPG image.
fileTypeChecker.isCR2(file)
Checks whether a file is a CR2 image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid CR2 image.
fileTypeChecker.isEXR(file)
Checks whether a file is a EXR image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid EXR image.
fileTypeChecker.isGIF(file)
Checks whether a file is a GIF image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid GIF image.
fileTypeChecker.isHEIC(file)
Checks whether a file is a HEIC image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid HEIC image.
fileTypeChecker.isICO(file)
Checks whether a file is an ICO image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid ICO image.
fileTypeChecker.isJPEG(file)
Checks whether a file is a JPEG image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid JPEG image.
fileTypeChecker.isPBM(file)
Checks whether a file is a PBM image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid PBM image.
fileTypeChecker.isPGM(file)
Checks whether a file is a PGM image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid PGM image.
fileTypeChecker.isPNG(file)
Checks whether a file is a PNG image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid PNG image.
fileTypeChecker.isPPM(file)
Checks whether a file is a PPM image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid PPM image.
fileTypeChecker.isPSD(file)
Checks whether a file is a PSD image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid PSD image.
fileTypeChecker.isTTF(file)
Checks whether a file is a TTF image by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid TTF image.
Video:
fileTypeChecker.isAVI(file)
Checks whether a file is an AVI video by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid AVI video.
fileTypeChecker.isFLV(file)
Checks whether a file is a FLV video by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid FLV video.
fileTypeChecker.isM4V(file)
Checks whether a file is a M4v video by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid M4v video.
fileTypeChecker.isMKV(file)
Checks whether a file is a MKV video by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid MKV video.
fileTypeChecker.isMOV(file)
Checks whether a file is a MOV video by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid MOV video.
fileTypeChecker.isMP4(file, options?)
Checks whether a file is a MP4 video by inspecting its file signature.
Parameters:
-
file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
-
options
(optional) : object
- An object that contains additional actions to perfoem on the file:
excludeSimilarTypes
(optional) : boolean
- Specifies whether to ignore signatures of similar file types during validation. When validating a mp4
file, the m4v
signature will be ignored. The default value is false.
Returns a boolean
indicating whether the file is a valid MP4 video.
fileTypeChecker.isOGG(file)
Checks whether a file is an OGG video by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid OGG video.
fileTypeChecker.isSWF(file)
Checks whether a file is a SWF video by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid SWF video.
fileTypeChecker.isWEBM(file)
Checks whether a file is a WEBM video by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid WEBM video.
Audio:
fileTypeChecker.isAAC(file, options?)
Checks whether a file is an AAC audio by inspecting its file signature.
Parameters:
-
file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
-
options
(optional) : object
- An object that contains additional actions to perfoem on the file:
excludeSimilarTypes
(optional) : boolean
- Specifies whether to ignore signatures of similar file types during validation. When validating a aac
file, the m4a
signature will be ignored. The default value is false.
Returns a boolean
indicating whether the file is a valid AAC audio.
fileTypeChecker.isAMR(file)
Checks whether a file is an AMR audio by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid AMR audio.
fileTypeChecker.isFLAC(file)
Checks whether a file is a FLAC audio by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid FLAC audio.
fileTypeChecker.isM4A(file)
Checks whether a file is a M4A audio by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid M4A audio.
fileTypeChecker.isMP3(file)
Checks whether a file is a MP3 audio by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid MP3 audio.
fileTypeChecker.isWAV(file)
Checks whether a file is a WAV audio by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid WAV audio.
Compressed:
fileTypeChecker.is7z(file)
Checks whether a file is a 7z compressed archive by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid 7z file.
fileTypeChecker.isLZH(file)
Checks whether a file is a LZH compressed archive by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid LZH file.
fileTypeChecker.isRAR(file)
Checks whether a file is a RAR compressed archive by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid RAR file.
fileTypeChecker.isZIP(file, options?)
Checks whether a file is a ZIP compressed archive by inspecting its file signature.
Parameters:
-
file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
-
options
(optional) : object
- An object that contains additional actions to perfoem on the file:
chunkSize
(optional) : number
- Specifies the size of the file chunk to analyze, starting from the beginning of the file. For ZIP files, it is recommended to set this to 30,000 bytes. The default value is 64.
Returns a boolean
indicating whether the file is a valid ZIP file.
Other:
fileTypeChecker.isBLEND(file)
Checks whether a file is a BLEND file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid BLEND file.
fileTypeChecker.isELF(file)
Checks whether a file is an ELF file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid ELF file.
fileTypeChecker.isINDD(file)
Checks whether a file is an INDD file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is an INDD file.
fileTypeChecker.isORC(file)
Checks whether a file is a ORC file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid ORC file.
fileTypeChecker.isPARQUET(file)
Checks whether a file is a PARQUET file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid PARQUET file.
fileTypeChecker.isPDF(file)
Checks whether a file is a PDF file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid PDF file.
fileTypeChecker.isPS(file)
Checks whether a file is a PS file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid PS file.
fileTypeChecker.isRTF(file)
Checks whether a file is a RTF file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid RTF file.
fileTypeChecker.isSQLITE(file)
Checks whether a file is a SQLITE file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid SQLITE file.
fileTypeChecker.isSTL(file)
Checks whether a file is a STL file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid STL file.
fileTypeChecker.isTTF(file)
Checks whether a file is a TTF file by inspecting its file signature.
Parameters: - file
: Array<number>
, ArrayBuffer
, or Uint8Array
- Binary data represents the file content.
Returns a boolean
indicating whether the file is a valid TTF file.
License
MIT