Convert Figma logo to code with AI

h2non logofiletype

Fast, dependency-free Go package to infer binary file types based on the magic numbers header signature

2,086
175
2,086
46

Top Related Projects

Detect the file type of a file, stream, or data

A fast Golang library for media type and file extension detection, based on magic numbers

29,063

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.

1,246

A lightning fast image processing and resizing library for Go

Quick Overview

h2non/filetype is a Go package that provides fast and simple file type checking and MIME type detection. It supports a wide range of file formats and can identify file types based on their content (magic numbers) rather than relying solely on file extensions.

Pros

  • Fast and efficient file type detection
  • Supports a large number of file formats
  • Can be used for both in-memory data and files on disk
  • Easily extensible to add custom file type matchers

Cons

  • Limited to file type detection only, doesn't provide file parsing or manipulation
  • May not cover all possible file types or variants
  • Relies on magic numbers, which can sometimes be misleading
  • Requires manual updates to add support for new file types

Code Examples

  1. Checking file type from a file path:
package main

import (
    "fmt"
    "github.com/h2non/filetype"
)

func main() {
    kind, _ := filetype.MatchFile("image.jpg")
    if kind == filetype.IMAGE {
        fmt.Println("File is an image")
    }
}
  1. Detecting MIME type from a byte slice:
package main

import (
    "fmt"
    "io/ioutil"
    "github.com/h2non/filetype"
)

func main() {
    buf, _ := ioutil.ReadFile("document.pdf")
    kind, _ := filetype.Match(buf)
    if kind != filetype.Unknown {
        fmt.Printf("MIME type: %s\n", kind.MIME.Value)
    }
}
  1. Adding a custom file type matcher:
package main

import (
    "github.com/h2non/filetype"
)

var customType = filetype.NewType("cst", "application/x-custom")

func customMatcher(buf []byte) bool {
    return len(buf) > 3 &&
        buf[0] == 0x11 && buf[1] == 0x22 && buf[2] == 0x33 && buf[3] == 0x44
}

func init() {
    filetype.AddMatcher(customType, customMatcher)
}

Getting Started

To use h2non/filetype in your Go project, first install it:

go get github.com/h2non/filetype

Then, import it in your Go code:

import "github.com/h2non/filetype"

Now you can use the package to detect file types:

kind, _ := filetype.Match([]byte{0xFF, 0xD8, 0xFF})
if kind == filetype.IMAGE {
    fmt.Println("File type: IMAGE")
    fmt.Printf("MIME type: %s\n", kind.MIME.Value)
}

Competitor Comparisons

Detect the file type of a file, stream, or data

Pros of file-type

  • More comprehensive file type support, including audio, video, and archive formats
  • Regular updates and active maintenance
  • Extensive documentation and examples

Cons of file-type

  • Larger package size due to more extensive file type support
  • Slightly more complex API for some use cases

Code Comparison

file-type:

import {fileTypeFromBuffer} from 'file-type';

const buffer = readChunk.sync('path/to/file', 0, 4100);
const type = await fileTypeFromBuffer(buffer);

console.log(type); // { ext: 'png', mime: 'image/png' }

filetype:

const filetype = require('filetype');

const buffer = readChunk.sync('path/to/file', 0, 262);
const type = filetype(buffer);

console.log(type); // { ext: 'png', mime: 'image/png' }

Both libraries provide similar functionality for detecting file types from buffers. file-type uses a promise-based approach and requires a larger buffer size, while filetype uses a synchronous function and works with a smaller buffer.

file-type offers more extensive file type support and regular updates, making it suitable for projects requiring broad format detection. filetype, on the other hand, has a simpler API and smaller package size, which may be preferable for projects with more specific file type detection needs.

A fast Golang library for media type and file extension detection, based on magic numbers

Pros of mimetype

  • Supports a wider range of file types, including audio and font formats
  • Generally faster performance, especially for large files
  • More actively maintained with frequent updates

Cons of mimetype

  • Slightly larger binary size
  • Less comprehensive documentation compared to filetype

Code Comparison

mimetype:

mimetype, err := mimetype.DetectFile("file.jpg")
if err != nil {
    log.Fatal(err)
}
fmt.Println(mimetype.String())

filetype:

buf, _ := ioutil.ReadFile("file.jpg")
kind, _ := filetype.Match(buf)
if kind == filetype.Unknown {
    fmt.Println("Unknown file type")
} else {
    fmt.Println(kind.MIME.Value)
}

