diff --git a/EXAMPLES.md b/EXAMPLES.md index 636784fc..380c32a6 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -5,21 +5,24 @@ - [Binary file vs text file](#binary-file-vs-text-file) ### Detect -Get the MIME type from a slice of bytes, from a reader and from a file. +Get the MIME type from a path to a file. ```go -// Detect the MIME type of a file stored as a byte slice. file := "testdata/pdf.pdf" -// Detect the MIME type of a file. -mime, ferr := mimetype.DetectFile(file) -fmt.Println(mime, ferr) +mime, err := mimetype.DetectFile(file) +fmt.Println(mime, err) // Output: application/pdf nil - -// 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) +``` +Get the MIME type from a reader. +```go +reader, _ := os.Open(file) // ignoring error for brevity's sake +mime, err := mimetype.DetectReader(reader) +fmt.Println(mime, err) // Output: application/pdf nil +``` +Get the MIME type from a byte slice. +```go +data, _ := ioutil.ReadFile(file) // ignoring error for brevity's sake mime := mimetype.Detect(data) fmt.Println(mime) // Output: application/pdf diff --git a/LICENSE b/LICENSE index f1b456e9..6aac070c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018, 2019 Gabriel Vasile +Copyright (c) 2018-2020 Gabriel Vasile Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/internal/matchers/font.go b/internal/matchers/font.go index 11ae659d..a07bd13a 100644 --- a/internal/matchers/font.go +++ b/internal/matchers/font.go @@ -1,6 +1,8 @@ package matchers -import "bytes" +import ( + "bytes" +) // Woff matches a Web Open Font Format file. func Woff(in []byte) bool { diff --git a/internal/matchers/video_ftyp.go b/internal/matchers/ftyp.go similarity index 66% rename from internal/matchers/video_ftyp.go rename to internal/matchers/ftyp.go index 51eb8564..d658d9d0 100644 --- a/internal/matchers/video_ftyp.go +++ b/internal/matchers/ftyp.go @@ -29,9 +29,17 @@ var ( // Nero Digital AAC Audio ftypSig("NDAS"), } - qtSigs = []sig{ftypSig("qt "), ftypSig("moov")} - mqvSigs = []sig{ftypSig("mqt ")} - m4aSigs = []sig{ftypSig("M4A ")} + qtSigs = []sig{ftypSig("qt "), ftypSig("moov")} + mqvSigs = []sig{ftypSig("mqt ")} + m4aSigs = []sig{ftypSig("M4A ")} + heicSigs = []sig{ftypSig("heic"), ftypSig("heix")} + heicSeqSigs = []sig{ftypSig("hevc"), ftypSig("hevx")} + heifSigs = []sig{ + ftypSig("mif1"), ftypSig("heim"), ftypSig("heis"), ftypSig("avic"), + } + heifSeqSigs = []sig{ + ftypSig("msf1"), ftypSig("hevm"), ftypSig("hevs"), ftypSig("avcs"), + } // TODO: add support for remaining video formats at ftyps.com. ) @@ -69,3 +77,23 @@ func Mqv(in []byte) bool { func M4a(in []byte) bool { return detect(in, m4aSigs) } + +// Heic matches a High Efficiency Image Coding (HEIC) file. +func Heic(in []byte) bool { + return detect(in, heicSigs) +} + +// HeicSequence matches a High Efficiency Image Coding (HEIC) file sequence. +func HeicSequence(in []byte) bool { + return detect(in, heicSeqSigs) +} + +// Heif matches a High Efficiency Image File Format (HEIF) file. +func Heif(in []byte) bool { + return detect(in, heifSigs) +} + +// HeifSequence matches a High Efficiency Image File Format (HEIF) file sequence. +func HeifSequence(in []byte) bool { + return detect(in, heifSeqSigs) +} diff --git a/internal/matchers/image.go b/internal/matchers/image.go index b4309d45..c773415e 100644 --- a/internal/matchers/image.go +++ b/internal/matchers/image.go @@ -118,47 +118,3 @@ func Dwg(in []byte) bool { return false } - -// Heic matches a High Efficiency Image Coding (HEIC) file. -func Heic(in []byte) bool { - if len(in) <= 12 { - return false - } - - return bytes.Equal(in[4:12], []byte("ftypheic")) || - bytes.Equal(in[4:12], []byte("ftypheix")) -} - -// HeicSequence matches a High Efficiency Image Coding (HEIC) file sequence. -func HeicSequence(in []byte) bool { - if len(in) <= 12 { - return false - } - - return bytes.Equal(in[4:12], []byte("ftyphevc")) || - bytes.Equal(in[4:12], []byte("ftyphevx")) -} - -// Heif matches a High Efficiency Image File Format (HEIF) file. -func Heif(in []byte) bool { - if len(in) <= 12 { - return false - } - - return bytes.Equal(in[4:12], []byte("ftypmif1")) || - bytes.Equal(in[4:12], []byte("ftypheim")) || - bytes.Equal(in[4:12], []byte("ftypheis")) || - bytes.Equal(in[4:12], []byte("ftypavic")) -} - -// HeifSequence matches a High Efficiency Image File Format (HEIF) file sequence. -func HeifSequence(in []byte) bool { - if len(in) <= 12 { - return false - } - - return bytes.Equal(in[4:12], []byte("ftypmsf1")) || - bytes.Equal(in[4:12], []byte("ftyphevm")) || - bytes.Equal(in[4:12], []byte("ftyphevs")) || - bytes.Equal(in[4:12], []byte("ftypavcs")) -}