Skip to content

Commit

Permalink
fix: fix transform for resizeCropAuto and resize in one take
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Feb 5, 2020
1 parent a76208f commit f06af8c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/mort/mort.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

const (
// Version of mort
Version = "0.14.1"
Version = "0.14.3"
// BANNER just fancy command line banner
BANNER = `
/\/\ ___ _ __| |_
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ require (
gopkg.in/kothar/go-backblaze.v0 v0.0.0-20190520213052-702d4e7eb465 // indirect
gopkg.in/yaml.v2 v2.2.2
)

go 1.13
10 changes: 5 additions & 5 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ type S3Key struct {

// Bucket describe single bucket entry in config
type Bucket struct {
Transform *Transform `yaml:"transform,omitempty"`
Storages StorageTypes `yaml:"storages"`
Keys []S3Key `yaml:"keys"`
Headers map[string]string `yaml:"headers"`
Transform *Transform `yaml:"transform,omitempty"`
Storages StorageTypes `yaml:"storages"`
Keys []S3Key `yaml:"keys"`
Headers map[string]string `yaml:"headers"`
Name string
}

// HeaderYaml allow you to override response headers
type HeaderYaml struct {
StatusCodes []int `yaml:"statusCodes"`
Override bool `yaml:"override`
Override bool `yaml:"override`
Values map[string]string `yaml:"values"`
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/engine/image_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ func (c *ImageEngine) Process(obj *object.FileObject, trans []transforms.Transfo

optsArr, err := tran.BimgOptions(transforms.NewImageInfo(meta, bimg.DetermineImageTypeName(buf)))
if err != nil {
monitoring.Log().Warn("ImageEngine unable to create opts array age", obj.LogData(zap.Any("transforms", trans), zap.Any("currentTrans", tran), zap.Error(err))...)
return response.NewError(500, err), err
}
optsLen := len(optsArr)
for i, opts := range optsArr {
buf, err = image.Process(opts)
if err != nil {
monitoring.Log().Warn("ImageEngine unable to process image", obj.LogData(zap.Any("optsArr", optsArr), zap.Any("opts", opts), zap.Error(err))...)
return response.NewError(500, err), err
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,11 @@ func (r *RequestProcessor) processImage(obj *object.FileObject, parent *response
eng := engine.NewImageEngine(parent)
res, err := eng.Process(obj, mergedTrans)
if err != nil {
return response.NewError(400, err)
errRes := response.NewError(400, err)
errRes.SetTransforms(mergedTrans)
return errRes
}
res.SetTransforms(mergedTrans)

resCpy, err := res.Copy()
if err == nil {
Expand Down
23 changes: 18 additions & 5 deletions pkg/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"github.com/aldor007/mort/pkg/helpers"
"github.com/aldor007/mort/pkg/monitoring"
"github.com/aldor007/mort/pkg/object"
"github.com/aldor007/mort/pkg/transforms"
"github.com/djherbis/stream"
"github.com/pquerna/cachecontrol/cacheobject"
"github.com/vmihailenco/msgpack"
"go.uber.org/zap"

"io"
"io/ioutil"
Expand Down Expand Up @@ -43,6 +45,7 @@ type Response struct {
transformer bodyTransformFnc // function that can transform body writner
cachable bool // flag indicating if response can be cached
ttl int // time to live in cache
trans []transforms.Transforms
}

// New create response object with io.ReadCloser
Expand Down Expand Up @@ -184,6 +187,12 @@ func (r *Response) SetDebug(obj *object.FileObject) *Response {

if obj.HasTransform() {
r.Headers.Set("x-mort-transform", "true")
buf, err := json.Marshal(r.trans)
if err == nil {
r.Headers.Set("x-mort-transform-json", string(buf))
} else {
monitoring.Log().Warn("Response/SetDebug unable to marshal trans", zap.Error(err))
}
}

if obj.HasParent() {
Expand All @@ -199,6 +208,10 @@ func (r *Response) SetDebug(obj *object.FileObject) *Response {
return r
}

func (r *Response) SetTransforms(trans []transforms.Transforms) {
r.trans = trans
}

// HasError check if response contains error
func (r *Response) HasError() bool {
return r.errorValue != nil
Expand All @@ -219,10 +232,6 @@ func (r *Response) Send(w http.ResponseWriter) error {
var resStream io.Reader
if r.ContentLength != 0 {
resStream = r.Stream()
if resStream == nil {
monitoring.Log().Error("Response send resStream = nil")
r.StatusCode = 500
}
}

w.WriteHeader(r.StatusCode)
Expand Down Expand Up @@ -324,9 +333,13 @@ func (r *Response) Copy() (*Response, error) {
c.Headers[k] = v
}

c.trans = make([]transforms.Transforms, len(r.trans))
copy(c.trans, r.trans)

if r.body != nil {
c.ContentLength = int64(len(r.body))
c.body = r.body
c.body = make([]byte, c.ContentLength)
copy(c.body, r.body)
c.bodySeeker = bytes.NewReader(c.body)
c.reader = ioutil.NopCloser(c.bodySeeker)
} else if r.reader != nil {
Expand Down
19 changes: 17 additions & 2 deletions pkg/transforms/transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ func Merge(transformsTab []Transforms) []Transforms {
transformsTab[i], transformsTab[j] = transformsTab[j], transformsTab[i]
}

for i := 0; i < transLen; i++ {
if transformsTab[i].NoMerge {
return transformsTab
}
}

result := make([]Transforms, 1)
baseTrans := transformsTab[0]
result[0] = baseTrans
Expand Down Expand Up @@ -512,8 +518,17 @@ func (t *Transforms) BimgOptions(imageInfo ImageInfo) ([]bimg.Options, error) {
if t.autoCropHeight != 0 || t.autoCropWidth != 0 {
bAutoCrop := bimg.Options{}
bAutoCrop.Left, bAutoCrop.Top, bAutoCrop.AreaWidth, bAutoCrop.AreaHeight = t.calculateAutoCrop(imageInfo)
opts = append(opts, bAutoCrop)
opts = append(opts, bimg.Options{Width: t.autoCropWidth, Height: t.autoCropHeight, Crop: true, Gravity: bimg.GravityCentre})
if t.width != 0 || t.height != 0 {
if t.width > t.areaWidth || t.height > t.areaHeight {
opts = append(opts, bAutoCrop, bimg.Options{Width: t.autoCropWidth, Height: t.autoCropHeight, Crop: true, Gravity: bimg.GravityCentre})
} else {

opts = append([]bimg.Options{bAutoCrop, bimg.Options{Width: t.autoCropWidth, Height: t.autoCropHeight, Crop: true, Gravity: bimg.GravityCentre}}, opts...)
}

} else {
opts = append(opts, bAutoCrop, bimg.Options{Width: t.autoCropWidth, Height: t.autoCropHeight, Crop: true, Gravity: bimg.GravityCentre})
}
}

return opts, nil
Expand Down

0 comments on commit f06af8c

Please sign in to comment.