Both libraries offer similar functionality for detecting file types, but mimetype provides a more straightforward API with built-in file reading. filetype requires manual file reading before detection.

mimetype uses a tree-based approach for MIME type detection, which contributes to its performance advantages. It also offers additional features like extension-based detection and custom MIME type definition.

filetype, while slightly older, has a more established presence in the Go community and may be preferred for simpler projects or those with stricter size constraints.

29,063

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.

Pros of sharp

  • Powerful image processing capabilities, including resizing, format conversion, and various transformations
  • High performance with native bindings to libvips image processing library
  • Extensive documentation and active community support

Cons of sharp

  • Larger package size due to native dependencies
  • More complex setup and installation process
  • Steeper learning curve for basic file type detection

Code comparison

sharp:

const sharp = require('sharp');

sharp('input.jpg')
  .resize(300, 200)
  .toFile('output.png')
  .then(info => console.log(info));

filetype:

const filetype = require('filetype');

(async () => {
  const type = await filetype.fromFile('path/to/file');
  console.log(type);
})();

Summary

sharp is a comprehensive image processing library with powerful features and high performance, but it comes with a larger footprint and more complex setup. filetype, on the other hand, is a lightweight and simple solution for file type detection, lacking image manipulation capabilities but offering a straightforward API for identifying file types. Choose sharp for extensive image processing needs, and filetype for quick and easy file type identification.

1,246

A lightning fast image processing and resizing library for Go

Pros of govips

  • Comprehensive image processing library with a wide range of operations
  • High-performance C bindings to libvips for efficient image manipulation
  • Supports various image formats and color spaces

Cons of govips

  • Larger and more complex codebase, potentially steeper learning curve
  • Requires external dependencies (libvips) for full functionality
  • May be overkill for simple file type detection tasks

Code Comparison

filetype:

kind, _ := filetype.Match(buf)
if kind == filetype.Image {
    fmt.Println("File is an image")
}

govips:

image, err := vips.NewImageFromBuffer(buf)
if err == nil {
    fmt.Printf("Image format: %s\n", image.Format())
    defer image.Close()
}

Summary

filetype is a lightweight library focused on file type detection, while govips is a comprehensive image processing library. filetype is simpler to use for basic file type checks, but govips offers more advanced image manipulation capabilities at the cost of increased complexity and external dependencies. Choose filetype for quick file type detection or govips for extensive image processing needs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

filetype GoDoc Go Version

Small and dependency free Go package to infer file and MIME type checking the magic numbers signature.

For SVG file type checking, see go-is-svg package. Python port: filetype.py.

Features

  • Supports a wide range of file types
  • Provides file extension and proper MIME type
  • File discovery by extension or MIME type
  • File discovery by class (image, video, audio...)
  • Provides a bunch of helpers and file matching shortcuts
  • Pluggable: add custom new types and matchers
  • Simple and semantic API
  • Blazing fast, even processing large files
  • Only first 262 bytes representing the max file header is required, so you can just pass a slice
  • Dependency free (just Go code, no C compilation needed)
  • Cross-platform file recognition

Installation

go get github.com/h2non/filetype

API

See Godoc reference.

Subpackages

Examples

Simple file type checking

package main

import (
  "fmt"
  "io/ioutil"

  "github.com/h2non/filetype"
)

func main() {
  buf, _ := ioutil.ReadFile("sample.jpg")

  kind, _ := filetype.Match(buf)
  if kind == filetype.Unknown {
    fmt.Println("Unknown file type")
    return
  }

  fmt.Printf("File type: %s. MIME: %s\n", kind.Extension, kind.MIME.Value)
}

Check type class

package main

import (
  "fmt"
  "io/ioutil"

  "github.com/h2non/filetype"
)

func main() {
  buf, _ := ioutil.ReadFile("sample.jpg")

  if filetype.IsImage(buf) {
    fmt.Println("File is an image")
  } else {
    fmt.Println("Not an image")
  }
}

Supported type

package main

import (
  "fmt"

  "github.com/h2non/filetype"
)

func main() {
  // Check if file is supported by extension
  if filetype.IsSupported("jpg") {
    fmt.Println("Extension supported")
  } else {
    fmt.Println("Extension not supported")
  }

  // Check if file is supported by extension
  if filetype.IsMIMESupported("image/jpeg") {
    fmt.Println("MIME type supported")
  } else {
    fmt.Println("MIME type not supported")
  }
}

File header

package main

import (
  "fmt"
  "os"

  "github.com/h2non/filetype"
)

