package mimetype
import "github.com/gabriel-vasile/mimetype"
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.
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` also works with
MIME aliases.
Code:play
Output: To find the MIME type of some bytes/reader/file, perform a detect on the input.
Code:play
Output: 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.
Code:play
Output:Example (Check)¶
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)
}
true true <nil>
Example (Detect)¶
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/gabriel-vasile/mimetype"
)
func main() {
file := "testdata/pdf.pdf"
reader, _ := os.Open(file)
data, _ := ioutil.ReadFile(file)
dmime := mimetype.Detect(data)
rmime, rerr := mimetype.DetectReader(reader)
fmime, ferr := mimetype.DetectFile(file)
fmt.Println(dmime, rmime, fmime)
fmt.Println(rerr, ferr)
}
application/pdf application/pdf application/pdf
<nil> <nil>
Example (Parent)¶
package main
import (
"fmt"
"github.com/gabriel-vasile/mimetype"
)
func main() {
// Ex: if you are interested in text/plain and all of its subtypes:
// text/html, text/xml, text/csv, etc.
mime, err := mimetype.DetectFile("testdata/html.html")
isText := false
for ; mime != nil; mime = mime.Parent() {
if mime.Is("text/plain") {
isText = true
}
}
// isText is true, even if the detected MIME was text/html.
fmt.Println(isText, err)
}
true <nil>
Index ¶
Examples ¶
Types ¶
type MIME ¶
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.
Failure to identify the format results in application/octet-stream being returned.
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.
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.
func (*MIME) Extension ¶
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 ¶
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.
Code:play
Output: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)
}
true true false <nil>
func (*MIME) Parent ¶
Parent returns the parent MIME type from the tree structure. Each MIME type has a non-nil parent, except for the root MIME type.
func (*MIME) String ¶
String returns the string representation of the MIME type, e.g., "application/zip".
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
internal |
- Version
- v1.0.0
- Published
- Dec 11, 2019
- Platform
- darwin/amd64
- Imports
- 4 packages
- Last checked
- 2 hours ago –
Tools for package owners.