0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-16 21:56:43 -05:00

fix go lint warnings

- handle errors where possible
- explicitly ignore errors where it makes sense to
- fix deprecations and unused var
This commit is contained in:
Will Norris 2020-06-19 18:08:35 -07:00
parent 5600290c82
commit fc79b851b2
7 changed files with 31 additions and 19 deletions

View file

@ -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
}

View file

@ -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")

View file

@ -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)

View file

@ -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:

View file

@ -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))
}

View file

@ -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))
}

View file

@ -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 {