webp-sh / webp_server_go

Go version of WebP Server. A tool that will serve your JPG/PNG/BMP/SVGs as WebP/AVIF format with compression, on-the-fly.
https://docs.webp.sh
GNU General Public License v3.0
1.79k stars 174 forks source link

should use filetype #243

Closed BennyThink closed 1 year ago

BennyThink commented 1 year ago
func avifMatcher(buf []byte) bool {
    // use hexdump on macOS to see the magic number
    // 0000001c 66747970 61766966 00000000 61766966 6d696631 6d696166
    magicHeader := []byte{
        0x0, 0x0, 0x0, 0x1c,
        0x66, 0x74, 0x79, 0x70,
        0x61, 0x76, 0x69, 0x66,
        0x0, 0x0, 0x0, 0x0,
        0x61, 0x76, 0x69, 0x66,
        0x6d, 0x69, 0x66, 0x31,
        0x6d, 0x69, 0x61, 0x66,
    }

    return len(buf) > 1 && bytes.Equal(buf[:28], magicHeader) || strings.Contains(string(buf), "ftypavif")
}

func GetFileContentType(buffer []byte) string {
    filetype.AddMatcher(filetype.NewType("avif", "image/avif"), avifMatcher)
    kind, _ := filetype.Match(buffer)
    return kind.MIME.Value
}

https://github.com/webp-sh/webp_server_go/blob/77584df0ea50cc0e57c5377745554cb4a4d9d85e/handler/router.go#L98