| 1 | package webread |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "image" |
| 7 | "image/draw" |
| 8 | "image/gif" |
| 9 | "image/jpeg" |
| 10 | "image/png" |
| 11 | "mime" |
| 12 | "strings" |
| 13 | "unicode/utf8" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/attach" |
| 16 | ) |
| 17 | |
| 18 | // sniff names what body is from its bytes. kind is KindImage for PNG, |
| 19 | // JPEG and GIF, KindText for text, and "" for anything refused; typ is |
| 20 | // the media type the bytes show. declared, the server's Content-Type, |
| 21 | // only labels plain text as CSS or Markdown: it decides nothing. |
| 22 | func sniff(body []byte, declared string) (kind, typ string) { |
| 23 | switch m := attach.Sniff(body); m { |
| 24 | case attach.PNG, attach.JPEG, attach.GIF: |
| 25 | return KindImage, m |
| 26 | case "": |
| 27 | default: |
| 28 | return "", m |
| 29 | } |
| 30 | if !isText(body) { |
| 31 | return "", "application/octet-stream" |
| 32 | } |
| 33 | head := strings.ToLower(strings.TrimLeft(string(body[:min(len(body), 1024)]), "\ufeff \t\r\n\f")) |
| 34 | if strings.HasPrefix(head, "<svg") || strings.HasPrefix(head, "<?xml") && strings.Contains(head, "<svg") { |
| 35 | return KindText, "image/svg+xml" |
| 36 | } |
| 37 | if strings.HasPrefix(head, "<!doctype html") || strings.HasPrefix(head, "<html") || |
| 38 | strings.HasPrefix(head, "<") && (strings.Contains(head, "<head") || strings.Contains(head, "<body") || strings.Contains(head, "<title")) { |
| 39 | return KindText, "text/html" |
| 40 | } |
| 41 | if (strings.HasPrefix(head, "{") || strings.HasPrefix(head, "[")) && json.Valid(body) { |
| 42 | return KindText, "application/json" |
| 43 | } |
| 44 | if d, _, err := mime.ParseMediaType(declared); err == nil && (d == "text/css" || d == "text/markdown") { |
| 45 | return KindText, d |
| 46 | } |
| 47 | return KindText, "text/plain" |
| 48 | } |
| 49 | |
| 50 | // isText is valid UTF-8 with no NUL and no control characters beyond |
| 51 | // tab, newline, carriage return and form feed. |
| 52 | func isText(b []byte) bool { |
| 53 | if !utf8.Valid(b) { |
| 54 | return false |
| 55 | } |
| 56 | for _, r := range string(b) { |
| 57 | if r < 0x20 && r != '\t' && r != '\n' && r != '\r' && r != '\f' || r == 0x7f { |
| 58 | return false |
| 59 | } |
| 60 | } |
| 61 | return true |
| 62 | } |
| 63 | |
| 64 | // normaliseImage checks an image's header bounds before decoding it, |
| 65 | // decodes it, fits it within MaxEdge on its long side and re-encodes it as |
| 66 | // PNG, which keeps no metadata. |
| 67 | func normaliseImage(media string, body []byte) (out []byte, w, h int, reason string) { |
| 68 | var cfg image.Config |
| 69 | var err error |
| 70 | decode := map[string]func(*bytes.Reader) (image.Image, error){ |
| 71 | attach.PNG: func(r *bytes.Reader) (image.Image, error) { return png.Decode(r) }, |
| 72 | attach.JPEG: func(r *bytes.Reader) (image.Image, error) { return jpeg.Decode(r) }, |
| 73 | attach.GIF: func(r *bytes.Reader) (image.Image, error) { return gif.Decode(r) }, |
| 74 | }[media] |
| 75 | switch media { |
| 76 | case attach.PNG: |
| 77 | cfg, err = png.DecodeConfig(bytes.NewReader(body)) |
| 78 | case attach.JPEG: |
| 79 | cfg, err = jpeg.DecodeConfig(bytes.NewReader(body)) |
| 80 | case attach.GIF: |
| 81 | cfg, err = gif.DecodeConfig(bytes.NewReader(body)) |
| 82 | default: |
| 83 | return nil, 0, 0, ReasonType |
| 84 | } |
| 85 | if err != nil || cfg.Width <= 0 || cfg.Height <= 0 { |
| 86 | return nil, 0, 0, ReasonImageInvalid |
| 87 | } |
| 88 | if int64(cfg.Width)*int64(cfg.Height) > MaxPixels { |
| 89 | return nil, 0, 0, ReasonPixels |
| 90 | } |
| 91 | img, err := decode(bytes.NewReader(body)) |
| 92 | if err != nil { |
| 93 | return nil, 0, 0, ReasonImageInvalid |
| 94 | } |
| 95 | b := img.Bounds() |
| 96 | if b.Dx() <= 0 || b.Dy() <= 0 || int64(b.Dx())*int64(b.Dy()) > MaxPixels { |
| 97 | return nil, 0, 0, ReasonPixels |
| 98 | } |
| 99 | rgba := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) |
| 100 | draw.Draw(rgba, rgba.Bounds(), img, b.Min, draw.Src) |
| 101 | if w, h = b.Dx(), b.Dy(); max(w, h) > MaxEdge { |
| 102 | if w >= h { |
| 103 | w, h = MaxEdge, max(1, h*MaxEdge/w) |
| 104 | } else { |
| 105 | w, h = max(1, w*MaxEdge/h), MaxEdge |
| 106 | } |
| 107 | rgba = shrink(rgba, w, h) |
| 108 | } |
| 109 | var buf bytes.Buffer |
| 110 | if err = png.Encode(&buf, rgba); err != nil { |
| 111 | return nil, 0, 0, ReasonImageInvalid |
| 112 | } |
| 113 | if buf.Len() > MaxImageFile { |
| 114 | return nil, 0, 0, ReasonTooLarge |
| 115 | } |
| 116 | return buf.Bytes(), w, h, "" |
| 117 | } |
| 118 | |
| 119 | // shrink is an area average over premultiplied RGBA: each output pixel is |
| 120 | // the mean of the source pixels it covers. |
| 121 | func shrink(src *image.RGBA, w, h int) *image.RGBA { |
| 122 | sw, sh := src.Rect.Dx(), src.Rect.Dy() |
| 123 | dst := image.NewRGBA(image.Rect(0, 0, w, h)) |
| 124 | for y := 0; y < h; y++ { |
| 125 | y0, y1 := y*sh/h, max((y+1)*sh/h, y*sh/h+1) |
| 126 | for x := 0; x < w; x++ { |
| 127 | x0, x1 := x*sw/w, max((x+1)*sw/w, x*sw/w+1) |
| 128 | var s [4]uint64 |
| 129 | var n uint64 |
| 130 | for sy := y0; sy < y1; sy++ { |
| 131 | row := src.Pix[sy*src.Stride:] |
| 132 | for sx := x0; sx < x1; sx++ { |
| 133 | p := row[sx*4:] |
| 134 | s[0], s[1], s[2], s[3] = s[0]+uint64(p[0]), s[1]+uint64(p[1]), s[2]+uint64(p[2]), s[3]+uint64(p[3]) |
| 135 | n++ |
| 136 | } |
| 137 | } |
| 138 | d := dst.Pix[y*dst.Stride+x*4:] |
| 139 | d[0], d[1], d[2], d[3] = uint8(s[0]/n), uint8(s[1]/n), uint8(s[2]/n), uint8(s[3]/n) |
| 140 | } |
| 141 | } |
| 142 | return dst |
| 143 | } |
| 144 | |