From fc79b851b26b19e1c83dc4b5d49d5ce06e09842a Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 19 Jun 2020 18:08:35 -0700 Subject: [PATCH] fix go lint warnings - handle errors where possible - explicitly ignore errors where it makes sense to - fix deprecations and unused var --- cmd/imageproxy-sign/main.go | 4 +++- cmd/imageproxy/main.go | 2 +- imageproxy.go | 14 +++++++++----- imageproxy_test.go | 2 +- internal/gcscache/gcscache.go | 2 +- internal/s3cache/s3cache.go | 2 +- transform_test.go | 24 +++++++++++++++--------- 7 files changed, 31 insertions(+), 19 deletions(-) diff --git a/cmd/imageproxy-sign/main.go b/cmd/imageproxy-sign/main.go index b7803dc..02c94c1 100644 --- a/cmd/imageproxy-sign/main.go +++ b/cmd/imageproxy-sign/main.go @@ -54,7 +54,9 @@ func sign(key string, s string, urlOnly bool) ([]byte, error) { } mac := hmac.New(sha256.New, []byte(k)) - mac.Write([]byte(u.String())) + if _, err := mac.Write([]byte(u.String())); err != nil { + return nil, err + } return mac.Sum(nil), nil } diff --git a/cmd/imageproxy/main.go b/cmd/imageproxy/main.go index 14b0b8b..0138ee7 100644 --- a/cmd/imageproxy/main.go +++ b/cmd/imageproxy/main.go @@ -54,7 +54,7 @@ var signatureKeys signatureKeyList var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions") var timeout = flag.Duration("timeout", 0, "time limit for requests served by this proxy") var verbose = flag.Bool("verbose", false, "print verbose logging messages") -var version = flag.Bool("version", false, "Deprecated: this flag does nothing") +var _ = flag.Bool("version", false, "Deprecated: this flag does nothing") var contentTypes = flag.String("contentTypes", "image/*", "comma separated list of allowed content types") var userAgent = flag.String("userAgent", "willnorris/imageproxy", "specify the user-agent used by imageproxy when fetching images from origin website") diff --git a/imageproxy.go b/imageproxy.go index 16c887b..6ffb993 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -234,7 +234,9 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.WriteHeader(resp.StatusCode) - io.Copy(w, resp.Body) + if _, err := io.Copy(w, resp.Body); err != nil { + p.logf("error copying response: %v", err) + } } // peekContentType peeks at the first 512 bytes of p, and attempts to detect @@ -365,7 +367,7 @@ func validSignature(key []byte, r *Request) bool { // check signature with URL only mac := hmac.New(sha256.New, key) - mac.Write([]byte(r.URL.String())) + _, _ = mac.Write([]byte(r.URL.String())) want := mac.Sum(nil) if hmac.Equal(got, want) { return true @@ -377,7 +379,7 @@ func validSignature(key []byte, r *Request) bool { u.Fragment = opt.String() mac = hmac.New(sha256.New, key) - mac.Write([]byte(u.String())) + _, _ = mac.Write([]byte(u.String())) want = mac.Sum(nil) return hmac.Equal(got, want) } @@ -482,11 +484,13 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er // replay response with transformed image and updated content length buf := new(bytes.Buffer) fmt.Fprintf(buf, "%s %s\n", resp.Proto, resp.Status) - resp.Header.WriteSubset(buf, map[string]bool{ + if err := resp.Header.WriteSubset(buf, map[string]bool{ "Content-Length": true, // exclude Content-Type header if the format may have changed during transformation "Content-Type": opt.Format != "" || resp.Header.Get("Content-Type") == "image/webp" || resp.Header.Get("Content-Type") == "image/tiff", - }) + }); err != nil { + t.log("error copying headers: %v", err) + } fmt.Fprintf(buf, "Content-Length: %d\n\n", len(img)) buf.Write(img) diff --git a/imageproxy_test.go b/imageproxy_test.go index 98b8bcd..bbbc6f4 100644 --- a/imageproxy_test.go +++ b/imageproxy_test.go @@ -375,7 +375,7 @@ func (t testTransport) RoundTrip(req *http.Request) (*http.Response, error) { case "/png": m := image.NewNRGBA(image.Rect(0, 0, 1, 1)) img := new(bytes.Buffer) - png.Encode(img, m) + _ = png.Encode(img, m) raw = fmt.Sprintf("HTTP/1.1 200 OK\nContent-Length: %d\nContent-Type: image/png\n\n%s", len(img.Bytes()), img.Bytes()) default: diff --git a/internal/gcscache/gcscache.go b/internal/gcscache/gcscache.go index df28061..6e9eb25 100644 --- a/internal/gcscache/gcscache.go +++ b/internal/gcscache/gcscache.go @@ -63,7 +63,7 @@ func (c *cache) object(key string) *storage.ObjectHandle { func keyToFilename(key string) string { h := md5.New() - io.WriteString(h, key) + _, _ = io.WriteString(h, key) return hex.EncodeToString(h.Sum(nil)) } diff --git a/internal/s3cache/s3cache.go b/internal/s3cache/s3cache.go index ad9b661..25213b2 100644 --- a/internal/s3cache/s3cache.go +++ b/internal/s3cache/s3cache.go @@ -75,7 +75,7 @@ func (c *cache) Delete(key string) { func keyToFilename(key string) string { h := md5.New() - io.WriteString(h, key) + _, _ = io.WriteString(h, key) return hex.EncodeToString(h.Sum(nil)) } diff --git a/transform_test.go b/transform_test.go index 7609a1b..260e4a6 100644 --- a/transform_test.go +++ b/transform_test.go @@ -44,7 +44,7 @@ var ( func newImage(w, h int, pixels ...color.Color) image.Image { m := image.NewNRGBA(image.Rect(0, 0, w, h)) if len(pixels) == 1 { - draw.Draw(m, m.Bounds(), &image.Uniform{pixels[0]}, image.ZP, draw.Src) + draw.Draw(m, m.Bounds(), &image.Uniform{pixels[0]}, image.Point{}, draw.Src) } else { for i, p := range pixels { m.Set(i%w, i/w, p) @@ -106,22 +106,26 @@ func TestTransform(t *testing.T) { src := newImage(2, 2, red, green, blue, yellow) buf := new(bytes.Buffer) - png.Encode(buf, src) + if err := png.Encode(buf, src); err != nil { + t.Errorf("error encoding reference image: %v", err) + } tests := []struct { name string - encode func(io.Writer, image.Image) + encode func(io.Writer, image.Image) error exactOutput bool // whether input and output should match exactly }{ - {"bmp", func(w io.Writer, m image.Image) { bmp.Encode(w, m) }, true}, - {"gif", func(w io.Writer, m image.Image) { gif.Encode(w, m, nil) }, true}, - {"jpeg", func(w io.Writer, m image.Image) { jpeg.Encode(w, m, nil) }, false}, - {"png", func(w io.Writer, m image.Image) { png.Encode(w, m) }, true}, + {"bmp", func(w io.Writer, m image.Image) error { return bmp.Encode(w, m) }, true}, + {"gif", func(w io.Writer, m image.Image) error { return gif.Encode(w, m, nil) }, true}, + {"jpeg", func(w io.Writer, m image.Image) error { return jpeg.Encode(w, m, nil) }, false}, + {"png", func(w io.Writer, m image.Image) error { return png.Encode(w, m) }, true}, } for _, tt := range tests { buf := new(bytes.Buffer) - tt.encode(buf, src) + if err := tt.encode(buf, src); err != nil { + t.Errorf("error encoding image: %v", err) + } in := buf.Bytes() out, err := Transform(in, emptyOptions) @@ -152,7 +156,9 @@ func TestTransform(t *testing.T) { func TestTransform_InvalidFormat(t *testing.T) { src := newImage(2, 2, red, green, blue, yellow) buf := new(bytes.Buffer) - png.Encode(buf, src) + if err := png.Encode(buf, src); err != nil { + t.Errorf("error encoding reference image: %v", err) + } _, err := Transform(buf.Bytes(), Options{Format: "invalid"}) if err == nil {