mime

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0, MIT Imports: 7 Imported by: 0

README

MIME

Port from https://github.com/gabriel-vasile/mimetype

主要改进:浏览器安全。

Documentation

Overview

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

File formats are stored in a hierarchy with application/octet-stream at its root. For example, the hierarchy for HTML format is application/octet-stream -> text/plain -> text/html.

Index

Constants

This section is empty.

Variables

View Source
var (
	// These types are excluded from the logic that allows all text/ types because
	// while they are technically text, it's very unlikely that a user expects to
	// see them rendered in text form.
	UnsupportedTextTypes = []string{
		"text/calendar",
		"text/x-calendar",
		"text/x-vcalendar",
		"text/vcalendar",
		"text/vcard",
		"text/x-vcard",
		"text/directory",
		"text/ldif",
		"text/qif",
		"text/x-qif",
		"text/x-csv",
		"text/x-vcf",
		"text/rtf",
		"text/comma-separated-values",
		"text/csv",
		"text/tab-separated-values",
		"text/tsv",
		"text/ofx",
		"text/vnd.sun.j2me.app-descriptor",
		"text/x-ms-iqy",
		"text/x-ms-odc",
		"text/x-ms-rqy",
		"text/x-ms-contact",
	}
	SupportedNonImageTypes = []string{
		"image/svg+xml",

		"application/xml", "application/atom+xml", "application/rss+xml",
		"application/xhtml+xml", "application/json",
		"message/rfc822",
		"multipart/related",
		"multipart/x-mixed-replace",
	}
)

https://github.com/chromium/chromium/blob/main/third_party/blink/common/mime_util/mime_util.cc

Functions

func EqualsAny

func EqualsAny(s string, mimes ...string) bool

EqualsAny reports whether s MIME type is equal to any MIME type in mimes. MIME type equality test is done on the "type/subtype" section, ignores any optional MIME parameters, ignores any leading and trailing whitespace, and is case insensitive.

func Extend

func Extend(detector func(raw []byte, limit uint32) bool, mime, extension string, aliases ...string)

Extend adds detection for other file formats. It is equivalent to calling Extend() on the root mime type "application/octet-stream".

func SetLimit

func SetLimit(limit uint32)

SetLimit sets the maximum number of bytes read from input when detecting the MIME type. Increasing the limit provides better detection for file formats which store their magical numbers towards the end of the file: docx, pptx, xlsx, etc. During detection data is read in a single block of size limit, i.e. it is not buffered. A limit of 0 means the whole input file will be used.

Types

type MIME

type MIME struct {
	// contains filtered or unexported fields
}

MIME struct holds information about a file format: the string representation of the MIME type, the extension and the parent file format.

func Detect

func Detect(in []byte) *MIME

Detect returns the MIME type found from the provided byte slice.

The result is always a valid MIME type, with application/octet-stream returned when identification failed.

func DetectAny

func DetectAny(in []byte) *MIME

func DetectFile

func DetectFile(path string) (*MIME, error)

DetectFile returns the MIME type of the provided file.

The result is always a valid MIME type, with application/octet-stream returned when identification failed with or without an error. Any error returned is related to the opening and reading from the input file.

func DetectReader

func DetectReader(r io.Reader) (*MIME, error)

DetectReader returns the MIME type of the provided reader.

The result is always a valid MIME type, with application/octet-stream returned when identification failed with or without an error. Any error returned is related to the reading from the input reader.

DetectReader assumes the reader offset is at the start. If the input is an io.ReadSeeker you previously read from, it should be rewinded before detection:

reader.Seek(0, io.SeekStart)

func Lookup

func Lookup(mime string) *MIME

Lookup finds a MIME object by its string representation. The representation can be the main mime type, or any of its aliases.

func (*MIME) Extend

func (m *MIME) Extend(detector func(raw []byte, limit uint32) bool, mime, extension string, aliases ...string)

Extend adds detection for a sub-format. The detector is a function returning true when the raw input file satisfies a signature. The sub-format will be detected if all the detectors in the parent chain return true. The extension should include the leading dot, as in ".html".

func (*MIME) Extension

func (m *MIME) Extension() string

Extension returns the file extension associated with the MIME type. It includes the leading dot, as in ".html". When the file format does not have an extension, the empty string is returned.

func (*MIME) Flatten

func (m *MIME) Flatten() []*MIME

flatten transforms an hierarchy of MIMEs into a slice of MIMEs.

func (*MIME) Is

func (m *MIME) Is(expectedMIME string) bool

Is checks whether this MIME type, or any of its aliases, is equal to the expected MIME type. MIME type equality test is done on the "type/subtype" section, ignores any optional MIME parameters, ignores any leading and trailing whitespace, and is case insensitive.

func (*MIME) Parent

func (m *MIME) Parent() *MIME

Parent returns the parent MIME type from the hierarchy. Each MIME type has a non-nil parent, except for the root MIME type.

For example, the application/json and text/html MIME types have text/plain as their parent because they are text files who happen to contain JSON or HTML. Another example is the ZIP format, which is used as container for Microsoft Office files, EPUB files, JAR files, and others.

func (*MIME) Sanitize

func (m *MIME) Sanitize() string

func (*MIME) String

func (m *MIME) String() string

String returns the string representation of the MIME type, e.g., "application/zip".

Directories

Path Synopsis
internal
json
Package json provides a JSON value parser state machine.
Package json provides a JSON value parser state machine.
magic
Package magic holds the matching functions used to find MIME types.
Package magic 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