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

scaleup flag

This commit is contained in:
Rune Madsen 2015-08-12 14:39:38 -04:00
parent f6e6a2cc86
commit 4e60c683b0
7 changed files with 57 additions and 15 deletions

View file

@ -45,6 +45,7 @@ var baseURL = flag.String("baseURL", "", "default base URL for relative remote U
var cacheDir = flag.String("cacheDir", "", "directory to use for file cache") var cacheDir = flag.String("cacheDir", "", "directory to use for file cache")
var cacheSize = flag.Uint64("cacheSize", 100, "maximum size of file cache (in MB)") var cacheSize = flag.Uint64("cacheSize", 100, "maximum size of file cache (in MB)")
var signatureKey = flag.String("signatureKey", "", "HMAC key used in calculating request signatures") var signatureKey = flag.String("signatureKey", "", "HMAC key used in calculating request signatures")
var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions")
var version = flag.Bool("version", false, "print version information") var version = flag.Bool("version", false, "print version information")
func main() { func main() {
@ -90,6 +91,8 @@ func main() {
} }
} }
p.ScaleUp = *scaleUp
server := &http.Server{ server := &http.Server{
Addr: *addr, Addr: *addr,
Handler: p, Handler: p,

View file

@ -65,6 +65,9 @@ type Options struct {
// HMAC Signature for signed requests. // HMAC Signature for signed requests.
Signature string Signature string
// Allow images to scale beyond their original dimensions.
ScaleUp bool
} }
var emptyOptions = Options{} var emptyOptions = Options{}

View file

@ -29,11 +29,11 @@ func TestOptions_String(t *testing.T) {
"0x0", "0x0",
}, },
{ {
Options{1, 2, true, 90, true, true, 80, ""}, Options{1, 2, true, 90, true, true, 80, "", false},
"1x2,fit,r90,fv,fh,q80", "1x2,fit,r90,fv,fh,q80",
}, },
{ {
Options{0.15, 1.3, false, 45, false, false, 95, "c0ffee"}, Options{0.15, 1.3, false, 45, false, false, 95, "c0ffee", false},
"0.15x1.3,r45,q95,sc0ffee", "0.15x1.3,r45,q95,sc0ffee",
}, },
} }
@ -82,8 +82,8 @@ func TestParseOptions(t *testing.T) {
{"FOO,1,BAR,r90,BAZ", Options{Width: 1, Height: 1, Rotate: 90}}, {"FOO,1,BAR,r90,BAZ", Options{Width: 1, Height: 1, Rotate: 90}},
// all flags, in different orders // all flags, in different orders
{"q70,1x2,fit,r90,fv,fh,sc0ffee", Options{1, 2, true, 90, true, true, 70, "c0ffee"}}, {"q70,1x2,fit,r90,fv,fh,sc0ffee", Options{1, 2, true, 90, true, true, 70, "c0ffee", false}},
{"r90,fh,sc0ffee,q90,1x2,fv,fit", Options{1, 2, true, 90, true, true, 90, "c0ffee"}}, {"r90,fh,sc0ffee,q90,1x2,fv,fit", Options{1, 2, true, 90, true, true, 90, "c0ffee", false}},
} }
for _, tt := range tests { for _, tt := range tests {

View file

@ -54,6 +54,9 @@ type Proxy struct {
// SignatureKey is the HMAC key used to verify signed requests. // SignatureKey is the HMAC key used to verify signed requests.
SignatureKey []byte SignatureKey []byte
// Allow images to scale beyond their original dimensions.
ScaleUp bool
} }
// NewProxy constructs a new proxy. The provided http RoundTripper will be // NewProxy constructs a new proxy. The provided http RoundTripper will be
@ -67,17 +70,20 @@ func NewProxy(transport http.RoundTripper, cache Cache) *Proxy {
cache = NopCache cache = NopCache
} }
proxy := Proxy{
Cache: cache,
}
client := new(http.Client) client := new(http.Client)
client.Transport = &httpcache.Transport{ client.Transport = &httpcache.Transport{
Transport: &TransformingTransport{transport, client}, Transport: &TransformingTransport{transport, client, &proxy},
Cache: cache, Cache: cache,
MarkCachedResponses: true, MarkCachedResponses: true,
} }
return &Proxy{ proxy.Client = client
Client: client,
Cache: cache, return &proxy
}
} }
// ServeHTTP handles image requests. // ServeHTTP handles image requests.
@ -232,6 +238,9 @@ type TransformingTransport struct {
// used rather than Transport directly in order to ensure that // used rather than Transport directly in order to ensure that
// responses are properly cached. // responses are properly cached.
CachingClient *http.Client CachingClient *http.Client
// Proxy is used to access command line flag settings during roundtripping.
Proxy *Proxy
} }
// RoundTrip implements the http.RoundTripper interface. // RoundTrip implements the http.RoundTripper interface.
@ -256,6 +265,12 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
} }
opt := ParseOptions(req.URL.Fragment) opt := ParseOptions(req.URL.Fragment)
// assign static settings from proxy to options
if t.Proxy != nil {
opt.ScaleUp = t.Proxy.ScaleUp
}
img, err := Transform(b, opt) img, err := Transform(b, opt)
if err != nil { if err != nil {
glog.Errorf("error transforming image: %v", err) glog.Errorf("error transforming image: %v", err)

View file

@ -12,6 +12,8 @@ import (
"net/url" "net/url"
"strings" "strings"
"testing" "testing"
"github.com/gregjones/httpcache"
) )
func TestAllowed(t *testing.T) { func TestAllowed(t *testing.T) {
@ -187,6 +189,15 @@ func TestCheck304(t *testing.T) {
} }
} }
// make sure that the proxy is passed to transport in order
// to access the command line flags.
func TestProxyPointer(t *testing.T) {
p := NewProxy(nil, nil)
if p.Client.Transport.(*httpcache.Transport).Transport.(*TransformingTransport).Proxy != p {
t.Errorf("Transport doesnt have proxy pointer")
}
}
// testTransport is an http.RoundTripper that returns certained canned // testTransport is an http.RoundTripper that returns certained canned
// responses for particular requests. // responses for particular requests.
type testTransport struct{} type testTransport struct{}
@ -272,7 +283,10 @@ func TestProxy_ServeHTTP_is304(t *testing.T) {
func TestTransformingTransport(t *testing.T) { func TestTransformingTransport(t *testing.T) {
client := new(http.Client) client := new(http.Client)
tr := &TransformingTransport{testTransport{}, client} tr := &TransformingTransport{
Transport: testTransport{},
CachingClient: client,
}
client.Transport = tr client.Transport = tr
tests := []struct { tests := []struct {

View file

@ -102,12 +102,14 @@ func transformImage(m image.Image, opt Options) image.Image {
} }
// never resize larger than the original image // never resize larger than the original image
if !opt.ScaleUp {
if w > imgW { if w > imgW {
w = imgW w = imgW
} }
if h > imgH { if h > imgH {
h = imgH h = imgH
} }
}
// resize // resize
if w != 0 || h != 0 { if w != 0 || h != 0 {

View file

@ -127,6 +127,11 @@ func TestTransformImage(t *testing.T) {
Options{Width: 100, Height: 100}, Options{Width: 100, Height: 100},
ref, ref,
}, },
{ // can resize larger than original image
ref,
Options{Width: 4, Height: 4, ScaleUp: true},
newImage(4, 4, red, red, green, green, red, red, green, green, blue, blue, yellow, yellow, blue, blue, yellow, yellow),
},
{ // invalid values { // invalid values
ref, ref,
Options{Width: -1, Height: -1}, Options{Width: -1, Height: -1},