Skip to content

Commit

Permalink
remove zap to slog, and add commandContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Houtmann committed Sep 18, 2023
1 parent 9dc3f31 commit 330214e
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 222 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,6 @@ Levels available are:
* info
* error
* warning
* fatal

Allowed sizes
-------------
Expand Down
2 changes: 2 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ var StickPositions = []string{
}

const ModifiedTimeFormat = time.RFC1123

const RequestIDCtx = "request-id"
13 changes: 7 additions & 6 deletions engine/backend/backend.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backend

import (
"context"
"fmt"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -32,11 +33,11 @@ func (o Options) String() string {

// Engine is an interface to define an image engine
type Backend interface {
Fit(img *image.ImageFile, options *Options) ([]byte, error)
Flat(background *image.ImageFile, options *Options) ([]byte, error)
Flip(img *image.ImageFile, options *Options) ([]byte, error)
Resize(img *image.ImageFile, options *Options) ([]byte, error)
Rotate(img *image.ImageFile, options *Options) ([]byte, error)
Fit(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error)
Flat(ctx context.Context, background *image.ImageFile, options *Options) ([]byte, error)
Flip(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error)
Resize(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error)
Rotate(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error)
String() string
Thumbnail(img *image.ImageFile, options *Options) ([]byte, error)
Thumbnail(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error)
}
15 changes: 8 additions & 7 deletions engine/backend/gifsicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"bytes"
"context"
"errors"
"fmt"
"image/gif"
Expand All @@ -20,7 +21,7 @@ func (b *Gifsicle) String() string {
}

// Resize implements Backend.
func (b *Gifsicle) Resize(imgfile *image.ImageFile, opts *Options) ([]byte, error) {
func (b *Gifsicle) Resize(ctx context.Context, imgfile *image.ImageFile, opts *Options) ([]byte, error) {
img, err := gif.Decode(bytes.NewReader(imgfile.Source))
if err != nil {
return nil, err
Expand Down Expand Up @@ -49,7 +50,7 @@ func (b *Gifsicle) Resize(imgfile *image.ImageFile, opts *Options) ([]byte, erro
}

// Thumbnail implements Backend.
func (b *Gifsicle) Thumbnail(imgfile *image.ImageFile, opts *Options) ([]byte, error) {
func (b *Gifsicle) Thumbnail(ctx context.Context, imgfile *image.ImageFile, opts *Options) ([]byte, error) {
img, err := gif.Decode(bytes.NewReader(imgfile.Source))
if err != nil {
return nil, err
Expand All @@ -62,7 +63,7 @@ func (b *Gifsicle) Thumbnail(imgfile *image.ImageFile, opts *Options) ([]byte, e
bounds := img.Bounds()
left, top, cropw, croph := computecrop(bounds.Dx(), bounds.Dy(), opts.Width, opts.Height)

cmd := exec.Command(b.Path,
cmd := exec.CommandContext(ctx, b.Path,
"--crop", fmt.Sprintf("%d,%d+%dx%d", left, top, cropw, croph),
"--resize", fmt.Sprintf("%dx%d", opts.Width, opts.Height),
)
Expand All @@ -82,22 +83,22 @@ func (b *Gifsicle) Thumbnail(imgfile *image.ImageFile, opts *Options) ([]byte, e
}

// Rotate implements Backend.
func (b *Gifsicle) Rotate(*image.ImageFile, *Options) ([]byte, error) {
func (b *Gifsicle) Rotate(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error) {
return nil, MethodNotImplementedError
}

// Fit implements Backend.
func (b *Gifsicle) Fit(*image.ImageFile, *Options) ([]byte, error) {
func (b *Gifsicle) Fit(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error) {
return nil, MethodNotImplementedError
}

// Flat implements Backend.
func (b *Gifsicle) Flat(*image.ImageFile, *Options) ([]byte, error) {
func (b *Gifsicle) Flat(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error) {
return nil, MethodNotImplementedError
}

// Flip implements Backend.
func (b *Gifsicle) Flip(*image.ImageFile, *Options) ([]byte, error) {
func (b *Gifsicle) Flip(ctx context.Context, img *image.ImageFile, options *Options) ([]byte, error) {
return nil, MethodNotImplementedError
}

Expand Down
11 changes: 6 additions & 5 deletions engine/backend/goimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"bytes"
"context"
"fmt"
"image"
"image/color/palette"
Expand Down Expand Up @@ -43,15 +44,15 @@ type GoImage struct{}
func (e *GoImage) String() string {
return "goimage"
}
func (e *GoImage) Resize(img *imagefile.ImageFile, options *Options) ([]byte, error) {
func (e *GoImage) Resize(ctx context.Context, img *imagefile.ImageFile, options *Options) ([]byte, error) {
return e.resize(img, options, imaging.Resize)
}

func (e *GoImage) Thumbnail(img *imagefile.ImageFile, options *Options) ([]byte, error) {
func (e *GoImage) Thumbnail(ctx context.Context, img *imagefile.ImageFile, options *Options) ([]byte, error) {
return e.resize(img, options, imaging.Thumbnail)
}

func (e *GoImage) Rotate(img *imagefile.ImageFile, options *Options) ([]byte, error) {
func (e *GoImage) Rotate(ctx context.Context, img *imagefile.ImageFile, options *Options) ([]byte, error) {
image, err := e.source(img)
if err != nil {
return nil, err
Expand All @@ -67,7 +68,7 @@ func (e *GoImage) Rotate(img *imagefile.ImageFile, options *Options) ([]byte, er
return e.toBytes(transform(image), options.Format, options.Quality)
}

func (e *GoImage) Flip(img *imagefile.ImageFile, options *Options) ([]byte, error) {
func (e *GoImage) Flip(ctx context.Context, img *imagefile.ImageFile, options *Options) ([]byte, error) {
image, err := e.source(img)
if err != nil {
return nil, err
Expand All @@ -83,7 +84,7 @@ func (e *GoImage) Flip(img *imagefile.ImageFile, options *Options) ([]byte, erro
return e.toBytes(transform(image), options.Format, options.Quality)
}

func (e *GoImage) Fit(img *imagefile.ImageFile, options *Options) ([]byte, error) {
func (e *GoImage) Fit(ctx context.Context, img *imagefile.ImageFile, options *Options) ([]byte, error) {
if options.Format == imaging.GIF {
content, err := e.transformGIF(img, options, imaging.Thumbnail)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion engine/backend/goimage_flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"bytes"
"context"
"fmt"
"image"
"image/draw"
Expand All @@ -16,7 +17,7 @@ import (
imagefile "github.com/thoas/picfit/image"
)

func (e *GoImage) Flat(backgroundFile *imagefile.ImageFile, options *Options) ([]byte, error) {
func (e *GoImage) Flat(ctx context.Context, backgroundFile *imagefile.ImageFile, options *Options) ([]byte, error) {
var err error
images := make([]image.Image, len(options.Images))
for i := range options.Images {
Expand Down
34 changes: 17 additions & 17 deletions engine/engine.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package engine

import (
"context"
"fmt"
"log/slog"
"os/exec"
"sort"
"strings"

"github.com/thoas/picfit/engine/backend"
"github.com/thoas/picfit/engine/config"
"github.com/thoas/picfit/image"
"github.com/thoas/picfit/logger"
"go.uber.org/zap"
)

type Engine struct {
DefaultFormat string
DefaultQuality int
Format string
backends []*backendWrapper
logger *zap.Logger
logger *slog.Logger
}

type backendWrapper struct {
Expand All @@ -28,7 +28,7 @@ type backendWrapper struct {
}

// New initializes an Engine
func New(cfg config.Config, logger *zap.Logger) *Engine {
func New(cfg config.Config, logger *slog.Logger) *Engine {
var b []*backendWrapper

if cfg.Backends == nil {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (e Engine) String() string {
return strings.Join(backendNames, " ")
}

func (e Engine) Transform(output *image.ImageFile, operations []EngineOperation) (*image.ImageFile, error) {
func (e Engine) Transform(ctx context.Context, output *image.ImageFile, operations []EngineOperation) (*image.ImageFile, error) {
var (
err error
processed []byte
Expand All @@ -109,13 +109,13 @@ func (e Engine) Transform(output *image.ImageFile, operations []EngineOperation)
continue
}

e.logger.Debug("Processing image...",
logger.String("backend", e.backends[j].backend.String()),
logger.String("operation", operations[i].Operation.String()),
logger.String("options", operations[i].Options.String()),
e.logger.DebugContext(ctx, "Processing image...",
slog.String("backend", e.backends[j].backend.String()),
slog.String("operation", operations[i].Operation.String()),
slog.String("options", operations[i].Options.String()),
)

processed, err = operate(e.backends[j].backend, output, operations[i].Operation, operations[i].Options)
processed, err = operate(ctx, e.backends[j].backend, output, operations[i].Operation, operations[i].Options)
if err == nil {
output.Source = processed
break
Expand All @@ -132,22 +132,22 @@ func (e Engine) Transform(output *image.ImageFile, operations []EngineOperation)
return output, err
}

func operate(b backend.Backend, img *image.ImageFile, operation Operation, options *backend.Options) ([]byte, error) {
func operate(ctx context.Context, b backend.Backend, img *image.ImageFile, operation Operation, options *backend.Options) ([]byte, error) {
switch operation {
case Noop:
return img.Source, nil
case Flip:
return b.Flip(img, options)
return b.Flip(ctx, img, options)
case Rotate:
return b.Rotate(img, options)
return b.Rotate(ctx, img, options)
case Resize:
return b.Resize(img, options)
return b.Resize(ctx, img, options)
case Thumbnail:
return b.Thumbnail(img, options)
return b.Thumbnail(ctx, img, options)
case Fit:
return b.Fit(img, options)
return b.Fit(ctx, img, options)
case Flat:
return b.Flat(img, options)
return b.Flat(ctx, img, options)
default:
return nil, fmt.Errorf("Operation not found for %s", operation)
}
Expand Down
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ require (
github.com/ulule/gokvstores v0.1.1-0.20221229151109-3bd12fb72ebe
github.com/ulule/gostorages v0.2.5-0.20230314124119-11134a4bce61
github.com/urfave/cli v1.22.10
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.24.0
golang.org/x/image v0.6.0
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
gopkg.in/fukata/golang-stats-api-handler.v1 v1.0.0
)

require (
github.com/gin-contrib/zap v0.1.0
github.com/google/uuid v1.3.0
github.com/prometheus/client_golang v1.14.0
)

Expand Down Expand Up @@ -64,6 +61,7 @@ require (
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -77,6 +75,7 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -85,8 +84,6 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
Expand All @@ -100,6 +97,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 330214e

Please sign in to comment.