Skip to content

Commit

Permalink
remove magic strings from processor/native/processor.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Jul 1, 2019
1 parent eb99107 commit 731b964
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pkg/processor/native/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import (
const (
pngType = "png"
jpgType = "jpeg"

cropDurationKey = "cropDuration"
resizeDurationKey = "resizeDuration"
watermarkDurationKey = "watermarkDuration"
grayScaleDurationKey = "grayScaleDuration"
decodeDurationKey = "decodeDuration"
encodeDurationKey = "encodeDuration"
)

// BildProcessor uses bild library to process images using native Golang image.Image interface
Expand All @@ -37,7 +44,7 @@ func (bp *BildProcessor) Crop(input []byte, width, height int, point processor.C
x0, y0 := getStartingPointForCrop(w, h, width, height, point)
rect := image.Rect(x0, y0, width+x0, height+y0)
img = (clone.AsRGBA(img)).SubImage(rect)
metrics.Update(metrics.UpdateOption{Name: "cropDuration", Type: metrics.Duration, Duration: time.Since(t)})
metrics.Update(metrics.UpdateOption{Name: cropDurationKey, Type: metrics.Duration, Duration: time.Since(t)})

return bp.encode(img, f)
}
Expand All @@ -55,7 +62,7 @@ func (bp *BildProcessor) Resize(input []byte, width, height int) ([]byte, error)
if w != initW || h != initH {
t := time.Now()
img = transform.Resize(img, w, h, transform.Linear)
metrics.Update(metrics.UpdateOption{Name: "resizeDuration", Type: metrics.Duration, Duration: time.Since(t)})
metrics.Update(metrics.UpdateOption{Name: resizeDurationKey, Type: metrics.Duration, Duration: time.Since(t)})
}

return bp.encode(img, f)
Expand Down Expand Up @@ -88,7 +95,7 @@ func (bp *BildProcessor) Watermark(base []byte, overlay []byte, opacity uint8) (

// Performing overlay
draw.DrawMask(baseImg.(draw.Image), overlayImg.Bounds().Add(offset), overlayImg, image.ZP, mask, image.ZP, draw.Over)
metrics.Update(metrics.UpdateOption{Name: "watermarkDuration", Type: metrics.Duration, Duration: time.Since(t)})
metrics.Update(metrics.UpdateOption{Name: watermarkDurationKey, Type: metrics.Duration, Duration: time.Since(t)})

return bp.encode(baseImg, f)
}
Expand All @@ -101,7 +108,7 @@ func (bp *BildProcessor) GrayScale(input []byte) ([]byte, error) {

t := time.Now()
img = effect.Grayscale(img)
metrics.Update(metrics.UpdateOption{Name: "grayScaleDuration", Type: metrics.Duration, Duration: time.Since(t)})
metrics.Update(metrics.UpdateOption{Name: grayScaleDurationKey, Type: metrics.Duration, Duration: time.Since(t)})

return bp.encode(img, f)
}
Expand All @@ -110,7 +117,7 @@ func (bp *BildProcessor) decode(data []byte) (image.Image, string, error) {
t := time.Now()
img, f, err := image.Decode(bytes.NewReader(data))
if err == nil {
metrics.Update(metrics.UpdateOption{Name: "decodeDuration", Type: metrics.Duration, Duration: time.Since(t)})
metrics.Update(metrics.UpdateOption{Name: decodeDurationKey, Type: metrics.Duration, Duration: time.Since(t)})
}
return img, f, err
}
Expand All @@ -128,7 +135,7 @@ func (bp *BildProcessor) encode(img image.Image, format string) ([]byte, error)
} else {
err = jpeg.Encode(buff, img, nil)
}
metrics.Update(metrics.UpdateOption{Name: "encodeDuration", Type: metrics.Duration, Duration: time.Since(t)})
metrics.Update(metrics.UpdateOption{Name: encodeDurationKey, Type: metrics.Duration, Duration: time.Since(t)})
return buff.Bytes(), err
}

Expand Down

0 comments on commit 731b964

Please sign in to comment.