-
Notifications
You must be signed in to change notification settings - Fork 8
/
image_get_request.go
78 lines (63 loc) · 1.71 KB
/
image_get_request.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"github.com/julienschmidt/httprouter"
"github.com/tokubai/kinu/logger"
"github.com/tokubai/kinu/resizer"
"github.com/tokubai/kinu/resource"
)
type ImageGetRequest struct {
Category string
Id string
Geometry *resizer.Geometry
Extension string
}
type ErrInvalidRequest struct {
error
Message string
}
func (e *ErrInvalidRequest) Error() string { return e.Message }
func ExtractId(filename string) string {
return strings.Split(filename, ".")[0]
}
func ExtractExtension(filename string) string {
return strings.Replace(filepath.Ext(filename), ".", "", 1)
}
func IsValidImageExt(ext string) bool {
for _, e := range resource.ValidExtensions {
if e == ext {
return true
}
}
return false
}
func NewImageGetRequest(ps httprouter.Params) (*ImageGetRequest, error) {
imageType := ps.ByName("type")
if len(imageType) == 0 {
return nil, &ErrInvalidRequest{Message: "required image type."}
}
filename := ps.ByName("filename")
if len(filename) == 0 {
return nil, &ErrInvalidRequest{Message: "required filename."}
}
ext := ExtractExtension(ps.ByName("filename"))
if len(ext) == 0 {
return nil, &ErrInvalidRequest{Message: "required extension."}
}
id := ExtractId(filename)
if len(id) == 0 {
return nil, &ErrInvalidRequest{Message: "invalid filename"}
}
geometry, err := resizer.ParseGeometry(ps.ByName("geometry"))
if err != nil {
return nil, err
}
logger.WithFields(logrus.Fields{
"geometry": geometry.ToString(),
"image_type": imageType,
"image_id": id,
}).Debug("parse success image get request.")
return &ImageGetRequest{Category: imageType, Id: id, Geometry: geometry, Extension: ext}, nil
}