mimetype

package module
v0.3.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2019 License: MIT Imports: 3 Imported by: 829

README

mimetype

A package for detecting MIME types and extensions based on magic numbers

No bindings, thread safe and completely written in go

Build Status Documentation Go report card Go report card License

Install

go get github.com/gabriel-vasile/mimetype

Use

The library exposes three functions you can use in order to detect a file type. See Godoc for full reference.

func Detect(in []byte) (mime, extension string) {...}
func DetectReader(r io.Reader) (mime, extension string, err error) {...}
func DetectFile(file string) (mime, extension string, err error) {...}

When detecting from a ReadSeeker interface, such as os.File, make sure to reset the offset of the reader to the beginning if needed:

_, err = file.Seek(0, io.SeekStart)

Supported MIME types

See supported mimes for the list of detected MIME types. If support is needed for a specific file format, please open an issue.

Structure

mimetype uses an hierarchical structure to keep the matching functions. This reduces the number of calls needed for detecting the file type. The reason behind this choice is that there are file formats used as containers for other file formats. For example, Microsoft office files are just zip archives, containing specific metadata files.

structure

Contributing

See CONTRIBUTING.md.

Documentation

Overview

Package mimetype uses magic number signatures to detect the MIME type and extension of a file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Detect

func Detect(in []byte) (mime, extension string)

Detect returns the MIME type and extension of the provided byte slice.

mime is always a valid MIME type, with application/octet-stream as fallback. extension is empty string if detected file format does not have an extension.

func DetectFile

func DetectFile(file string) (mime, extension string, err error)

DetectFile returns the MIME type and extension of the provided file.

mime is always a valid MIME type, with application/octet-stream as fallback. extension is empty string if detection failed with an error or detected file format does not have an extension.

func DetectReader

func DetectReader(r io.Reader) (mime, extension string, err error)

DetectReader returns the MIME type and extension of the byte slice read from the provided reader.

mime is always a valid MIME type, with application/octet-stream as fallback. extension is empty string if detection failed with an error or detected file format does not have an extension.

Types

This section is empty.

Directories

Path Synopsis
internal
json
Package json provides a JSON value parser state machine.
Package json provides a JSON value parser state machine.
matchers
Package matchers holds the matching functions used to find mime types.
Package matchers holds the matching functions used to find mime types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL