Skip to content

Commit

Permalink
feat: resizeCropAuto
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Apr 21, 2019
1 parent dfdad7d commit f100d0e
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 294 deletions.
206 changes: 0 additions & 206 deletions Gopkg.lock

This file was deleted.

6 changes: 1 addition & 5 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
name = "github.com/aldor007/go-aws-auth"

[[constraint]]
branch = "develop"
branch = "master"
name = "github.com/aldor007/stow"

[[constraint]]
Expand Down Expand Up @@ -81,10 +81,6 @@
name = "gopkg.in/h2non/gock.v1"
version = "1.0.12"

[[constraint]]
branch = "master"
name = "gopkg.in/kothar/brotli-go.v0"

[[constraint]]
name = "gopkg.in/yaml.v2"
version = "2.2.2"
Expand Down
11 changes: 9 additions & 2 deletions 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.12.0"
Version = "0.13.0"
// BANNER just fancy command line banner
BANNER = `
/\/\ ___ _ __| |_
Expand Down Expand Up @@ -92,7 +92,13 @@ func handleSignals(servers []*http.Server, socketPaths []string, wg *sync.WaitGr
}

func configureMonitoring(mortConfig *config.Config) {
logCfg := zap.NewProductionConfig()
var logCfg zap.Config
if mortConfig.Server.LogLevel == "debug" {
logCfg = zap.NewDevelopmentConfig()
} else {
logCfg = zap.NewProductionConfig()
}

logCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, _ := logCfg.Build()

Expand Down Expand Up @@ -219,6 +225,7 @@ func main() {
res.Set("Access-Control-Allow-Headers", "Content-Type, X-Amz-Public-Width, X-Amz-Public-Height")
res.Set("Access-Control-Expose-Headers", "Content-Type, X-Amz-Public-Width, X-Amz-Public-Height")
res.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, HEAD")
res.Set("Access-Control-Allow-Origin", "*")
defer monitoring.Log().Sync() // flushes buffer, if any
if res.HasError() {
monitoring.Log().Warn("Mort process error", zap.String("obj.Key", obj.Key), zap.Error(res.Error()))
Expand Down
1 change: 1 addition & 0 deletions configuration/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
server:
logLevel: "debug"
listens:
- ":8080"
- "unix:/tmp/mort.sock"
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ func (c *Config) validateTransform(bucketName string, bucket Bucket) error {
}

func (c *Config) validateServer() error {
if c.Server.LogLevel == "" {
c.Server.LogLevel = "prod"
}

if c.Server.SingleListen == "" {
c.Server.SingleListen = ":8080"
}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type HeaderYaml struct {

// Server configure HTTP server
type Server struct {
LogLevel string `yaml:"logLevel"`
InternalListen string `yaml:"internalListen"`
SingleListen string `yaml:"listen"`
CacheSize int64 `yaml:"cacheSize"`
Expand Down
26 changes: 5 additions & 21 deletions pkg/engine/image_engine.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package engine

import (
"encoding/binary"
"net/http"
"strconv"
"time"

"github.com/spaolacci/murmur3"
"gopkg.in/h2non/bimg.v1"

"bytes"
Expand All @@ -16,6 +14,8 @@ import (
"github.com/aldor007/mort/pkg/transforms"
"go.uber.org/zap"
"sync"
"crypto/md5"
"encoding/hex"
)

// bufPool for string concatenations
Expand Down Expand Up @@ -45,7 +45,6 @@ func (c *ImageEngine) Process(obj *object.FileObject, trans []transforms.Transfo
return response.NewError(500, err), err
}

var transHash uint64
for _, tran := range trans {
image := bimg.NewImage(buf)
meta, err := image.Metadata()
Expand All @@ -62,21 +61,17 @@ func (c *ImageEngine) Process(obj *object.FileObject, trans []transforms.Transfo
if err != nil {
return response.NewError(500, err), err
}
transHash = transHash + tran.Hash().Sum64()
}

transHashB := make([]byte, 8)
binary.LittleEndian.PutUint64(transHashB, transHash)

hash := murmur3.New64()
hash.Write([]byte(obj.Key))
hash.Write(transHashB)
bodyHash := md5.New()
bodyHash.Write(buf)

res := response.NewBuf(200, buf)
res.SetContentType("image/" + bimg.DetermineImageTypeName(buf))
//res.Set("cache-control", "max-age=6000, public")
res.Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
res.Set("ETag", createWeakEtag(strconv.FormatUint(hash.Sum64(), 16)))
res.Set("ETag", hex.EncodeToString(bodyHash.Sum(nil)))
meta, err := bimg.Metadata(buf)
if err == nil {
res.Set("x-amz-meta-public-width", strconv.Itoa(meta.Size.Width))
Expand All @@ -89,14 +84,3 @@ func (c *ImageEngine) Process(obj *object.FileObject, trans []transforms.Transfo
return res, nil
}

func createWeakEtag(transHash string) string {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
buf.WriteByte('W')
buf.WriteByte('/')
buf.WriteByte('"')
buf.WriteString(transHash)
buf.WriteByte('"')
defer bufPool.Put(buf)
return buf.String()
}
Loading

0 comments on commit f100d0e

Please sign in to comment.