0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-16 21:56:43 -05:00

collapse data and transform packages into proxy

This commit is contained in:
Will Norris 2013-12-26 12:33:22 -08:00
parent d8aa853b3d
commit fc8552e6b4
4 changed files with 19 additions and 26 deletions

View file

@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
// Package data provides common shared data structures for imageproxy. // Package data provides common shared data structures for imageproxy.
package data package proxy
import ( import (
"fmt" "fmt"

View file

@ -27,8 +27,6 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"github.com/gregjones/httpcache" "github.com/gregjones/httpcache"
"github.com/willnorris/imageproxy/data"
"github.com/willnorris/imageproxy/transform"
) )
// URLError reports a malformed URL error. // URLError reports a malformed URL error.
@ -42,9 +40,9 @@ func (e URLError) Error() string {
} }
// NewRequest parses an http.Request into an image request. // 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 var err error
req := new(data.Request) req := new(Request)
path := r.URL.Path[1:] // strip leading slash path := r.URL.Path[1:] // strip leading slash
req.URL, err = url.Parse(path) 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} 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() { if !req.URL.IsAbs() {
@ -142,7 +140,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
b, _ := transform.Transform(image.Bytes, req.Options) b, _ := Transform(image.Bytes, req.Options)
image.Bytes = b image.Bytes = b
w.Header().Add("Content-Length", strconv.Itoa(len(image.Bytes))) 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) 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) resp, err := p.Client.Get(u)
if err != nil { if err != nil {
return nil, err return nil, err
@ -166,7 +164,7 @@ func (p *Proxy) fetchRemoteImage(u string) (*data.Image, error) {
return nil, err return nil, err
} }
return &data.Image{ return &Image{
URL: u, URL: u,
Expires: parseExpires(resp), Expires: parseExpires(resp),
Etag: resp.Header.Get("Etag"), Etag: resp.Header.Get("Etag"),

View file

@ -18,17 +18,13 @@ import (
"net/http" "net/http"
"reflect" "reflect"
"testing" "testing"
"github.com/willnorris/imageproxy/data"
) )
var emptyOptions = new(data.Options)
func TestNewRequest(t *testing.T) { func TestNewRequest(t *testing.T) {
tests := []struct { tests := []struct {
URL string URL string
RemoteURL string RemoteURL string
Options *data.Options Options *Options
ExpectError bool ExpectError bool
}{ }{
// invalid URLs // invalid URLs
@ -52,7 +48,7 @@ func TestNewRequest(t *testing.T) {
}, },
{ {
"http://localhost/1xs/http://example.com/", "http://localhost/1xs/http://example.com/",
"http://example.com/", &data.Options{Width: 1}, false, "http://example.com/", &Options{Width: 1}, false,
}, },
// valid URLs // valid URLs
@ -84,27 +80,27 @@ func TestNewRequest(t *testing.T) {
}, },
{ {
"http://localhost/1x/http://example.com/", "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://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://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://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://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://localhost/1x2,fit,r90,fv,fh/http://example.com/",
"http://example.com/", &data.Options{1, 2, true, 90, true, true}, false, "http://example.com/", &Options{1, 2, true, 90, true, true}, false,
}, },
} }

View file

@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
// Package transform handles image transformation such as resizing. // Package transform handles image transformation such as resizing.
package transform package proxy
import ( import (
"bytes" "bytes"
@ -24,13 +24,12 @@ import (
"reflect" "reflect"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/willnorris/imageproxy/data"
) )
var emptyOptions = new(data.Options) var emptyOptions = new(Options)
// Transform the provided image. // 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) { if opt == nil || reflect.DeepEqual(opt, emptyOptions) {
// bail if no transformation was requested // bail if no transformation was requested
return img, nil return img, nil