You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # file-type [![Build Status](https://travis-ci.org/sindresorhus/file-type.svg?branch=master)](https://travis-ci.org/sindresorhus/file-type)
  2. > Detect the file type of a Buffer/Uint8Array
  3. The file type is detected by checking the [magic number](http://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
  4. ## Install
  5. ```
  6. $ npm install file-type
  7. ```
  8. ## Usage
  9. ##### Node.js
  10. ```js
  11. const readChunk = require('read-chunk');
  12. const fileType = require('file-type');
  13. const buffer = readChunk.sync('unicorn.png', 0, 4100);
  14. fileType(buffer);
  15. //=> {ext: 'png', mime: 'image/png'}
  16. ```
  17. Or from a remote location:
  18. ```js
  19. const http = require('http');
  20. const fileType = require('file-type');
  21. const url = 'http://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';
  22. http.get(url, res => {
  23. res.once('data', chunk => {
  24. res.destroy();
  25. console.log(fileType(chunk));
  26. //=> {ext: 'gif', mime: 'image/gif'}
  27. });
  28. });
  29. ```
  30. ##### Browser
  31. ```js
  32. const xhr = new XMLHttpRequest();
  33. xhr.open('GET', 'unicorn.png');
  34. xhr.responseType = 'arraybuffer';
  35. xhr.onload = () => {
  36. fileType(new Uint8Array(this.response));
  37. //=> {ext: 'png', mime: 'image/png'}
  38. };
  39. xhr.send();
  40. ```
  41. ## API
  42. ### fileType(input)
  43. Returns an `Object` with:
  44. - `ext` - One of the [supported file types](#supported-file-types)
  45. - `mime` - The [MIME type](http://en.wikipedia.org/wiki/Internet_media_type)
  46. Or `null` when no match.
  47. #### input
  48. Type: `Buffer` `Uint8Array`
  49. It only needs the first 4100 bytes.
  50. ## Supported file types
  51. - [`jpg`](https://en.wikipedia.org/wiki/JPEG)
  52. - [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics)
  53. - [`gif`](https://en.wikipedia.org/wiki/GIF)
  54. - [`webp`](https://en.wikipedia.org/wiki/WebP)
  55. - [`flif`](https://en.wikipedia.org/wiki/Free_Lossless_Image_Format)
  56. - [`cr2`](http://fileinfo.com/extension/cr2)
  57. - [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format)
  58. - [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format)
  59. - [`jxr`](https://en.wikipedia.org/wiki/JPEG_XR)
  60. - [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format)
  61. - [`zip`](https://en.wikipedia.org/wiki/Zip_(file_format))
  62. - [`tar`](https://en.wikipedia.org/wiki/Tar_(computing)#File_format)
  63. - [`rar`](https://en.wikipedia.org/wiki/RAR_(file_format))
  64. - [`gz`](https://en.wikipedia.org/wiki/Gzip)
  65. - [`bz2`](https://en.wikipedia.org/wiki/Bzip2)
  66. - [`7z`](https://en.wikipedia.org/wiki/7z)
  67. - [`dmg`](https://en.wikipedia.org/wiki/Apple_Disk_Image)
  68. - [`mp4`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#Filename_extensions)
  69. - [`m4v`](https://en.wikipedia.org/wiki/M4V)
  70. - [`mid`](https://en.wikipedia.org/wiki/MIDI)
  71. - [`mkv`](https://en.wikipedia.org/wiki/Matroska)
  72. - [`webm`](https://en.wikipedia.org/wiki/WebM)
  73. - [`mov`](https://en.wikipedia.org/wiki/QuickTime_File_Format)
  74. - [`avi`](https://en.wikipedia.org/wiki/Audio_Video_Interleave)
  75. - [`wmv`](https://en.wikipedia.org/wiki/Windows_Media_Video)
  76. - [`mpg`](https://en.wikipedia.org/wiki/MPEG-1)
  77. - [`mp3`](https://en.wikipedia.org/wiki/MP3)
  78. - [`m4a`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#.MP4_versus_.M4A)
  79. - [`ogg`](https://en.wikipedia.org/wiki/Ogg)
  80. - [`opus`](https://en.wikipedia.org/wiki/Opus_(audio_format))
  81. - [`flac`](https://en.wikipedia.org/wiki/FLAC)
  82. - [`wav`](https://en.wikipedia.org/wiki/WAV)
  83. - [`amr`](https://en.wikipedia.org/wiki/Adaptive_Multi-Rate_audio_codec)
  84. - [`pdf`](https://en.wikipedia.org/wiki/Portable_Document_Format)
  85. - [`epub`](https://en.wikipedia.org/wiki/EPUB)
  86. - [`exe`](https://en.wikipedia.org/wiki/.exe)
  87. - [`swf`](https://en.wikipedia.org/wiki/SWF)
  88. - [`rtf`](https://en.wikipedia.org/wiki/Rich_Text_Format)
  89. - [`woff`](https://en.wikipedia.org/wiki/Web_Open_Font_Format)
  90. - [`woff2`](https://en.wikipedia.org/wiki/Web_Open_Font_Format)
  91. - [`eot`](https://en.wikipedia.org/wiki/Embedded_OpenType)
  92. - [`ttf`](https://en.wikipedia.org/wiki/TrueType)
  93. - [`otf`](https://en.wikipedia.org/wiki/OpenType)
  94. - [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format))
  95. - [`flv`](https://en.wikipedia.org/wiki/Flash_Video)
  96. - [`ps`](https://en.wikipedia.org/wiki/Postscript)
  97. - [`xz`](https://en.wikipedia.org/wiki/Xz)
  98. - [`sqlite`](https://www.sqlite.org/fileformat2.html)
  99. - [`nes`](http://fileinfo.com/extension/nes)
  100. - [`crx`](https://developer.chrome.com/extensions/crx)
  101. - [`xpi`](https://en.wikipedia.org/wiki/XPInstall)
  102. - [`cab`](https://en.wikipedia.org/wiki/Cabinet_(file_format))
  103. - [`deb`](https://en.wikipedia.org/wiki/Deb_(file_format))
  104. - [`ar`](https://en.wikipedia.org/wiki/Ar_(Unix))
  105. - [`rpm`](http://fileinfo.com/extension/rpm)
  106. - [`Z`](http://fileinfo.com/extension/z)
  107. - [`lz`](https://en.wikipedia.org/wiki/Lzip)
  108. - [`msi`](https://en.wikipedia.org/wiki/Windows_Installer)
  109. - [`mxf`](https://en.wikipedia.org/wiki/Material_Exchange_Format)
  110. - [`mts`](https://en.wikipedia.org/wiki/.m2ts)
  111. - [`wasm`](https://en.wikipedia.org/wiki/WebAssembly)
  112. - [`blend`](https://wiki.blender.org/index.php/Dev:Source/Architecture/File_Format)
  113. - [`bpg`](https://bellard.org/bpg/)
  114. - [`docx`](https://en.wikipedia.org/wiki/Office_Open_XML)
  115. - [`pptx`](https://en.wikipedia.org/wiki/Office_Open_XML)
  116. - [`xlsx`](https://en.wikipedia.org/wiki/Office_Open_XML)
  117. *SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).*
  118. *Pull request welcome for additional commonly used file types.*
  119. ## Related
  120. - [file-type-cli](https://github.com/sindresorhus/file-type-cli) - CLI for this module
  121. ## Created by
  122. - [Sindre Sorhus](https://github.com/sindresorhus)
  123. - [Mikael Finstad](https://github.com/mifi)
  124. ## License
  125. MIT