Skip to content

Commit

Permalink
Use existing ftyp sig type for heic and heif detection
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Jan 21, 2020
1 parent eda792d commit af152f1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 59 deletions.
23 changes: 13 additions & 10 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion internal/matchers/font.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package matchers

import "bytes"
import (
"bytes"
)

// Woff matches a Web Open Font Format file.
func Woff(in []byte) bool {
Expand Down
34 changes: 31 additions & 3 deletions internal/matchers/video_ftyp.go → internal/matchers/ftyp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
)

Expand Down Expand Up @@ -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)
}
44 changes: 0 additions & 44 deletions internal/matchers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

0 comments on commit af152f1

Please sign in to comment.