|
5 | 5 | "io" |
6 | 6 |
|
7 | 7 | "github.com/h2non/filetype" |
| 8 | + "github.com/h2non/go-is-svg" |
8 | 9 |
|
9 | 10 | "github.com/operator-framework/operator-registry/alpha/declcfg" |
10 | 11 | ) |
@@ -36,16 +37,31 @@ func (i Init) Run() (*declcfg.Package, error) { |
36 | 37 | if err != nil { |
37 | 38 | return nil, fmt.Errorf("read icon: %v", err) |
38 | 39 | } |
| 40 | + |
| 41 | + // Try filetype detection first |
39 | 42 | iconType, err := filetype.Match(iconData) |
40 | 43 | if err != nil { |
41 | 44 | return nil, fmt.Errorf("detect icon mediatype: %v", err) |
42 | 45 | } |
43 | | - if iconType.MIME.Type != "image" { |
44 | | - return nil, fmt.Errorf("detected invalid type %q: not an image", iconType.MIME.Value) |
| 46 | + |
| 47 | + var mediaType string |
| 48 | + // If filetype didn't detect it, check if it's SVG |
| 49 | + if iconType.MIME.Value == "" { |
| 50 | + if issvg.Is(iconData) { |
| 51 | + mediaType = "image/svg+xml" |
| 52 | + } else { |
| 53 | + return nil, fmt.Errorf("detected invalid type %q: not an image", iconType.MIME.Value) |
| 54 | + } |
| 55 | + } else { |
| 56 | + if iconType.MIME.Type != "image" { |
| 57 | + return nil, fmt.Errorf("detected invalid type %q: not an image", iconType.MIME.Value) |
| 58 | + } |
| 59 | + mediaType = iconType.MIME.Value |
45 | 60 | } |
| 61 | + |
46 | 62 | pkg.Icon = &declcfg.Icon{ |
47 | 63 | Data: iconData, |
48 | | - MediaType: iconType.MIME.Value, |
| 64 | + MediaType: mediaType, |
49 | 65 | } |
50 | 66 | } |
51 | 67 | return pkg, nil |
|
0 commit comments