func main() {
  // Open a file descriptor
  file, _ := os.Open("movie.mp4")

  // We only have to pass the file header = first 261 bytes
  head := make([]byte, 261)
  file.Read(head)

  if filetype.IsImage(head) {
    fmt.Println("File is an image")
  } else {
    fmt.Println("Not an image")
  }
}

Add additional file type matchers

package main

import (
  "fmt"

  "github.com/h2non/filetype"
)

var fooType = filetype.NewType("foo", "foo/foo")

func fooMatcher(buf []byte) bool {
  return len(buf) > 1 && buf[0] == 0x01 && buf[1] == 0x02
}

func main() {
  // Register the new matcher and its type
  filetype.AddMatcher(fooType, fooMatcher)

  // Check if the new type is supported by extension
  if filetype.IsSupported("foo") {
    fmt.Println("New supported type: foo")
  }

  // Check if the new type is supported by MIME
  if filetype.IsMIMESupported("foo/foo") {
    fmt.Println("New supported MIME type: foo/foo")
  }

  // Try to match the file
  fooFile := []byte{0x01, 0x02}
  kind, _ := filetype.Match(fooFile)
  if kind == filetype.Unknown {
    fmt.Println("Unknown file type")
  } else {
    fmt.Printf("File type matched: %s\n", kind.Extension)
  }
}

Supported types

Image

  • jpg - image/jpeg
  • png - image/png
  • gif - image/gif
  • webp - image/webp
  • cr2 - image/x-canon-cr2
  • tif - image/tiff
  • bmp - image/bmp
  • heif - image/heif
  • jxr - image/vnd.ms-photo
  • psd - image/vnd.adobe.photoshop
  • ico - image/vnd.microsoft.icon
  • dwg - image/vnd.dwg
  • avif - image/avif

Video

  • mp4 - video/mp4
  • m4v - video/x-m4v
  • mkv - video/x-matroska
  • webm - video/webm
  • mov - video/quicktime
  • avi - video/x-msvideo
  • wmv - video/x-ms-wmv
  • mpg - video/mpeg
  • flv - video/x-flv
  • 3gp - video/3gpp

Audio

  • mid - audio/midi
  • mp3 - audio/mpeg
  • m4a - audio/mp4
  • ogg - audio/ogg
  • flac - audio/x-flac
  • wav - audio/x-wav
  • amr - audio/amr
  • aac - audio/aac
  • aiff - audio/x-aiff

Archive

  • epub - application/epub+zip
  • zip - application/zip
  • tar - application/x-tar
  • rar - application/vnd.rar
  • gz - application/gzip
  • bz2 - application/x-bzip2
  • 7z - application/x-7z-compressed
  • xz - application/x-xz
  • zstd - application/zstd
  • pdf - application/pdf
  • exe - application/vnd.microsoft.portable-executable
  • swf - application/x-shockwave-flash
  • rtf - application/rtf
  • iso - application/x-iso9660-image
  • eot - application/octet-stream
  • ps - application/postscript
  • sqlite - application/vnd.sqlite3
  • nes - application/x-nintendo-nes-rom
  • crx - application/x-google-chrome-extension
  • cab - application/vnd.ms-cab-compressed
  • deb - application/vnd.debian.binary-package
  • ar - application/x-unix-archive
  • Z - application/x-compress
  • lz - application/x-lzip
  • rpm - application/x-rpm
  • elf - application/x-executable
  • dcm - application/dicom

Documents

  • doc - application/msword
  • docx - application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • xls - application/vnd.ms-excel
  • xlsx - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • ppt - application/vnd.ms-powerpoint
  • pptx - application/vnd.openxmlformats-officedocument.presentationml.presentation

Font

  • woff - application/font-woff
  • woff2 - application/font-woff
  • ttf - application/font-sfnt
  • otf - application/font-sfnt

Application

  • wasm - application/wasm
  • dex - application/vnd.android.dex
  • dey - application/vnd.android.dey

Benchmarks

Measured using real files.

Environment: OSX x64 i7 2.7 Ghz

BenchmarkMatchTar-8    1000000        1083 ns/op
BenchmarkMatchZip-8    1000000        1162 ns/op
BenchmarkMatchJpeg-8   1000000        1280 ns/op
BenchmarkMatchGif-8    1000000        1315 ns/op
BenchmarkMatchPng-8    1000000        1121 ns/op

License

MIT - Tomas Aparicio

NPM DownloadsLast 30 Days