ivy

package module
v0.0.0-...-c733eee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2015 License: Apache-2.0 Imports: 15 Imported by: 0

README

Ivy godoc badge gocover badge Build Status Go Report Card

Assets & Image processing on the fly by GraphicsMagick

Installation

go get -u github.com/plimble/ivy

#####GraphicsMagick

Ubuntu

sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:rwky/graphicsmagick
sudo apt-get update
sudo apt-get install graphicsmagick

OSX

brew install graphicsmagick
Documentation
Sources
File System
	source := ivy.NewFileSystemSource("/path/to/asset")
	cache := ivy.NewFileSystemCache("/path/to/cache")
	processor := ivy.NewGMProcessor()

	config := &ivy.Config{
		IsDevelopment: false, //If false, Enable cache
		HTTPCache:     66000, //If > 0, Enable HTTP Cache
	}

	iv := ivy.New(source, cache, processor, config)
AWS S3
	source := ivy.NewS3Source("accessKey", "secretKey")
	cache := ivy.NewFileSystemCache("/path/to/cache")
	processor := ivy.NewGMProcessor()

	config := &ivy.Config{
		IsDevelopment: false, //If false, Enable cache
		HTTPCache:     66000, //If > 0, Enable HTTP Cache
	}

	iv := ivy.New(source, cache, processor, config)
Cache

You can use file system for caching or set nil for CDN like Cloudfront or disable caching

Server

You can run built-in server (ivy folder) or implement in your server

For more config

./ivy -h

NAME:
   Ivy - Assets & Image processing on the fly

USAGE:
   Ivy [global options] command [command options] [arguments...]

COMMANDS:
   help, h	Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --url, -u ":4900"	   server port
   --httpcache, -t "0"	   if cache enable this is http cache in second
   --cache, -c 		       enable cache, specific  eg, file
   --source, -s "file"	   source of image eg, file, s3
   --s3key 		           if source is s3, AWS S3 access key [$AWS_ACCESS_KEY]
   --s3secret 		       if source is s3, AWS S3 secret key [$AWS_SECRET_KEY]
   --sourceroot 	       if source is file, specific root path of image
   --cacheroot 		       if cache is file, specific root path of cache
   --help, -h		       show help
   --version, -v	       print the version
Ace Example
	a := ace.New()
	a.GET("/:bucket/:params/*path", func(c *ace.C) {
		iv.Get(
			c.Params.ByName("bucket"),
			c.Params.ByName("params"),
			c.Params.ByName("path"),
			c.Writer,
			c.Request,
		)
	})

	a.Run(":3000")

###Customs

Custom Source
	type Source interface {
		Load(bucket string, filename string) (*bytes.Buffer, error)
		GetFilePath(bucket string, filename string) string
	}
Custom Cache
	type Cache interface {
		Save(bucket, filename string, params *Params, file []byte) error
		Load(bucket, filename string, params *Params) (*bytes.Buffer, error)
		Delete(bucket, filename string, params *Params) error
		Flush(bucket string) error
	}
Custom Processor
	type Processor interface {
		Process(params *Params, path string, file *bytes.Buffer) (*bytes.Buffer, error)
	}
Example Request Image

Get image with original size or non image

http://localhost:3000/bucket/_/test.jpg
http://localhost:3000/bucket/0/test.jpg
Original image After image
before after

#####Resize 100x100

http://localhost:3000/bucket/r_100x100/test.jpg
Original image After image
before after

#####Resize width 100px aspect ratio

http://localhost:3000/bucket/r_100x0/test.jpg
Original image After image
before after

#####Crop image 200x200 with default gravity (NorthWest)

http://localhost:3000/bucket/c_200x200/test.jpg
Original image After image
before after

#####Crop with gravity East image 200x200

http://localhost:3000/bucket/c_200x200,g_e/test.jpg
Original image After image
before after

#####Resize 400x400 then crop 200x200 and gravity center

http://localhost:3000/bucket/r_400x400,c_200x200,g_c/test.jpg
Original image After image
before after

#####Quality 100

http://localhost:3000/bucket/q_100/test.jpg
Original image After image
before after

###Params Table

Param Description
r_{width}x{height} Resize image, if 0 is aspect ratio
c_{width}x{height} Crop image
g_{direction} Gravity image
q_{quality} Quality image maximum 100

###Gravity position

Param Description
nw North West
n North
ne North East
w West
c Center
e East
sw South West
s South
se South East

###Contributing

