Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove zap to slog, and add commandContext #179

Merged
merged 8 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.20"
go-version: "1.21"

- name: Build
run: make build
Expand Down
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)
}
17 changes: 9 additions & 8 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 All @@ -30,7 +31,7 @@ func (b *Gifsicle) Resize(imgfile *image.ImageFile, opts *Options) ([]byte, erro
return imgfile.Source, nil
}

cmd := exec.Command(b.Path,
cmd := exec.CommandContext(ctx, b.Path,
"--resize", fmt.Sprintf("%dx%d", opts.Width, opts.Height),
)
cmd.Stdin = bytes.NewReader(imgfile.Source)
Expand All @@ -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
48 changes: 26 additions & 22 deletions engine/engine.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package engine

import (
"context"
"fmt"
"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"
"log/slog"
"os/exec"
"sort"
"strings"
"time"
)

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,11 +87,12 @@ 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
source = output.Source
start = time.Now()
)

ct := output.ContentType()
Expand All @@ -109,13 +110,16 @@ 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()),
)
defer func() {
e.logger.InfoContext(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()),
slog.String("duration", time.Now().Sub(start).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 +136,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
12 changes: 5 additions & 7 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,9 +97,10 @@ 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
)

go 1.19
go 1.21
Loading
Loading