diff --git a/pkg/magic/magic.go b/pkg/magic/magic.go index 0edf01654..2a0744ed8 100644 --- a/pkg/magic/magic.go +++ b/pkg/magic/magic.go @@ -30,6 +30,8 @@ type prefixEntry struct { mtype string } +// usable source: http://www.garykessler.net/library/file_sigs.html +// mime types: http://www.iana.org/assignments/media-types/media-types.xhtml var prefixTable = []prefixEntry{ {[]byte("GIF87a"), "image/gif"}, {[]byte("GIF89a"), "image/gif"}, // TODO: Others? @@ -39,8 +41,33 @@ var prefixTable = []prefixEntry{ {[]byte("\xff\xd8\xff\xdb"), "image/jpeg"}, {[]byte("\x49\x49\x2a\x00\x10\x00\x00\x00\x43\x52\x02"), "image/cr2"}, {[]byte{137, 'P', 'N', 'G', '\r', '\n', 26, 10}, "image/png"}, + {[]byte{0x49, 0x20, 0x49}, "image/tiff"}, + {[]byte{0x49, 0x49, 0x2A, 0}, "image/tiff"}, + {[]byte{0x4D, 0x4D, 0, 0x2A}, "image/tiff"}, + {[]byte{0x4D, 0x4D, 0, 0x2B}, "image/tiff"}, + {[]byte("8BPS"), "image/vnd.adobe.photoshop"}, + {[]byte("gimp xcf "), "image/xcf"}, {[]byte("-----BEGIN PGP PUBLIC KEY BLOCK---"), "text/x-openpgp-public-key"}, + {[]byte("fLaC\x00\x00\x00"), "audio/flac"}, {[]byte{'I', 'D', '3'}, "audio/mpeg"}, + {[]byte{0, 0, 1, 0xB7}, "video/mpeg"}, + {[]byte{0, 0, 0, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20}, "video/quicktime"}, + {[]byte{0, 0x6E, 0x1E, 0xF0}, "application/vnd.ms-powerpoint"}, + {[]byte{0x1A, 0x45, 0xDF, 0xA3}, "video/webm"}, + {[]byte("FLV\x01"), "application/vnd.adobe.flash.video"}, + {[]byte{0x1F, 0x8B, 0x08}, "application/gzip"}, + {[]byte{0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C}, "application/x-7z-compressed"}, + {[]byte("BZh"), "application/bzip2"}, + {[]byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0}, "application/x-xz"}, + {[]byte{'P', 'K', 3, 4, 0x0A, 0, 2, 0}, "application/epub+zip"}, + {[]byte{0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1}, "application/vnd.ms-word"}, + {[]byte{'P', 'K', 3, 4, 0x0A, 0x14, 0, 6, 0}, "application/vnd.openxmlformats-officedocument.custom-properties+xml"}, + {[]byte{'P', 'K', 3, 4}, "application/zip"}, + {[]byte("%PDF"), "application/pdf"}, + {[]byte("{rtf"), "text/rtf1"}, + {[]byte("BEGIN:VCARD\x0D\x0A"), "text/vcard"}, + {[]byte("Return-Path: "), "message/rfc822"}, + // TODO(bradfitz): popular audio & video formats at least } diff --git a/pkg/magic/magic_test.go b/pkg/magic/magic_test.go index e81a7f3d3..140ced336 100644 --- a/pkg/magic/magic_test.go +++ b/pkg/magic/magic_test.go @@ -31,6 +31,15 @@ type magicTest struct { var tests = []magicTest{ {fileName: "smile.jpg", want: "image/jpeg"}, {fileName: "smile.png", want: "image/png"}, + {fileName: "smile.psd", want: "image/vnd.adobe.photoshop"}, + {fileName: "smile.tiff", want: "image/tiff"}, + {fileName: "smile.xcf", want: "image/xcf"}, + {fileName: "smile.gif", want: "image/gif"}, + {fileName: "foo.tar.gz", want: "application/gzip"}, + {fileName: "foo.tar.xz", want: "application/x-xz"}, + {fileName: "foo.tbz2", want: "application/bzip2"}, + {fileName: "foo.zip", want: "application/zip"}, + {fileName: "magic.pdf", want: "application/pdf"}, {data: "foo", want: "text/html"}, {data: "\xff", want: ""}, } diff --git a/pkg/magic/testdata/foo.tar.xz b/pkg/magic/testdata/foo.tar.xz new file mode 100644 index 000000000..bfcceda15 Binary files /dev/null and b/pkg/magic/testdata/foo.tar.xz differ diff --git a/pkg/magic/testdata/foo.tbz2 b/pkg/magic/testdata/foo.tbz2 new file mode 100644 index 000000000..a6acd0406 Binary files /dev/null and b/pkg/magic/testdata/foo.tbz2 differ diff --git a/pkg/magic/testdata/magic.pdf b/pkg/magic/testdata/magic.pdf new file mode 100644 index 000000000..0e9340c04 Binary files /dev/null and b/pkg/magic/testdata/magic.pdf differ