Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for detecting avif files #160

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/magic/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ var (
Xpm = prefix([]byte{0x2F, 0x2A, 0x20, 0x58, 0x50, 0x4D, 0x20, 0x2A, 0x2F})
)

func Avif(raw []byte, limit uint32) bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AVIF format is using the FTYP container format so you can use the existing ftyp function to create the detector. Like it's done for other FTYP formats in ftyp.go. The index where avif should be searched for is 8:12, not 20:24.

Also, it seems like image/avif should be returned for avif (single image) and avis (image sequence): reference

if len(raw) < 24 {
return false
}
return bytes.Equal(raw[4:8], []byte("ftyp")) &&
bytes.Equal(raw[20:24], []byte("avif"))
}

func jpeg2k(sig []byte) Detector {
return func(raw []byte, _ uint32) bool {
if len(raw) < 24 {
Expand Down
1 change: 1 addition & 0 deletions mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var files = map[string]string{
"atom.atom": "application/atom+xml",
"au.au": "audio/basic",
"avi.avi": "video/x-msvideo",
"avif.avif": "image/avif",
"bmp.bmp": "image/bmp",
"bpg.bpg": "image/bpg",
"bz2.bz2": "application/x-bzip2",
Expand Down
Binary file added testdata/avif.avif
Binary file not shown.
3 changes: 2 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var root = newMIME("application/octet-stream", "",
midi, ape, musePack, amr, wav, aiff, au, mpeg, quickTime, mqv, mp4, webM,
threeGP, threeG2, avi, flv, mkv, asf, aac, voc, aMp4, m4a, m3u, m4v, rmvb,
gzip, class, swf, crx, ttf, woff, woff2, otf, eot, wasm, shx, dbf, dcm, rar,
djvu, mobi, lit, bpg, sqlite3, dwg, nes, lnk, macho, qcp, icns, heic,
djvu, mobi, lit, bpg, sqlite3, dwg, nes, lnk, macho, qcp, icns, avif, heic,
heicSeq, heif, heifSeq, hdr, mrc, mdb, accdb, zstd, cab, rpm, xz, lzip,
torrent, cpio, tzif, xcf, pat, gbr, glb,
// Keep text last because it is the slowest check
Expand Down Expand Up @@ -115,6 +115,7 @@ var (
icns = newMIME("image/x-icns", ".icns", magic.Icns)
psd = newMIME("image/vnd.adobe.photoshop", ".psd", magic.Psd).
alias("image/x-psd", "application/photoshop")
avif = newMIME("image/avif", ".avif", magic.Avif)
heic = newMIME("image/heic", ".heic", magic.Heic)
heicSeq = newMIME("image/heic-sequence", ".heic", magic.HeicSequence)
heif = newMIME("image/heif", ".heif", magic.Heif)
Expand Down