If you'd like to help out with the project. You can put up a Pull Request.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is error not found

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Save(bucket, filename string, params *Params, file []byte) error
	Load(bucket, filename string, params *Params) (*bytes.Buffer, error)
	Delete(bucket, filename string, params *Params) error
	Flush(bucket string) error
}

Cache interface

type Config

type Config struct {
	HTTPCache     int64 //Enable http cache in second
	IsDevelopment bool  //Enable dev mode, all cache will be disabled
}

Config config for ivy

type FileSystemCache

type FileSystemCache struct {
	// contains filtered or unexported fields
}

FileSystemCache is file system cache

func NewFileSystemCache

func NewFileSystemCache(root string) *FileSystemCache

NewFileSystemCache create file system cache

func (*FileSystemCache) Delete

func (fs *FileSystemCache) Delete(bucket, filename string, params *Params) error

Delete delete individual ache file

func (*FileSystemCache) Flush

func (fs *FileSystemCache) Flush(bucket string) error

Flush remove all cache file specific bucket

func (*FileSystemCache) Load

func (fs *FileSystemCache) Load(bucket, filename string, params *Params) (*bytes.Buffer, error)

Load file from file system return not found if file doesn't exist

func (*FileSystemCache) Save

func (fs *FileSystemCache) Save(bucket, filename string, params *Params, data []byte) error

Save file into file system

type FileSystemSource

type FileSystemSource struct {
	// contains filtered or unexported fields
}

FileSystemSource is file system source

func NewFileSystemSource

func NewFileSystemSource(root string) *FileSystemSource

NewFileSystemSource create new file source

func (*FileSystemSource) GetFilePath

func (fs *FileSystemSource) GetFilePath(bucket, filename string) string

GetFilePath get file path

func (*FileSystemSource) Load

func (fs *FileSystemSource) Load(bucket string, filename string) (*bytes.Buffer, error)

Load file from file system

type GMProcessor

type GMProcessor struct{}

GMProcessor is GraphicsMagick processor

func NewGMProcessor

func NewGMProcessor() *GMProcessor

NewGMProcessor create GraphicsMagick processor

func (*GMProcessor) Process

func (gm *GMProcessor) Process(params *Params, filePath string, file *bytes.Buffer) (*bytes.Buffer, error)

Process process image

type Ivy

type Ivy struct {
	Source    Source
	Cache     Cache
	Processor Processor
	Config    *Config
}

Ivy main struct of this package

func New

func New(source Source, cache Cache, processor Processor, config *Config) *Ivy

New ivy with config source is the assets location cache is the location where cache will be store, or set nil if disable cache

func (*Ivy) DeleteCache

func (iv *Ivy) DeleteCache(bucket, filename, paramsStr string) error

DeleteCache remove individual cache file

func (*Ivy) FlushCache

func (iv *Ivy) FlushCache(bucket string) error

FlushCache remove all cache file in specific bucket

func (*Ivy) Get

func (iv *Ivy) Get(bucket, paramsStr, path string, w http.ResponseWriter, r *http.Request)

Get get file or image if content type is image type and param not nil, process and return image if content type is image type and param nil, return raw image if content type is not image type, return raw file if cache enable, image type will cache. Next time return cache file if cache disable image type will not cahce, process and return image every time if HTTPCache is enable in config, return 304 status on next time

type Params

type Params struct {
	Width         int
	Height        int
	CropWidth     int
	CropHeight    int
	Gravity       string
	Quality       int
	EnableCrop    bool
	EnableResize  bool
	EnableGravity bool
	IsDefault     bool
	// contains filtered or unexported fields
}

Params for set up image

func NewParams

func NewParams() *Params

NewParams create default params

func ParseParams

func ParseParams(paramsStr string) (*Params, error)

ParseParams parse params string into params struct

func (*Params) String

func (p *Params) String() string

type Processor

type Processor interface {
	Process(params *Params, filePath string, file *bytes.Buffer) (*bytes.Buffer, error)
}

Processor interface for process image

type S3Source

type S3Source struct {
	// contains filtered or unexported fields
}

S3Source is aws s3 asset source

func NewS3Source

func NewS3Source(accessKey, secretKey string) *S3Source

NewS3Source create s3 source with access key and secret key

func (*S3Source) GetFilePath

func (fs *S3Source) GetFilePath(bucket, filename string) string

GetFilePath return filepath

func (*S3Source) Load

func (fs *S3Source) Load(bucket, filename string) (*bytes.Buffer, error)

Load s3 file

type Source

type Source interface {
	Load(bucket string, filename string) (*bytes.Buffer, error)
	GetFilePath(bucket string, filename string) string
}

Source interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL