Skip to content

Commit

Permalink
chore: wrap errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed May 21, 2024
1 parent cf015e3 commit 952eb78
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions engine/backend/gifsicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package backend
import (
"bytes"
"context"
"errors"
"fmt"
"image/gif"
"os/exec"

"github.com/pkg/errors"
"github.com/thoas/picfit/image"
)

Expand All @@ -31,8 +31,9 @@ func (b *Gifsicle) Resize(ctx context.Context, imgfile *image.ImageFile, opts *O
return imgfile.Source, nil
}

resizeOption := fmt.Sprintf("%dx%d", opts.Width, opts.Height)
cmd := exec.CommandContext(ctx, b.Path,
"--resize", fmt.Sprintf("%dx%d", opts.Width, opts.Height),
"--resize", resizeOption,
)
cmd.Stdin = bytes.NewReader(imgfile.Source)
stdout := new(bytes.Buffer)
Expand All @@ -44,7 +45,7 @@ func (b *Gifsicle) Resize(ctx context.Context, imgfile *image.ImageFile, opts *O
if err := cmd.Run(); errors.As(err, &target) && target.Exited() {
return nil, errors.New(stderr.String())
} else if err != nil {
return nil, err
return nil, errors.Wrap(err, "unable to resize")
}
return stdout.Bytes(), nil
}
Expand All @@ -62,11 +63,14 @@ func (b *Gifsicle) Thumbnail(ctx context.Context, imgfile *image.ImageFile, opts

bounds := img.Bounds()
left, top, cropw, croph := computecrop(bounds.Dx(), bounds.Dy(), opts.Width, opts.Height)
cropOption := fmt.Sprintf("%d,%d+%dx%d", left, top, cropw, croph)
resizeOption := fmt.Sprintf("%dx%d", opts.Width, opts.Height)

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),
"--crop", cropOption,
"--resize", resizeOption,
)

cmd.Stdin = bytes.NewReader(imgfile.Source)
stdout := new(bytes.Buffer)
cmd.Stdout = stdout
Expand All @@ -77,7 +81,7 @@ func (b *Gifsicle) Thumbnail(ctx context.Context, imgfile *image.ImageFile, opts
if err := cmd.Run(); errors.As(err, &target) && target.Exited() {
return nil, errors.New(stderr.String())
} else if err != nil {
return nil, err
return nil, errors.Wrap(err, "unable to thumbnail")
}
return stdout.Bytes(), nil
}
Expand Down

0 comments on commit 952eb78

Please sign in to comment.