Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Aug 7, 2017
0 parents commit e5eb93f
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cmd/imgserver/imgserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (

"imgserver/object"
"imgserver/config"
"github.com/labstack/echo"
"imgserver"
)

func main() {
imgConfig := config.GetInstance()
imgConfig.Init("config/config.yml")
// Echo instance
e := echo.New()

// Route => handler
e.GET("/*", func(ctx echo.Context) error {
obj := object.NewFileObject(ctx.Request().URL.Path)
response := imgserver.Process(obj)
if response.Error != nil {
return ctx.NoContent(500)
}

//return ctx.JSON(200, obj)
return ctx.Blob(response.StatusCode, response.Headers["content-type"], response.Body)
})

// Start server
e.Logger.Fatal(e.Start(":8080"))
}
Binary file added config/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
liipConfigPath: "config/liip.yml"
localFilesPath: "/Users/aldor/workspace/mkaciubacom/web"
60 changes: 60 additions & 0 deletions config/liip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
liip_imagine:
resolvers:
default:
web_path: ~

filter_sets:
cache: ~
blog_small:
quality: 75
filters:
thumbnail: { size: [100, 70], mode: outbound }
interlace:
mode: line


blog_medium:
quality: 75
filters:
thumbnail: { size: [903, 600], mode: outbound }
crop: { start: [0, 0], size: [900, 320] }

blog_home:
quality: 75
filters:
entropy_crop: { size: [756, 396], mode: outbound }
recent_gallery:
quality: 75
filters:
entropy_crop: { size: [85, 85], mode: outbound }

promoted:
quality: 75
filters:
entropy_crop: { size: [896, 465], mode: outbound }

relatedposts:
quality: 75
filters:
entropy_crop: { size: [504, 369], mode: outbound }

blog_big:
quality: 75
filters:
auto_rotate: ~
strip: ~
gallery_thumb:
quality: 75
filters:
thumbnail: { size: [200], mode: outbound }
auto_rotate: ~
strip: ~
photocategory_img:
quality: 75
filters:
entropy_crop: { size: [600, 450], mode: outbound }
auto_rotate: ~
strip: ~
interlace:
mode: line

5 changes: 5 additions & 0 deletions install_osx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go get -u github.com/labstack/echo
go get gopkg.in/yaml.v2
brew tap homebrew/science
brew install vips --with-cfitsio --with-imagemagick --with-openexr --with-openslide --with-webp
go get -u gopkg.in/h2non/bimg.v1
3 changes: 3 additions & 0 deletions src/imgserver/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package api


58 changes: 58 additions & 0 deletions src/imgserver/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package config

import (
"fmt"
"sync"
"io/ioutil"
"gopkg.in/yaml.v2"
)

type Config struct {
LiipConfig map[string] LiipFiltersYAML
LocalFilesPath string `yaml:"localFileePath"`
}

type internalConfig struct {
LiipConfigPath string `yaml:"liipConfigPath"`
LiipConfig LiipConfigYAML
LocalFilesPath string `yaml:"localFilesPath"`
}

var instance *Config
var once sync.Once

func GetInstance() *Config {
once.Do(func() {
instance = &Config{}
})
return instance
}

func (self *Config) Init(filePath string) {
data, err := ioutil.ReadFile(filePath)
if err != nil {
panic(err)
}

internal := internalConfig{}
errYaml := yaml.Unmarshal([]byte(data), &internal)
if errYaml != nil {
panic(errYaml)
}

fmt.Printf("AAAAAa %s aa", internal.LiipConfigPath)

data, err = ioutil.ReadFile(internal.LiipConfigPath)
if err != nil {
panic(err)
}

errYaml = yaml.Unmarshal([]byte(data), &internal.LiipConfig)
if errYaml != nil {
panic(errYaml)
}

self.LiipConfig = internal.LiipConfig.LiipImagine.FilterSets
self.LocalFilesPath = internal.LocalFilesPath
fmt.Println(self.LiipConfig)
}
37 changes: 37 additions & 0 deletions src/imgserver/config/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

