Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Sep 27, 2017
1 parent 4d7b739 commit 4219906
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 85 deletions.
3 changes: 0 additions & 3 deletions cmd/mort.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ func main() {
// dodac placeholder
res := mort.Process(obj)
res.WriteHeaders(ctx.Response())
defer res.Close()

e.Logger.Info("res headers %s", res.Headers)
//return ctx.JSON(200, obj)
return ctx.Stream(res.StatusCode, res.Headers[response.ContentType], res.Stream)
})

Expand Down
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"gopkg.in/yaml.v2"
"io/ioutil"
"sync"
"regexp"
)

type Config struct {
Buckets map[string] Bucket `yaml:"buckets"`
Headers []HeaderYaml `yaml:"headers"`
}

var instance *Config
Expand All @@ -28,6 +30,11 @@ func (self *Config) Init(filePath string) {

errYaml := yaml.Unmarshal([]byte(data), self)

for name, bucket := range self.Buckets {
bucket.Transform.PathRegexp = regexp.MustCompile(bucket.Transform.Path)
self.Buckets[name] = bucket
}

if errYaml != nil {
panic(errYaml)
}
Expand Down
14 changes: 11 additions & 3 deletions config/yaml.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "regexp"

type PresetsYaml struct {
Quality int `yaml:"quality"`
Filters struct {
Expand All @@ -25,9 +27,10 @@ type PresetsYaml struct {
}

type TransformYaml struct {
Path string `yaml:"path"`
Kind string `yaml:"kind"`
Presets map[string]PresetsYaml `yaml:"presets"`
Path string `yaml:"path"`
PathRegexp *regexp.Regexp
Kind string `yaml:"kind"`
Presets map[string]PresetsYaml `yaml:"presets"`
}

type Storage struct {
Expand All @@ -44,3 +47,8 @@ type Bucket struct {
Transform TransformYaml `yaml:"transform"`
Storages StorageTypes `yaml:"storages"`
}

type HeaderYaml struct {
StatusCodes []int `yaml:"statusCodes""`
Values map[string]string `yaml:"values"`
}
11 changes: 11 additions & 0 deletions configuration/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
headers:
- statusCodes: [200]
values:
"cache-control": "max-age=84000, public"
- statusCodes: [404, 400]
values:
"cache-control": "max-age=60, public"
- statusCodes: [500, 503]
values:
"cache-control": "max-age=10, public"

buckets:
media2:
transform:
Expand Down
46 changes: 3 additions & 43 deletions object/file_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@ package object

import (
"errors"
"regexp"
"strings"

Logger "github.com/labstack/gommon/log"
"mort/config"
"mort/transforms"
)

const (
URI_TYPE_S3 = 0
URI_TYPE_LOCAL = 1
)

var URI_LIIP_RE = regexp.MustCompile(`\/media\/cache\/.*`)
var URI_LOCAL_RE = regexp.MustCompile(`\/media\/.*`)

func presetToTransform(preset config.PresetsYaml) transforms.Transforms {
var trans transforms.Transforms
filters := preset.Filters
Expand All @@ -43,7 +34,6 @@ type FileObject struct {
Uri string `json:"uri"`
Bucket string `json:"bucket"`
Key string `json:"key"`
UriType int `json:"uriType"`
Parent string `json:"parent"`
Transforms transforms.Transforms `json:"transforms"`
Storage config.Storage `json:"storage"`
Expand All @@ -52,22 +42,13 @@ type FileObject struct {
func NewFileObject(path string) (*FileObject, error) {
obj := FileObject{}
obj.Uri = path
if URI_LOCAL_RE.MatchString(path) {
obj.UriType = URI_TYPE_LOCAL
} else {
obj.UriType = URI_TYPE_S3
}

err := obj.decode()
Logger.Infof("UriType = %d key = %s bucket = %s parent = %s err = %s\n", obj.UriType, obj.Key, obj.Bucket, obj.Parent, err)
Logger.Infof("key = %s bucket = %s parent = %s\n", obj.Key, obj.Bucket, obj.Parent)
return &obj, err
}

func (self *FileObject) decode() error {
//if self.UriType == URI_TYPE_LOCAL {
// return self.decodeLiipPath()
//}

elements := strings.Split(self.Uri, "/")
if len(elements) < 3 {
return errors.New("Invalid path")
Expand All @@ -91,41 +72,20 @@ func (self *FileObject) decode() error {
}

func (self *FileObject) decodeKey(bucket config.Bucket) error {
pathRe := regexp.MustCompile(bucket.Transform.Path)
matches := pathRe.FindStringSubmatch(self.Key)
//pathRe := bucket.Transform.PathRegexp
matches := bucket.Transform.PathRegexp.FindStringSubmatch(self.Key)
if len(matches) == 0 {
return nil
}

Logger.Infof(" m = %s", matches)
presetName := string(matches[1])
parent := "/" + string(matches[2])

self.Transforms = presetToTransform(bucket.Transform.Presets[presetName])
self.Parent = parent
Logger.Infof("uri: %s parent: %s key: %s len: %d \n", self.Uri, self.Parent, self.Key)
return nil
}

//func (self *FileObject) decodeLiipPath() error {
// self.Uri = strings.Replace(self.Uri, "//", "/", 3)
// key := strings.Replace(self.Uri, "/media/cache", "", 1)
// key = strings.Replace(key, "/resolve", "", 1)
// elements := strings.Split(key, "/")
// if URI_LIIP_RE.MatchString(self.Uri) {
// presetName := elements[1]
// //self.Key = strings.Replace(self.Uri, "//", "/", 3)
// self.Key = strings.Replace(self.Uri, "//", "/", 3)
// self.Parent = "/" + strings.Join(elements[4:], "/")
// liipConfig := config.GetInstance().LiipConfig
// self.Transforms = liipToTransform(liipConfig[presetName])
// } else {
// self.Key = self.Uri
// }
//
// Logger.Debugf("uri: %s parent: %s key: %s len: %d \n", self.Uri, self.Parent, self.Key, len(elements))
// return nil
//}

func (self *FileObject) GetParent() *FileObject {
parent, _ := NewFileObject(self.Parent)
Expand Down
25 changes: 21 additions & 4 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package mort
import (
"strings"

"mort/config"
"mort/engine"
"mort/object"
"mort/response"
"mort/storage"
"fmt"
)

func Process(obj *object.FileObject) *response.Response {
Expand All @@ -16,18 +18,18 @@ func Process(obj *object.FileObject) *response.Response {
}

if obj.Transforms.NotEmpty == false {
return storage.Get(obj)
return updateHeaders(storage.Get(obj))
}

if parent.StatusCode == 404 {
return parent
return updateHeaders(parent)
}

if strings.Contains(parent.Headers[response.ContentType], "image/") {
return processImage(parent, obj)
return updateHeaders(processImage(parent, obj))
}

return storage.Get(obj)
return updateHeaders(storage.Get(obj))
}

func processImage(parent *response.Response, obj *object.FileObject) *response.Response {
Expand All @@ -40,3 +42,18 @@ func processImage(parent *response.Response, obj *object.FileObject) *response.R
return result

}

func updateHeaders(res *response.Response) *response.Response {
headers := config.GetInstance().Headers
for _, headerPred := range headers {
for _, status := range headerPred.StatusCodes {
if status == res.StatusCode {
for h, v := range headerPred.Values {
res.Set(h, v)
}
return res
}
}
}
return res
}
10 changes: 5 additions & 5 deletions response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const (

type Response struct {
StatusCode int
Stream io.Reader
Stream io.ReadCloser
Headers map[string]string
}

func New(statusCode int, body io.Reader) *Response {
func New(statusCode int, body io.ReadCloser) *Response {
res := Response{StatusCode: statusCode, Stream: body}
res.Headers = make(map[string]string)
if body == nil {
Expand All @@ -29,7 +29,7 @@ func New(statusCode int, body io.Reader) *Response {
return &res
}
func NewBuf(statusCode int, body []byte) *Response {
res := Response{StatusCode: statusCode, Stream: bytes.NewReader(body)}
res := Response{StatusCode: statusCode, Stream: ioutil.NopCloser(bytes.NewReader(body))}
res.Headers = make(map[string]string)
if body == nil {
res.SetContentType("application/octet-stream")
Expand All @@ -42,7 +42,7 @@ func NewBuf(statusCode int, body []byte) *Response {
func NewError(statusCode int, err error) *Response {
body := map[string]string{"message": err.Error()}
jsonBody, _ := json.Marshal(body)
res := Response{StatusCode: statusCode, Stream: bytes.NewReader(jsonBody)}
res := Response{StatusCode: statusCode, Stream: ioutil.NopCloser(bytes.NewReader(jsonBody))}
res.Headers = make(map[string]string)
res.SetContentType("application/json")
return &res
Expand All @@ -68,5 +68,5 @@ func (r *Response) ReadBody() ([]byte, error) {
}

func (r *Response) Close() {
return
r.Close()
}
29 changes: 2 additions & 27 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,30 @@
package storage

import (
"io/ioutil"
"io"
"mime"
"path"
"regexp"
"path/filepath"


"github.com/graymeta/stow"
fileStorage "github.com/graymeta/stow/local"
_ "github.com/graymeta/stow/s3"
Logger "github.com/labstack/gommon/log"

"mort/object"
"mort/response"
"io"
)

var isUrl_RE = regexp.MustCompile("http://")

const notFound = "{\"error\":\"not found\"}"


func Get(obj *object.FileObject) *response.Response {
key := obj.Key
//if isUrl_RE.MatchString(key) || obj.UriType != object.URI_TYPE_LOCAL {
// return response.NewError(400, errors.New("Not implemented"))
//}
//
//data, err := getFromDisk(obj, key)
//if os.IsNotExist(err) {
// fmt.Println(err)
// return response.New(404, []byte(notFound))
//} else if err != nil {
// return response.NewError(503, err)
//}

client, err := getClient(obj)
if err != nil {
return response.NewError(503, err)
}

item, errItem := client.Item(key)
if errItem != nil {
Logger.Infof("%s %s %s", errItem, key)
if errItem == stow.ErrNotFound {
return response.NewBuf(404, []byte(notFound))
}
Expand Down Expand Up @@ -79,15 +59,10 @@ func getClient(obj *object.FileObject) (stow.Container, error){
// XXX: check if it is ok
defer client.Close()

Logger.Infof("get client %s %s %s", client, obj.Bucket, obj.Key)
return client.Container(obj.Bucket)
}

func getFromDisk(obj *object.FileObject, filePath string) ([]byte, error) {
return ioutil.ReadFile(filepath.Join(obj.Storage.RootPath, obj.Bucket,filePath))
}

func prepareResponse(obj *object.FileObject, stream io.Reader) *response.Response {
func prepareResponse(obj *object.FileObject, stream io.ReadCloser) *response.Response {
res := response.New(200, stream)
res.SetContentType(mime.TypeByExtension(path.Ext(obj.Key)))
return res
Expand Down

0 comments on commit 4219906

Please sign in to comment.