/file-type-stream-go

Detect file types–extensions and mime–using magic numbers

Primary LanguageGoOtherNOASSERTION

file-type-stream-go

Go Reference

This library is used to detect the type of a binary file by looking at "magic numbers". It is optimized to work with streams, efficiently reading only a small number of bytes in most cases.

This is a port of sindresorhus/file-type to Go.

The current version of this package is based on version 17.1.3 of file-type.

Examples

Usage

To use this library, add it to your project with:

go get github.com/italypaleale/file-type-stream-go

You can then import it by adding this to your Go files' imports:

import "github.com/italypaleale/file-type-stream-go/pkg/filetype"

APIs

Full API reference

The main package (github.com/italypaleale/file-type-stream-go/pkg/filetype) contains 3 main methods:

  • Use the ParseFile method to detect the type of a file on disk:

    ext, mime, err := filetype.ParseFile("path/to/file")
  • Use the ParseBytes method to detect the type of a file contained in a byte slice:

    ext, mime, err := filetype.ParseFile([]byte{/* … */})
  • Use the ParseStream method to detect the type of a file from a readable stream:

    var r io.Reader = /* … */
    ext, mime, err := filetype.ParseStream(r)