| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "path" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // rasterImageType geeft het image-content-type als de extensie een veilig |
| 9 | // rasterformaat is én de eerste bytes daarbij passen; anders "". |
| 10 | func rasterImageType(name, head string) string { |
| 11 | ext := strings.ToLower(path.Ext(name)) |
| 12 | ok := false |
| 13 | switch ext { |
| 14 | case ".png": |
| 15 | ok = strings.HasPrefix(head, "\x89PNG\r\n\x1a\n") |
| 16 | case ".jpg", ".jpeg": |
| 17 | ok = strings.HasPrefix(head, "\xff\xd8\xff") |
| 18 | case ".gif": |
| 19 | ok = strings.HasPrefix(head, "GIF87a") || strings.HasPrefix(head, "GIF89a") |
| 20 | case ".webp": |
| 21 | ok = len(head) >= 12 && head[:4] == "RIFF" && head[8:12] == "WEBP" |
| 22 | case ".avif": |
| 23 | ok = len(head) >= 12 && head[4:8] == "ftyp" && (head[8:12] == "avif" || head[8:12] == "avis") |
| 24 | case ".bmp": |
| 25 | ok = strings.HasPrefix(head, "BM") |
| 26 | case ".ico": |
| 27 | ok = strings.HasPrefix(head, "\x00\x00\x01\x00") |
| 28 | } |
| 29 | if !ok { |
| 30 | return "" |
| 31 | } |
| 32 | return rawImageTypes[ext] |
| 33 | } |