/
server.go
62 lines (49 loc) · 1.35 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package ivy
import (
"github.com/h2non/bimg"
"github.com/kataras/iris"
"github.com/plimble/errors"
)
func NewServer(config *Config) (*iris.Framework, error) {
ir := iris.New(
iris.OptionDisableBanner(true),
)
source := NewS3(config.SourceAwsId, config.SourceAwsSecret, config.SourceAwsS3Bucket, config.SourceAwsS3Region)
p := NewProcessor(source)
ir.Get("/*path", handler(p))
return ir, nil
}
func handler(p Processor) iris.HandlerFunc {
return func(ctx *iris.Context) {
path := ctx.Param("path")
if path == "" {
ctx.SetStatusCode(404)
return
}
opt := &bimg.Options{}
opt.Width, opt.Height = splitWidthHeightString(ctx.FormValueString("r"))
opt.Crop = getBoolFromString(ctx.FormValueString("c"))
g := getGravityFromString(ctx.FormValueString("g"))
if g > -1 {
opt.Gravity = g
}
f := getBoolFromString(ctx.FormValueString("f"))
if f {
opt.Force = true
} else {
opt.Embed = true
}
opt.Quality = stringToInt(ctx.FormValueString("q"))
opt.Type = stringToImageType(ctx.FormValueString("t"))
img, imgType, err := p.Process(path, opt)
if err != nil {
status, err := errors.ErrorStatus(err)
ctx.Text(status, err.Error())
return
}
ctx.Response.Header.SetContentType(getContentType(imgType))
ctx.Response.Header.SetContentLength(len(img))
ctx.Response.AppendBody(img)
ctx.SetStatusCode(200)
}
}