Documentation
¶
Overview ¶
Package mimetype uses magic number signatures to detect the MIME type of a file.
mimetype stores the list of MIME types in a tree structure with "application/octet-stream" at the root of the hierarchy. The hierarchy approach minimizes the number of checks that need to be done on the input and allows for more precise results once the base type of file has been identified.
Example (Check) ¶
To check if some bytes/reader/file has a specific MIME type, first perform a detect on the input and then test against the MIME.
Is method can also be called with MIME aliases.
package main
import (
"fmt"
"github.com/gabriel-vasile/mimetype"
)
func main() {
mime, err := mimetype.DetectFile("testdata/zip.zip")
// application/x-zip is an alias of application/zip,
// therefore Is returns true both times.
fmt.Println(mime.Is("application/zip"), mime.Is("application/x-zip"), err)
}
Output: true true <nil>
Example (Detect) ¶
To find the MIME type of some input, perform a detect. In addition to the basic Detect,
mimetype.Detect([]byte) *MIME
there are shortcuts for detecting from a reader:
mimetype.DetectReader(io.Reader) (*MIME, error)
or from a file:
mimetype.DetectFile(string) (*MIME, error)
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/gabriel-vasile/mimetype"
)
func main() {
file := "testdata/pdf.pdf"
// Detect the MIME type of a file stored as a byte slice.
data, _ := ioutil.ReadFile(file) // ignoring error for brevity's sake
mime := mimetype.Detect(data)
fmt.Println(mime)
// Detect the MIME type of a reader.
reader, _ := os.Open(file) // ignoring error for brevity's sake
mime, rerr := mimetype.DetectReader(reader)
fmt.Println(mime, rerr)
// Detect the MIME type of a file.
mime, ferr := mimetype.DetectFile(file)
fmt.Println(mime, ferr)
}
Output: application/pdf application/pdf <nil> application/pdf <nil>
Example (Parent) ¶
To check if some bytes/reader/file has a base MIME type, first perform a detect on the input and then navigate the parents until the base MIME type is found.
Considering JAR files are just ZIPs containing some metadata files, if, for example, you need to tell if the input can be unzipped, go up the hierarchy until zip is found or the root is reached.
package main
import (
"fmt"
"github.com/gabriel-vasile/mimetype"
)
func main() {
detectedMIME, err := mimetype.DetectFile("testdata/jar.jar")
zip := false
for mime := detectedMIME; mime != nil; mime = mime.Parent() {
if mime.Is("application/zip") {
zip = true
}
}
// zip is true, even if the detected MIME was application/jar.
fmt.Println(zip, detectedMIME, err)
}
Output: true application/jar <nil>
Example (TextVsBinary) ¶
Considering the definition of a binary file as "a computer file that is not a text file", they can differentiated by searching for the text/plain MIME in it's MIME hierarchy.
package main
import (
"fmt"
"github.com/gabriel-vasile/mimetype"
)
func main() {
detectedMIME, err := mimetype.DetectFile("testdata/xml.xml")
isBinary := true
for mime := detectedMIME; mime != nil; mime = mime.Parent() {
if mime.Is("text/plain") {
isBinary = false
}
}
fmt.Println(isBinary, detectedMIME, err)
}
Output: false text/xml; charset=utf-8 <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MIME ¶ added in v1.0.0
type MIME struct {
// contains filtered or unexported fields
}
MIME represents a file format in the tree structure of formats.
func Detect ¶
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.
Example ¶
package main
import (
"fmt"
"io/ioutil"
"github.com/gabriel-vasile/mimetype"
)
func main() {
data, err := ioutil.ReadFile("testdata/zip.zip")
mime := mimetype.Detect(data)
fmt.Println(mime, err)
}
Output: application/zip <nil>
func DetectFile ¶
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.
To prevent loading entire files into memory, DetectFile reads at most matchers.ReadLimit bytes from the reader.
Example ¶
package main
import (
"fmt"
"github.com/gabriel-vasile/mimetype"
)
func main() {
mime, err := mimetype.DetectFile("testdata/zip.zip")
fmt.Println(mime, err)
}
Output: application/zip <nil>
func DetectReader ¶
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 a ReadSeeker you read from before, it should be rewinded before detection:
reader.Seek(0, io.SeekStart)
To prevent loading entire files into memory, DetectReader reads at most matchers.ReadLimit bytes from the reader.
Example ¶
package main
import (
"fmt"
"os"
"github.com/gabriel-vasile/mimetype"
)
func main() {
data, oerr := os.Open("testdata/zip.zip")
mime, merr := mimetype.DetectReader(data)
fmt.Println(mime, oerr, merr)
}
Output: application/zip <nil> <nil>
func (*MIME) Extension ¶ added in v1.0.0
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) Is ¶ added in v1.0.0
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" sections, ignores any optional MIME parameters, ignores any leading and trailing whitespace, and is case insensitive.
Example ¶
package main
import (
"fmt"
"github.com/gabriel-vasile/mimetype"
)
func main() {
mime, err := mimetype.DetectFile("testdata/pdf.pdf")
pdf := mime.Is("application/pdf")
xpdf := mime.Is("application/x-pdf")
txt := mime.Is("text/plain")
fmt.Println(pdf, xpdf, txt, err)
}
Output: true true false <nil>
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. |