-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
controller.go
183 lines (163 loc) · 3.94 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package icon
import (
"bytes"
"context"
"encoding/base64"
"image"
// import the gif format
_ "image/gif"
// import the jpeg format
_ "image/jpeg"
"image/png"
"io"
"os"
"sync"
"github.com/nfnt/resize"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/icon"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg"
"github.com/goharbor/harbor/src/pkg/artifact"
"github.com/goharbor/harbor/src/pkg/registry"
)
var (
builtInIcons = map[string]buildInIcon{
icon.DigestOfIconImage: {
path: "./icons/image.png",
resize: true,
},
icon.DigestOfIconChart: {
path: "./icons/chart.png",
resize: true,
},
icon.DigestOfIconCNAB: {
path: "./icons/cnab.png",
resize: true,
},
icon.DigestOfIconAccCosign: {
path: "./icons/cosign.png",
resize: false,
},
icon.DigestOfIconAccNotation: {
path: "./icons/notation.png",
resize: false,
},
icon.DigestOfIconAccNydus: {
path: "./icons/nydus.png",
resize: false,
},
icon.DigestOfIconWASM: {
path: "./icons/wasm.png",
resize: true,
},
icon.DigestOfIconAccSBOM: {
path: "./icons/sbom.png",
resize: true,
},
icon.DigestOfIconDefault: {
path: "./icons/default.png",
resize: true,
},
}
// Ctl is a global icon controller instance
Ctl = NewController()
)
type buildInIcon struct {
path string
resize bool
}
// Icon model for artifact icon
type Icon struct {
ContentType string
Content string // base64 encoded
}
// Controller defines the operations related with icon
type Controller interface {
// Get the icon specified by digest
Get(ctx context.Context, digest string) (icon *Icon, err error)
}
// NewController creates a new instance of the icon controller
func NewController() Controller {
return &controller{
artMgr: pkg.ArtifactMgr,
regCli: registry.Cli,
cache: sync.Map{},
}
}
type controller struct {
artMgr artifact.Manager
regCli registry.Client
cache sync.Map
}
func (c *controller) Get(ctx context.Context, digest string) (*Icon, error) {
ic, exist := c.cache.Load(digest)
if exist {
return ic.(*Icon), nil
}
var (
iconFile io.ReadCloser
err error
)
if i, exist := builtInIcons[digest]; exist {
iconFile, err = os.Open(i.path)
if err != nil {
return nil, err
}
defer iconFile.Close()
} else {
// read icon from blob
artifacts, err := c.artMgr.List(ctx, &q.Query{
Keywords: map[string]interface{}{
"Icon": digest,
},
})
if err != nil {
return nil, err
}
if len(artifacts) == 0 {
return nil, errors.New(nil).WithCode(errors.NotFoundCode).
WithMessage("the icon %s not found", digest)
}
_, iconFile, err = c.regCli.PullBlob(artifacts[0].RepositoryName, digest)
if err != nil {
return nil, err
}
defer iconFile.Close()
}
img, _, err := image.Decode(iconFile)
if err != nil {
return nil, err
}
// resize the icon to 50x50
if i, exist := builtInIcons[digest]; exist {
if i.resize {
img = resize.Thumbnail(50, 50, img, resize.NearestNeighbor)
}
} else {
img = resize.Thumbnail(50, 50, img, resize.NearestNeighbor)
}
// encode the resized icon to png
buf := &bytes.Buffer{}
if err = png.Encode(buf, img); err != nil {
return nil, err
}
icon := &Icon{
ContentType: "image/png",
Content: base64.StdEncoding.EncodeToString(buf.Bytes()),
}
c.cache.Store(digest, icon)
return icon, nil
}