Skip to content

Commit b735a4a

Browse files
committed
added test
1 parent 78cf270 commit b735a4a

13 files changed

+559
-650
lines changed

cmd/mort.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020

2121
// Route => handler
2222
e.GET("/*", func(ctx echo.Context) error {
23-
obj, err := object.NewFileObject(ctx.Request().URL.Path)
23+
obj, err := object.NewFileObject(ctx.Request().URL.Path, imgConfig)
2424
if err != nil {
2525
return ctx.NoContent(400)
2626
}

config/config.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ func (self *Config) Init(filePath string) {
3131
errYaml := yaml.Unmarshal([]byte(data), self)
3232

3333
for name, bucket := range self.Buckets {
34-
bucket.Transform.PathRegexp = regexp.MustCompile(bucket.Transform.Path)
35-
self.Buckets[name] = bucket
34+
if bucket.Transform != nil && bucket.Transform.Path != "" {
35+
bucket.Transform.PathRegexp = regexp.MustCompile(bucket.Transform.Path)
36+
self.Buckets[name] = bucket
37+
}
3638
}
3739

3840
if errYaml != nil {

config/yaml.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type StorageTypes struct {
5050
}
5151

5252
type Bucket struct {
53-
Transform TransformYaml `yaml:"transform"`
53+
Transform *TransformYaml `yaml:"transform",omitempty`
5454
Storages StorageTypes `yaml:"storages"`
5555
}
5656

gopher.png

233 KB
Loading

object/file_object.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,30 @@ type FileObject struct {
3434
Uri string `json:"uri"`
3535
Bucket string `json:"bucket"`
3636
Key string `json:"key"`
37-
Parent string `json:"parent"`
3837
Transforms transforms.Transforms `json:"transforms"`
3938
Storage config.Storage `json:"storage"`
39+
Parent *FileObject
4040
}
4141

42-
func NewFileObject(path string) (*FileObject, error) {
42+
func NewFileObject(path string, mortConfig *config.Config) (*FileObject, error) {
4343
obj := FileObject{}
4444
obj.Uri = path
4545

46-
err := obj.decode()
46+
err := obj.decode(mortConfig)
4747
Logger.Infof("key = %s bucket = %s parent = %s\n", obj.Key, obj.Bucket, obj.Parent)
4848
return &obj, err
4949
}
5050

51-
func (self *FileObject) decode() error {
51+
func (self *FileObject) decode(mortConfig *config.Config) error {
5252
elements := strings.Split(self.Uri, "/")
5353
if len(elements) < 3 {
5454
return errors.New("Invalid path")
5555
}
5656

5757
self.Bucket = elements[1]
5858
self.Key = "/" + strings.Join(elements[2:], "/")
59-
if bucket, ok := config.GetInstance().Buckets[self.Bucket]; ok {
60-
self.decodeKey(bucket)
59+
if bucket, ok := mortConfig.Buckets[self.Bucket]; ok {
60+
self.decodeKey(bucket, mortConfig)
6161
if self.HasTransform() {
6262
self.Storage = bucket.Storages.Transform
6363
} else {
@@ -71,29 +71,33 @@ func (self *FileObject) decode() error {
7171
return nil
7272
}
7373

74-
func (self *FileObject) decodeKey(bucket config.Bucket) error {
74+
func (self *FileObject) decodeKey(bucket config.Bucket, mortConfig *config.Config) error {
75+
if bucket.Transform == nil {
76+
return nil
77+
}
78+
7579
trans := bucket.Transform
7680
matches := trans.PathRegexp.FindStringSubmatch(self.Key)
77-
if len(matches) == 0 {
81+
if len(matches) < 3 {
7882
return nil
7983
}
8084

8185
presetName := string(matches[trans.Order.PresetName + 1])
8286
parent := "/" + string(matches[trans.Order.Parent + 1])
8387

8488
self.Transforms = presetToTransform(bucket.Transform.Presets[presetName])
85-
self.Parent = parent
89+
parentObj, _ := NewFileObject(parent, mortConfig)
90+
self.Parent = parentObj
8691
return nil
8792
}
8893

8994

9095
func (self *FileObject) GetParent() *FileObject {
91-
parent, _ := NewFileObject(self.Parent)
92-
return parent
96+
return self.Parent
9397
}
9498

9599
func (self *FileObject) HasParent() bool {
96-
return self.Parent != ""
100+
return self.Parent != nil
97101
}
98102

99103
func (self *FileObject) HasTransform() bool {
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
buckets:
2+
bucket:
3+
storages:
4+
basic:
5+
kind: "local"
6+
rootPath: "/Users/aldor/workspace/mkaciubacom/web"

object/testdata/bucket-transform.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
buckets:
2+
bucket:
3+
transform:
4+
path: "\\/+(?:[a-z)+\\/([a-z0-9_]+)\\/(.*)"
5+
kind: "presets"
6+
order:
7+
presetName: 0
8+
parent: 1
9+
presets:
10+
blog_small:
11+
quality: 75
12+
filters:
13+
thumbnail: { size: [100, 70], mode: outbound }
14+
interlace:
15+
mode: line
16+
storages:
17+
basic:
18+
kind: "local"
19+
rootPath: "/Users/aldor/workspace/mkaciubacom/web"
20+
transform:
21+
kind: "local"

0 commit comments

Comments
 (0)