|
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 | ) |
@@ -32,21 +33,44 @@ func (i Init) Run() (*declcfg.Package, error) { |
32 | 33 | } |
33 | 34 |
|
34 | 35 | if i.IconReader != nil { |
35 | | - iconData, err := io.ReadAll(i.IconReader) |
| 36 | + icon, err := processIcon(i.IconReader) |
36 | 37 | if err != nil { |
37 | | - return nil, fmt.Errorf("read icon: %v", err) |
| 38 | + return nil, err |
38 | 39 | } |
39 | | - iconType, err := filetype.Match(iconData) |
40 | | - if err != nil { |
41 | | - return nil, fmt.Errorf("detect icon mediatype: %v", err) |
| 40 | + pkg.Icon = icon |
| 41 | + } |
| 42 | + return pkg, nil |
| 43 | +} |
| 44 | + |
| 45 | +func processIcon(iconReader io.Reader) (*declcfg.Icon, error) { |
| 46 | + iconData, err := io.ReadAll(iconReader) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("read icon: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Try filetype detection first |
| 52 | + iconType, err := filetype.Match(iconData) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("detect icon mediatype: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + var mediaType string |
| 58 | + // If filetype didn't detect it, check if it's SVG |
| 59 | + if iconType.MIME.Value == "" { |
| 60 | + if issvg.Is(iconData) { |
| 61 | + mediaType = "image/svg+xml" |
| 62 | + } else { |
| 63 | + return nil, fmt.Errorf("detected invalid type %q: not an image", iconType.MIME.Value) |
42 | 64 | } |
| 65 | + } else { |
43 | 66 | if iconType.MIME.Type != "image" { |
44 | 67 | return nil, fmt.Errorf("detected invalid type %q: not an image", iconType.MIME.Value) |
45 | 68 | } |
46 | | - pkg.Icon = &declcfg.Icon{ |
47 | | - Data: iconData, |
48 | | - MediaType: iconType.MIME.Value, |
49 | | - } |
| 69 | + mediaType = iconType.MIME.Value |
50 | 70 | } |
51 | | - return pkg, nil |
| 71 | + |
| 72 | + return &declcfg.Icon{ |
| 73 | + Data: iconData, |
| 74 | + MediaType: mediaType, |
| 75 | + }, nil |
52 | 76 | } |
0 commit comments