| 1 | // Package logo normalizes an uploaded group or repository logo. |
| 2 | // Raster images larger than 512×512 are scaled down, keeping the |
| 3 | // aspect ratio. SVG is kept as text when it is small and has no script. |
| 4 | package logo |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "encoding/binary" |
| 9 | "encoding/xml" |
| 10 | "fmt" |
| 11 | "image" |
| 12 | "image/color" |
| 13 | _ "image/gif" |
| 14 | _ "image/jpeg" |
| 15 | "image/png" |
| 16 | "io" |
| 17 | "math" |
| 18 | "strings" |
| 19 | |
| 20 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 21 | ) |
| 22 | |
| 23 | const ( |
| 24 | MaxEdge = 512 |
| 25 | MaxSVG = 256 << 10 |
| 26 | MaxUpload = 2 << 20 |
| 27 | ) |
| 28 | |
| 29 | // Normalize returns bytes and a content type ready to store. |
| 30 | func Normalize(in []byte) ([]byte, string, error) { |
| 31 | if len(in) == 0 || len(in) > MaxUpload { |
| 32 | return nil, "", i18n.E("err.logoBig") |
| 33 | } |
| 34 | if looksSVG(in) { |
| 35 | if len(in) > MaxSVG { |
| 36 | return nil, "", i18n.E("err.logoBig") |
| 37 | } |
| 38 | if err := vetSVG(in); err != nil { |
| 39 | return nil, "", err |
| 40 | } |
| 41 | return in, "image/svg+xml", nil |
| 42 | } |
| 43 | if looksICO(in) { |
| 44 | if err := vetICO(in); err != nil { |
| 45 | return nil, "", err |
| 46 | } |
| 47 | return in, "image/x-icon", nil |
| 48 | } |
| 49 | img, _, err := image.Decode(bytes.NewReader(in)) |
| 50 | if err != nil { |
| 51 | return nil, "", i18n.E("err.logoType") |
| 52 | } |
| 53 | b := img.Bounds() |
| 54 | w, h := b.Dx(), b.Dy() |
| 55 | if w < 1 || h < 1 { |
| 56 | return nil, "", i18n.E("err.logoType") |
| 57 | } |
| 58 | nw, nh := fit(w, h) |
| 59 | var src image.Image = img |
| 60 | if nw != w || nh != h { |
| 61 | src = scale(img, nw, nh) |
| 62 | } |
| 63 | var buf bytes.Buffer |
| 64 | if err := png.Encode(&buf, src); err != nil { |
| 65 | return nil, "", i18n.E("err.logoStore") |
| 66 | } |
| 67 | return buf.Bytes(), "image/png", nil |
| 68 | } |
| 69 | |
| 70 | func fit(w, h int) (int, int) { |
| 71 | if w <= MaxEdge && h <= MaxEdge { |
| 72 | return w, h |
| 73 | } |
| 74 | s := float64(MaxEdge) / float64(w) |
| 75 | if float64(h)*s > MaxEdge { |
| 76 | s = float64(MaxEdge) / float64(h) |
| 77 | } |
| 78 | nw := int(math.Round(float64(w) * s)) |
| 79 | nh := int(math.Round(float64(h) * s)) |
| 80 | if nw < 1 { |
| 81 | nw = 1 |
| 82 | } |
| 83 | if nh < 1 { |
| 84 | nh = 1 |
| 85 | } |
| 86 | if nw > MaxEdge { |
| 87 | nw = MaxEdge |
| 88 | } |
| 89 | if nh > MaxEdge { |
| 90 | nh = MaxEdge |
| 91 | } |
| 92 | return nw, nh |
| 93 | } |
| 94 | |
| 95 | func scale(src image.Image, nw, nh int) *image.NRGBA { |
| 96 | dst := image.NewNRGBA(image.Rect(0, 0, nw, nh)) |
| 97 | sb := src.Bounds() |
| 98 | sw, sh := float64(sb.Dx()), float64(sb.Dy()) |
| 99 | for y := 0; y < nh; y++ { |
| 100 | sy := (float64(y)+0.5)*sh/float64(nh) - 0.5 |
| 101 | for x := 0; x < nw; x++ { |
| 102 | sx := (float64(x)+0.5)*sw/float64(nw) - 0.5 |
| 103 | dst.SetNRGBA(x, y, bilinear(src, sb, sx, sy)) |
| 104 | } |
| 105 | } |
| 106 | return dst |
| 107 | } |
| 108 | |
| 109 | func bilinear(src image.Image, b image.Rectangle, x, y float64) color.NRGBA { |
| 110 | x0 := int(math.Floor(x)) |
| 111 | y0 := int(math.Floor(y)) |
| 112 | fx := x - float64(x0) |
| 113 | fy := y - float64(y0) |
| 114 | c00 := nrgbaAt(src, b, x0, y0) |
| 115 | c10 := nrgbaAt(src, b, x0+1, y0) |
| 116 | c01 := nrgbaAt(src, b, x0, y0+1) |
| 117 | c11 := nrgbaAt(src, b, x0+1, y0+1) |
| 118 | mix := func(a, b, c, d uint8) uint8 { |
| 119 | top := float64(a)*(1-fx) + float64(b)*fx |
| 120 | bot := float64(c)*(1-fx) + float64(d)*fx |
| 121 | return uint8(math.Round(top*(1-fy) + bot*fy)) |
| 122 | } |
| 123 | return color.NRGBA{ |
| 124 | R: mix(c00.R, c10.R, c01.R, c11.R), |
| 125 | G: mix(c00.G, c10.G, c01.G, c11.G), |
| 126 | B: mix(c00.B, c10.B, c01.B, c11.B), |
| 127 | A: mix(c00.A, c10.A, c01.A, c11.A), |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func nrgbaAt(src image.Image, b image.Rectangle, x, y int) color.NRGBA { |
| 132 | if x < b.Min.X { |
| 133 | x = b.Min.X |
| 134 | } |
| 135 | if y < b.Min.Y { |
| 136 | y = b.Min.Y |
| 137 | } |
| 138 | if x >= b.Max.X { |
| 139 | x = b.Max.X - 1 |
| 140 | } |
| 141 | if y >= b.Max.Y { |
| 142 | y = b.Max.Y - 1 |
| 143 | } |
| 144 | r, g, bl, a := src.At(x, y).RGBA() |
| 145 | return color.NRGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(bl >> 8), A: uint8(a >> 8)} |
| 146 | } |
| 147 | |
| 148 | func looksSVG(in []byte) bool { |
| 149 | s := strings.ToLower(strings.TrimSpace(string(in))) |
| 150 | return strings.HasPrefix(s, "<svg") || strings.HasPrefix(s, "<?xml") || strings.HasPrefix(s, "<!doctype") |
| 151 | } |
| 152 | |
| 153 | func vetSVG(in []byte) error { |
| 154 | low := strings.ToLower(string(in)) |
| 155 | if !strings.Contains(low, "<svg") { |
| 156 | return i18n.E("err.logoType") |
| 157 | } |
| 158 | for _, bad := range []string{"<script", "foreignobject", "onload", "onerror", "javascript:", "<iframe", "<object", "<embed"} { |
| 159 | if strings.Contains(low, bad) { |
| 160 | return i18n.E("err.logoSVG") |
| 161 | } |
| 162 | } |
| 163 | dec := xml.NewDecoder(bytes.NewReader(in)) |
| 164 | saw := false |
| 165 | for { |
| 166 | tok, err := dec.Token() |
| 167 | if err == io.EOF { |
| 168 | break |
| 169 | } |
| 170 | if err != nil { |
| 171 | return i18n.E("err.logoSVG") |
| 172 | } |
| 173 | se, ok := tok.(xml.StartElement) |
| 174 | if !ok { |
| 175 | continue |
| 176 | } |
| 177 | if strings.EqualFold(se.Name.Local, "svg") { |
| 178 | saw = true |
| 179 | } |
| 180 | } |
| 181 | if !saw { |
| 182 | return i18n.E("err.logoType") |
| 183 | } |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | // looksICO herkent de ICO-kop (reserved=0, type=1). CUR (type=2) wijzen we af. |
| 188 | func looksICO(in []byte) bool { |
| 189 | return len(in) >= 6 && in[0] == 0 && in[1] == 0 && in[2] == 1 && in[3] == 0 |
| 190 | } |
| 191 | |
| 192 | func vetICO(in []byte) error { |
| 193 | if !looksICO(in) { |
| 194 | return i18n.E("err.logoType") |
| 195 | } |
| 196 | n := int(binary.LittleEndian.Uint16(in[4:6])) |
| 197 | if n < 1 || n > 64 { |
| 198 | return i18n.E("err.logoType") |
| 199 | } |
| 200 | need := 6 + n*16 |
| 201 | if len(in) < need { |
| 202 | return i18n.E("err.logoType") |
| 203 | } |
| 204 | for i := 0; i < n; i++ { |
| 205 | ent := in[6+i*16 : 6+(i+1)*16] |
| 206 | size := int(binary.LittleEndian.Uint32(ent[8:12])) |
| 207 | off := int(binary.LittleEndian.Uint32(ent[12:16])) |
| 208 | if size < 1 || off < need || off > len(in) || off+size > len(in) { |
| 209 | return i18n.E("err.logoType") |
| 210 | } |
| 211 | } |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | // SizeOf decodes a PNG or other raster and returns its pixel size. |
| 216 | func SizeOf(in []byte) (int, int, error) { |
| 217 | cfg, _, err := image.DecodeConfig(bytes.NewReader(in)) |
| 218 | if err != nil { |
| 219 | return 0, 0, fmt.Errorf("decode: %w", err) |
| 220 | } |
| 221 | return cfg.Width, cfg.Height, nil |
| 222 | } |