type LiipFiltersYAML struct {
Quality int `yaml:"quality"`
Filters struct {
Thumbnail struct {
Size []int `yaml:"size"`
Mode string `yaml:"mode"`
} `yaml:"thumbnail"`
Interlace struct {
Mode string `yaml:"mode"`
} `yaml:"interlace"`
Crop struct {
Size []int `yaml:"size"`
Start []int `yaml:"start"`
Mode string `yaml:"mode"`
} `yaml:"crop"`
SmartCrop struct {
Size []int `yaml:"size"`
Mode string `yaml:"mode"`
} `yaml:"entropy_crop"`
AutoRotate interface{} `yaml:"auto_rtate"`
Strip interface{} `yaml:"strip"`
} `yaml:"filters"`
}

type LiipConfigYAML struct {
LiipImagine struct {
Resolvers struct {
Default struct {
WebPath interface{} `yaml:"web_path"`
} `yaml:"default"`
} `yaml:"resolvers"`
FilterSets map[string] LiipFiltersYAML `yaml:"filter_sets"`
} `yaml:"liip_imagine"`
}

100 changes: 100 additions & 0 deletions src/imgserver/object/file_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package object

import (
"strings"
"regexp"
"fmt"
"imgserver/config"
"imgserver/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 liipToTransform(liip config.LiipFiltersYAML ) ([]transforms.Base, transforms.Param) {
filters := liip.Filters
var trans []transforms.Base

if len(filters.Thumbnail.Size) > 0 {
trans = append(trans, transforms.Thumbnail{transforms.ICrop{Size: filters.Thumbnail.Size, Mode: filters.Thumbnail.Mode}})
}

if len(filters.SmartCrop.Size) > 0 {
trans = append(trans, transforms.SmartCrop{transforms.ICrop{Size: filters.SmartCrop.Size, Mode: filters.SmartCrop.Mode}})
}

if len(filters.Crop.Size) > 0 {
trans = append(trans, transforms.Crop{ICrop: transforms.ICrop{Size: filters.Crop.Size, Mode: filters.Crop.Mode}, Start: filters.Crop.Start})
}

param := transforms.Quailty{Value:liip.Quality}

return trans, param
}


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.Base `json:"transforms"`
Params transforms.Param `json:"params"`

}

func NewFileObject(path string) *FileObject {
obj := FileObject{}
obj.Uri = path
if URI_LOCAL_RE.MatchString(path) {
obj.UriType = URI_TYPE_LOCAL
} else {
obj.UriType = URI_TYPE_S3
}
fmt.Printf("UriType = %d path = %s \n", obj.UriType, path)

obj.decode()
return &obj
}

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

func (self *FileObject) decodeLiipPath() *FileObject {
self.Uri = strings.Replace(self.Uri, "//", "/", 3)
key := strings.Replace(self.Uri, "/media/cache", "", 1)
key = strings.Replace(key, "/resolve", "", 1)
fmt.Printf("key = %s \n", key)
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, self.Params = liipToTransform(liipConfig[presetName])
} else {
self.Key = self.Uri
}
fmt.Printf("uri: %s parent: %s key: %s len: %d \n", self.Uri, self.Parent, self.Key, len(elements))
return self
}

func (self *FileObject) GetParent() *FileObject {
parent := NewFileObject(self.Parent)
return parent
}

func (self *FileObject) HasParent() bool{
return self.Parent != ""
}
24 changes: 24 additions & 0 deletions src/imgserver/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package imgserver


import (
"imgserver/storage"
"imgserver/object"
"imgserver/response"
)

func Process(obj *object.FileObject) (*response.Response) {
// check if parent exists
if obj.HasParent() {
// TODO head method
parent := storage.Get(obj.GetParent())
if parent.StatusCode == 404 {
return parent
}
}

res := storage.Get(obj)

return res

}
25 changes: 25 additions & 0 deletions src/imgserver/response/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package response


type Response struct{
StatusCode int
Body []byte
Error error
Headers map[string] string
}

func New(statusCode int, body []byte, err error) *Response{
res := Response{StatusCode: statusCode, Body: body, Error: err}
res.Headers = make(map[string] string)
if body == nil {
res.SetContentType("application/octet-stream")
} else {
res.SetContentType("application/json")
}
return &res
}

func (r *Response) SetContentType(contentType string) *Response {
r.Headers["content-type"] = contentType
return r
}
Loading

0 comments on commit e5eb93f

Please sign in to comment.