Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bc32bcd7b | ||
|
|
c8d4b9fd13 |
No files matched your search
@@ -267,10 +267,14 @@ func (p *ImageProcessor) Process(
|
||||
return nil, ErrInputTooLarge
|
||||
}
|
||||
|
||||
// Determine target dimensions; too large ones are refused before resizing
|
||||
targetWidth, targetHeight, err := targetDimensions(req.Size, origWidth, origHeight)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// Determine target dimensions
|
||||
targetWidth, targetHeight := targetDimensions(req.Size, origWidth, origHeight)
|
||||
|
||||
// A side given as 0 is computed from the input's proportions, so it can
|
||||
// come out above MaxOutputDimension; refuse it before resizing
|
||||
if targetWidth > MaxOutputDimension || targetHeight > MaxOutputDimension {
|
||||
return nil, fmt.Errorf("%w: %dx%d is above %d", ErrOutputTooLarge,
|
||||
targetWidth, targetHeight, MaxOutputDimension)
|
||||
}
|
||||
|
||||
// Resize if needed
|
||||
@@ -307,29 +311,21 @@ func (p *ImageProcessor) Process(
|
||||
|
||||
// targetDimensions calculates the output dimensions for a requested size,
|
||||
// scaling proportionally when only one dimension is given and keeping the
|
||||
// original dimensions when both are zero. A side scaled proportionally can
|
||||
// come out above MaxOutputDimension, which is ErrOutputTooLarge.
|
||||
func targetDimensions(size Size, origWidth, origHeight int) (int, int, error) {
|
||||
width, height := size.Width, size.Height
|
||||
|
||||
// original dimensions when both are zero.
|
||||
func targetDimensions(size Size, origWidth, origHeight int) (int, int) {
|
||||
switch {
|
||||
case width == 0 && height == 0:
|
||||
case size.Width == 0 && size.Height == 0:
|
||||
// Both are 0: keep original size
|
||||
return origWidth, origHeight, nil
|
||||
case width == 0:
|
||||
return origWidth, origHeight
|
||||
case size.Width == 0:
|
||||
// Only height specified: calculate width proportionally
|
||||
width = origWidth * height / origHeight
|
||||
case height == 0:
|
||||
return origWidth * size.Height / origHeight, size.Height
|
||||
case size.Height == 0:
|
||||
// Only width specified: calculate height proportionally
|
||||
height = origHeight * width / origWidth
|
||||
return size.Width, origHeight * size.Width / origWidth
|
||||
default:
|
||||
return size.Width, size.Height
|
||||
}
|
||||
|
||||
if width > MaxOutputDimension || height > MaxOutputDimension {
|
||||
return 0, 0, fmt.Errorf("%w: %dx%d is above %d", ErrOutputTooLarge,
|
||||
width, height, MaxOutputDimension)
|
||||
}
|
||||
|
||||
return width, height, nil
|
||||
}
|
||||
|
||||
// MIME types for the supported image formats.
|
||||
|
||||
@@ -22,12 +22,6 @@ import (
|
||||
func TestImageProxyRefusesComputedSideAboveMaximum(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// The upstream paths of the two sources
|
||||
const (
|
||||
widePath = "/wide.png"
|
||||
tallPath = "/tall.png"
|
||||
)
|
||||
|
||||
wide := encodeTestPNG(t, 3000, 600)
|
||||
tall := encodeTestPNG(t, 600, 3000)
|
||||
|
||||
@@ -35,7 +29,7 @@ func TestImageProxyRefusesComputedSideAboveMaximum(t *testing.T) {
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
|
||||
if r.URL.Path == tallPath {
|
||||
if r.URL.Path == "/tall.png" {
|
||||
_, _ = w.Write(tall)
|
||||
|
||||
return
|
||||
@@ -61,12 +55,12 @@ func TestImageProxyRefusesComputedSideAboveMaximum(t *testing.T) {
|
||||
// wantWidth and wantHeight are the served image's, 0 for a refusal
|
||||
wantWidth, wantHeight int
|
||||
}{
|
||||
{widePath, "0x8192", 0, 8192, 0, 0},
|
||||
{tallPath, "8192x0", 8192, 0, 0, 0},
|
||||
{widePath, "orig", 0, 0, 3000, 600},
|
||||
{widePath, "300x200", 300, 200, 300, 200},
|
||||
{widePath, "0x300", 0, 300, 1500, 300},
|
||||
{tallPath, "300x0", 300, 0, 300, 1500},
|
||||
{"/wide.png", "0x8192", 0, 8192, 0, 0},
|
||||
{"/tall.png", "8192x0", 8192, 0, 0, 0},
|
||||
{"/wide.png", "orig", 0, 0, 3000, 600},
|
||||
{"/wide.png", "300x200", 300, 200, 300, 200},
|
||||
{"/wide.png", "0x300", 0, 300, 1500, 300},
|
||||
{"/tall.png", "300x0", 300, 0, 300, 1500},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in new issue
Block a user