From fc8552e6b4215e2a3129c41fd614cbcce82bc67c Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 26 Dec 2013 12:33:22 -0800 Subject: [PATCH] collapse data and transform packages into proxy --- {data => proxy}/data.go | 2 +- proxy/proxy.go | 14 ++++++-------- proxy/proxy_test.go | 22 +++++++++------------- {transform => proxy}/transform.go | 7 +++---- 4 files changed, 19 insertions(+), 26 deletions(-) rename {data => proxy}/data.go (99%) rename {transform => proxy}/transform.go (93%) diff --git a/data/data.go b/proxy/data.go similarity index 99% rename from data/data.go rename to proxy/data.go index ee52cdc..76fb0cd 100644 --- a/data/data.go +++ b/proxy/data.go @@ -13,7 +13,7 @@ // limitations under the License. // Package data provides common shared data structures for imageproxy. -package data +package proxy import ( "fmt" diff --git a/proxy/proxy.go b/proxy/proxy.go index 85d8682..df0faa6 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -27,8 +27,6 @@ import ( "github.com/golang/glog" "github.com/gregjones/httpcache" - "github.com/willnorris/imageproxy/data" - "github.com/willnorris/imageproxy/transform" ) // URLError reports a malformed URL error. @@ -42,9 +40,9 @@ func (e URLError) Error() string { } // NewRequest parses an http.Request into an image request. -func NewRequest(r *http.Request) (*data.Request, error) { +func NewRequest(r *http.Request) (*Request, error) { var err error - req := new(data.Request) + req := new(Request) path := r.URL.Path[1:] // strip leading slash req.URL, err = url.Parse(path) @@ -60,7 +58,7 @@ func NewRequest(r *http.Request) (*data.Request, error) { return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL} } - req.Options = data.ParseOptions(parts[0]) + req.Options = ParseOptions(parts[0]) } if !req.URL.IsAbs() { @@ -142,7 +140,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - b, _ := transform.Transform(image.Bytes, req.Options) + b, _ := Transform(image.Bytes, req.Options) image.Bytes = b w.Header().Add("Content-Length", strconv.Itoa(len(image.Bytes))) @@ -150,7 +148,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write(image.Bytes) } -func (p *Proxy) fetchRemoteImage(u string) (*data.Image, error) { +func (p *Proxy) fetchRemoteImage(u string) (*Image, error) { resp, err := p.Client.Get(u) if err != nil { return nil, err @@ -166,7 +164,7 @@ func (p *Proxy) fetchRemoteImage(u string) (*data.Image, error) { return nil, err } - return &data.Image{ + return &Image{ URL: u, Expires: parseExpires(resp), Etag: resp.Header.Get("Etag"), diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index e24d9c7..22138ab 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -18,17 +18,13 @@ import ( "net/http" "reflect" "testing" - - "github.com/willnorris/imageproxy/data" ) -var emptyOptions = new(data.Options) - func TestNewRequest(t *testing.T) { tests := []struct { URL string RemoteURL string - Options *data.Options + Options *Options ExpectError bool }{ // invalid URLs @@ -52,7 +48,7 @@ func TestNewRequest(t *testing.T) { }, { "http://localhost/1xs/http://example.com/", - "http://example.com/", &data.Options{Width: 1}, false, + "http://example.com/", &Options{Width: 1}, false, }, // valid URLs @@ -84,27 +80,27 @@ func TestNewRequest(t *testing.T) { }, { "http://localhost/1x/http://example.com/", - "http://example.com/", &data.Options{1, 0, false, 0, false, false}, false, + "http://example.com/", &Options{1, 0, false, 0, false, false}, false, }, { "http://localhost/x1/http://example.com/", - "http://example.com/", &data.Options{0, 1, false, 0, false, false}, false, + "http://example.com/", &Options{0, 1, false, 0, false, false}, false, }, { "http://localhost/1x2/http://example.com/", - "http://example.com/", &data.Options{1, 2, false, 0, false, false}, false, + "http://example.com/", &Options{1, 2, false, 0, false, false}, false, }, { "http://localhost/0.1x0.2/http://example.com/", - "http://example.com/", &data.Options{0.1, 0.2, false, 0, false, false}, false, + "http://example.com/", &Options{0.1, 0.2, false, 0, false, false}, false, }, { "http://localhost/,fit/http://example.com/", - "http://example.com/", &data.Options{0, 0, true, 0, false, false}, false, + "http://example.com/", &Options{0, 0, true, 0, false, false}, false, }, { - "http://localhost/1x2,fit,r=90,fv,fh/http://example.com/", - "http://example.com/", &data.Options{1, 2, true, 90, true, true}, false, + "http://localhost/1x2,fit,r90,fv,fh/http://example.com/", + "http://example.com/", &Options{1, 2, true, 90, true, true}, false, }, } diff --git a/transform/transform.go b/proxy/transform.go similarity index 93% rename from transform/transform.go rename to proxy/transform.go index 79de32e..2f080a1 100644 --- a/transform/transform.go +++ b/proxy/transform.go @@ -13,7 +13,7 @@ // limitations under the License. // Package transform handles image transformation such as resizing. -package transform +package proxy import ( "bytes" @@ -24,13 +24,12 @@ import ( "reflect" "github.com/disintegration/imaging" - "github.com/willnorris/imageproxy/data" ) -var emptyOptions = new(data.Options) +var emptyOptions = new(Options) // Transform the provided image. -func Transform(img []byte, opt *data.Options) ([]byte, error) { +func Transform(img []byte, opt *Options) ([]byte, error) { if opt == nil || reflect.DeepEqual(opt, emptyOptions) { // bail if no transformation was requested return img, nil