| 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/xml" |
| 9 | "fmt" |
| 10 | "image" |
| 11 | "image/color" |
| 12 | _ "image/gif" |
| 13 | _ "image/jpeg" |
| 14 | "image/png" |
| 15 | "io" |
| 16 | "math" |
| 17 | "strings" |
| 18 | |
| 19 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 20 | ) |
| 21 | |
| 22 | const ( |
| 23 | MaxEdge = 512 |
| 24 | MaxSVG = 256 << 10 |
| 25 | MaxUpload = 2 << 20 |
| 26 | ) |
| 27 | |
| 28 | // Normalize returns bytes and a content type ready to store. |
| 29 | func Normalize(in []byte) ([]byte, string, error) { |
| 30 | if len(in) == 0 || len(in) > MaxUpload { |
| 31 | return nil, "", i18n.E("err.logoBig") |
| 32 | } |
| 33 | if looksSVG(in) { |
| 34 | if len(in) > MaxSVG { |
| 35 | return nil, "", i18n.E("err.logoBig") |
| 36 | } |
| 37 | if err := vetSVG(in); err != nil { |
| 38 | return nil, "", err |
| 39 | } |
| 40 | return in, "image/svg+xml", nil |
| 41 | } |
| 42 | img, _, err := image.Decode(bytes.NewReader(in)) |
| 43 | if err != nil { |
| 44 | return nil, "", i18n.E("err.logoType") |
| 45 | } |
| 46 | b := img.Bounds() |
| 47 | w, h := b.Dx(), b.Dy() |
| 48 | if w < 1 || h < 1 { |
| 49 | return nil, "", i18n.E("err.logoType") |
| 50 | } |
| 51 | nw, nh := fit(w, h) |
| 52 | var src image.Image = img |
| 53 | if nw != w || nh != h { |
| 54 | src = scale(img, nw, nh) |
| 55 | } |
| 56 | var buf bytes.Buffer |
| 57 | if err := png.Encode(&buf, src); err != nil { |
| 58 | return nil, "", i18n.E("err.logoStore") |
| 59 | } |
| 60 | return buf.Bytes(), "image/png", nil |
| 61 | } |
| 62 | |
| 63 | func fit(w, h int) (int, int) { |
| 64 | if w <= MaxEdge && h <= MaxEdge { |
| 65 | return w, h |
| 66 | } |
| 67 | s := float64(MaxEdge) / float64(w) |
| 68 | if float64(h)*s > MaxEdge { |
| 69 | s = float64(MaxEdge) / float64(h) |
| 70 | } |
| 71 | nw := int(math.Round(float64(w) * s)) |
| 72 | nh := int(math.Round(float64(h) * s)) |
| 73 | if nw < 1 { |
| 74 | nw = 1 |
| 75 | } |
| 76 | if nh < 1 { |
| 77 | nh = 1 |
| 78 | } |
| 79 | if nw > MaxEdge { |
| 80 | nw = MaxEdge |
| 81 | } |
| 82 | if nh > MaxEdge { |
| 83 | nh = MaxEdge |
| 84 | } |
| 85 | return nw, nh |
| 86 | } |
| 87 | |
| 88 | func scale(src image.Image, nw, nh int) *image.NRGBA { |
| 89 | dst := image.NewNRGBA(image.Rect(0, 0, nw, nh)) |
| 90 | sb := src.Bounds() |
| 91 | sw, sh := float64(sb.Dx()), float64(sb.Dy()) |
| 92 | for y := 0; y < nh; y++ { |
| 93 | sy := (float64(y)+0.5)*sh/float64(nh) - 0.5 |
| 94 | for x := 0; x < nw; x++ { |
| 95 | sx := (float64(x)+0.5)*sw/float64(nw) - 0.5 |
| 96 | dst.SetNRGBA(x, y, bilinear(src, sb, sx, sy)) |
| 97 | } |
| 98 | } |
| 99 | return dst |
| 100 | } |
| 101 | |
| 102 | func bilinear(src image.Image, b image.Rectangle, x, y float64) color.NRGBA { |
| 103 | x0 := int(math.Floor(x)) |
| 104 | y0 := int(math.Floor(y)) |
| 105 | fx := x - float64(x0) |
| 106 | fy := y - float64(y0) |
| 107 | c00 := nrgbaAt(src, b, x0, y0) |
| 108 | c10 := nrgbaAt(src, b, x0+1, y0) |
| 109 | c01 := nrgbaAt(src, b, x0, y0+1) |
| 110 | c11 := nrgbaAt(src, b, x0+1, y0+1) |
| 111 | mix := func(a, b, c, d uint8) uint8 { |
| 112 | top := float64(a)*(1-fx) + float64(b)*fx |
| 113 | bot := float64(c)*(1-fx) + float64(d)*fx |
| 114 | return uint8(math.Round(top*(1-fy) + bot*fy)) |
| 115 | } |
| 116 | return color.NRGBA{ |
| 117 | R: mix(c00.R, c10.R, c01.R, c11.R), |
| 118 | G: mix(c00.G, c10.G, c01.G, c11.G), |
| 119 | B: mix(c00.B, c10.B, c01.B, c11.B), |
| 120 | A: mix(c00.A, c10.A, c01.A, c11.A), |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func nrgbaAt(src image.Image, b image.Rectangle, x, y int) color.NRGBA { |
| 125 | if x < b.Min.X { |
| 126 | x = b.Min.X |
| 127 | } |
| 128 | if y < b.Min.Y { |
| 129 | y = b.Min.Y |
| 130 | } |
| 131 | if x >= b.Max.X { |
| 132 | x = b.Max.X - 1 |
| 133 | } |
| 134 | if y >= b.Max.Y { |
| 135 | y = b.Max.Y - 1 |
| 136 | } |
| 137 | r, g, bl, a := src.At(x, y).RGBA() |
| 138 | return color.NRGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(bl >> 8), A: uint8(a >> 8)} |
| 139 | } |
| 140 | |
| 141 | func looksSVG(in []byte) bool { |
| 142 | s := strings.ToLower(strings.TrimSpace(string(in))) |
| 143 | return strings.HasPrefix(s, "<svg") || strings.HasPrefix(s, "<?xml") || strings.HasPrefix(s, "<!doctype") |
| 144 | } |
| 145 | |
| 146 | func vetSVG(in []byte) error { |
| 147 | low := strings.ToLower(string(in)) |
| 148 | if !strings.Contains(low, "<svg") { |
| 149 | return i18n.E("err.logoType") |
| 150 | } |
| 151 | for _, bad := range []string{"<script", "foreignobject", "onload", "onerror", "javascript:", "<iframe", "<object", "<embed"} { |
| 152 | if strings.Contains(low, bad) { |
| 153 | return i18n.E("err.logoSVG") |
| 154 | } |
| 155 | } |
| 156 | dec := xml.NewDecoder(bytes.NewReader(in)) |
| 157 | saw := false |
| 158 | for { |
| 159 | tok, err := dec.Token() |
| 160 | if err == io.EOF { |
| 161 | break |
| 162 | } |
| 163 | if err != nil { |
| 164 | return i18n.E("err.logoSVG") |
| 165 | } |
| 166 | se, ok := tok.(xml.StartElement) |
| 167 | if !ok { |
| 168 | continue |
| 169 | } |
| 170 | if strings.EqualFold(se.Name.Local, "svg") { |
| 171 | saw = true |
| 172 | } |
| 173 | } |
| 174 | if !saw { |
| 175 | return i18n.E("err.logoType") |
| 176 | } |
| 177 | return nil |
| 178 | } |
| 179 | |
| 180 | // SizeOf decodes a PNG or other raster and returns its pixel size. |
| 181 | func SizeOf(in []byte) (int, int, error) { |
| 182 | cfg, _, err := image.DecodeConfig(bytes.NewReader(in)) |
| 183 | if err != nil { |
| 184 | return 0, 0, fmt.Errorf("decode: %w", err) |
| 185 | } |
| 186 | return cfg.Width, cfg.Height, nil |
| 187 | } |