2013-12-06 20:40:35 -05:00
|
|
|
// Copyright 2013 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-07-30 20:23:43 -05:00
|
|
|
package imageproxy
|
2013-12-04 03:37:13 -05:00
|
|
|
|
|
|
|
import (
|
2013-12-26 15:50:22 -05:00
|
|
|
"bytes"
|
2013-12-04 03:37:13 -05:00
|
|
|
"fmt"
|
2013-12-26 17:38:15 -05:00
|
|
|
"net/http"
|
2013-12-04 03:37:13 -05:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-01-25 03:30:15 -05:00
|
|
|
const (
|
|
|
|
optFit = "fit"
|
|
|
|
optFlipVertical = "fv"
|
|
|
|
optFlipHorizontal = "fh"
|
|
|
|
optRotatePrefix = "r"
|
2015-01-12 12:32:00 -05:00
|
|
|
optQualityPrefix = "q"
|
2015-01-25 03:30:15 -05:00
|
|
|
optSizeDelimiter = "x"
|
|
|
|
)
|
|
|
|
|
2013-12-26 21:31:20 -05:00
|
|
|
// URLError reports a malformed URL error.
|
|
|
|
type URLError struct {
|
|
|
|
Message string
|
|
|
|
URL *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e URLError) Error() string {
|
|
|
|
return fmt.Sprintf("malformed URL %q: %s", e.URL, e.Message)
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:34:39 -05:00
|
|
|
// Options specifies transformations to be performed on the requested image.
|
2013-12-05 02:12:44 -05:00
|
|
|
type Options struct {
|
2014-11-23 18:34:39 -05:00
|
|
|
// See ParseOptions for interpretation of Width and Height values
|
|
|
|
Width float64
|
|
|
|
Height float64
|
2013-12-06 14:01:34 -05:00
|
|
|
|
|
|
|
// If true, resize the image to fit in the specified dimensions. Image
|
|
|
|
// will not be cropped, and aspect ratio will be maintained.
|
|
|
|
Fit bool
|
2013-12-06 21:03:16 -05:00
|
|
|
|
2014-11-23 18:34:39 -05:00
|
|
|
// Rotate image the specified degrees counter-clockwise. Valid values
|
|
|
|
// are 90, 180, 270.
|
2013-12-06 21:03:16 -05:00
|
|
|
Rotate int
|
2013-12-07 01:18:44 -05:00
|
|
|
|
|
|
|
FlipVertical bool
|
|
|
|
FlipHorizontal bool
|
2015-01-12 12:32:00 -05:00
|
|
|
|
|
|
|
// Quality of output image
|
|
|
|
Quality int
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
|
2014-11-20 00:59:52 -05:00
|
|
|
var emptyOptions = Options{}
|
2013-12-26 18:04:14 -05:00
|
|
|
|
2013-12-05 02:12:44 -05:00
|
|
|
func (o Options) String() string {
|
2013-12-26 15:50:22 -05:00
|
|
|
buf := new(bytes.Buffer)
|
2015-01-25 03:30:15 -05:00
|
|
|
fmt.Fprintf(buf, "%v%s%v", o.Width, optSizeDelimiter, o.Height)
|
2013-12-26 15:50:22 -05:00
|
|
|
if o.Fit {
|
2015-01-25 03:30:15 -05:00
|
|
|
fmt.Fprintf(buf, ",%s", optFit)
|
2013-12-26 15:50:22 -05:00
|
|
|
}
|
|
|
|
if o.Rotate != 0 {
|
2015-01-25 03:30:15 -05:00
|
|
|
fmt.Fprintf(buf, ",%s%d", string(optRotatePrefix), o.Rotate)
|
2013-12-26 15:50:22 -05:00
|
|
|
}
|
|
|
|
if o.FlipVertical {
|
2015-01-25 03:30:15 -05:00
|
|
|
fmt.Fprintf(buf, ",%s", optFlipVertical)
|
2013-12-26 15:50:22 -05:00
|
|
|
}
|
|
|
|
if o.FlipHorizontal {
|
2015-01-25 03:30:15 -05:00
|
|
|
fmt.Fprintf(buf, ",%s", optFlipHorizontal)
|
2013-12-26 15:50:22 -05:00
|
|
|
}
|
2015-01-12 12:32:00 -05:00
|
|
|
fmt.Fprintf(buf, ",%s%d", string(optQualityPrefix), o.Quality)
|
2013-12-26 15:50:22 -05:00
|
|
|
return buf.String()
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
|
2014-11-23 18:34:39 -05:00
|
|
|
// ParseOptions parses str as a list of comma separated transformation options.
|
|
|
|
// The following options can be specified in any order:
|
|
|
|
//
|
|
|
|
// Size and Cropping
|
|
|
|
//
|
|
|
|
// The size option takes the general form "{width}x{height}", where width and
|
|
|
|
// height are numbers. Integer values greater than 1 are interpreted as exact
|
|
|
|
// pixel values. Floats between 0 and 1 are interpreted as percentages of the
|
|
|
|
// original image size. If either value is omitted or set to 0, it will be
|
|
|
|
// automatically set to preserve the aspect ratio based on the other dimension.
|
|
|
|
// If a single number is provided (with no "x" separator), it will be used for
|
|
|
|
// both height and width.
|
|
|
|
//
|
|
|
|
// Depending on the size options specified, an image may be cropped to fit the
|
|
|
|
// requested size. In all cases, the original aspect ratio of the image will be
|
|
|
|
// preserved; imageproxy will never stretch the original image.
|
|
|
|
//
|
|
|
|
// When no explicit crop mode is specified, the following rules are followed:
|
|
|
|
//
|
|
|
|
// - If both width and height values are specified, the image will be scaled to
|
|
|
|
// fill the space, cropping if necessary to fit the exact dimension.
|
|
|
|
//
|
|
|
|
// - If only one of the width or height values is specified, the image will be
|
|
|
|
// resized to fit the specified dimension, scaling the other dimension as
|
|
|
|
// needed to maintain the aspect ratio.
|
|
|
|
//
|
|
|
|
// If the "fit" option is specified together with a width and height value, the
|
|
|
|
// image will be resized to fit within a containing box of the specified size.
|
|
|
|
// As always, the original aspect ratio will be preserved. Specifying the "fit"
|
|
|
|
// option with only one of either width or height does the same thing as if
|
|
|
|
// "fit" had not been specified.
|
|
|
|
//
|
|
|
|
// Rotation and Flips
|
|
|
|
//
|
|
|
|
// The "r{degrees}" option will rotate the image the specified number of
|
|
|
|
// degrees, counter-clockwise. Valid degrees values are 90, 180, and 270.
|
|
|
|
//
|
|
|
|
// The "fv" option will flip the image vertically. The "fh" option will flip
|
|
|
|
// the image horizontally. Images are flipped after being rotated.
|
|
|
|
//
|
2015-01-12 12:32:00 -05:00
|
|
|
// Quality
|
|
|
|
//
|
|
|
|
// The "q{qualityPercentage}" option can be used to specify the quality of the
|
|
|
|
// output file (JPEG only)
|
|
|
|
//
|
2014-11-23 18:34:39 -05:00
|
|
|
// Examples
|
|
|
|
//
|
|
|
|
// 0x0 - no resizing
|
|
|
|
// 200x - 200 pixels wide, proportional height
|
|
|
|
// 0.15x - 15% original width, proportional height
|
|
|
|
// x100 - 100 pixels tall, proportional width
|
|
|
|
// 100x150 - 100 by 150 pixels, cropping as needed
|
|
|
|
// 100 - 100 pixels square, cropping as needed
|
|
|
|
// 150,fit - scale to fit 150 pixels square, no cropping
|
|
|
|
// 100,r90 - 100 pixels square, rotated 90 degrees
|
|
|
|
// 100,fv,fh - 100 pixels square, flipped horizontal and vertical
|
2015-01-12 12:32:00 -05:00
|
|
|
// 200x,q80 - 200 pixels wide, proportional height, 80% quality
|
2014-11-20 00:59:52 -05:00
|
|
|
func ParseOptions(str string) Options {
|
2015-05-04 12:26:22 -05:00
|
|
|
var options Options
|
2014-11-23 18:34:39 -05:00
|
|
|
|
|
|
|
for _, opt := range strings.Split(str, ",") {
|
|
|
|
switch {
|
2015-01-25 03:30:15 -05:00
|
|
|
case len(opt) == 0:
|
|
|
|
break
|
|
|
|
case opt == optFit:
|
2014-11-23 18:34:39 -05:00
|
|
|
options.Fit = true
|
2015-01-25 03:30:15 -05:00
|
|
|
case opt == optFlipVertical:
|
2014-11-23 18:34:39 -05:00
|
|
|
options.FlipVertical = true
|
2015-01-25 03:30:15 -05:00
|
|
|
case opt == optFlipHorizontal:
|
2014-11-23 18:34:39 -05:00
|
|
|
options.FlipHorizontal = true
|
2015-01-25 03:30:15 -05:00
|
|
|
case strings.HasPrefix(opt, optRotatePrefix):
|
|
|
|
value := strings.TrimPrefix(opt, optRotatePrefix)
|
|
|
|
options.Rotate, _ = strconv.Atoi(value)
|
2015-01-12 12:32:00 -05:00
|
|
|
case strings.HasPrefix(opt, optQualityPrefix):
|
|
|
|
value := strings.TrimPrefix(opt, optQualityPrefix)
|
|
|
|
options.Quality, _ = strconv.Atoi(value)
|
2015-01-25 03:30:15 -05:00
|
|
|
case strings.Contains(opt, optSizeDelimiter):
|
|
|
|
size := strings.SplitN(opt, optSizeDelimiter, 2)
|
2014-11-23 18:34:39 -05:00
|
|
|
if w := size[0]; w != "" {
|
|
|
|
options.Width, _ = strconv.ParseFloat(w, 64)
|
2013-12-26 16:35:23 -05:00
|
|
|
}
|
2014-11-23 18:34:39 -05:00
|
|
|
if h := size[1]; h != "" {
|
|
|
|
options.Height, _ = strconv.ParseFloat(h, 64)
|
2013-12-26 16:35:23 -05:00
|
|
|
}
|
2014-11-23 18:34:39 -05:00
|
|
|
default:
|
|
|
|
if size, err := strconv.ParseFloat(opt, 64); err == nil {
|
|
|
|
options.Width = size
|
|
|
|
options.Height = size
|
2013-12-26 16:35:23 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
|
2014-11-23 18:34:39 -05:00
|
|
|
return options
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
|
2014-11-23 18:34:39 -05:00
|
|
|
// Request is an imageproxy request which includes a remote URL of an image to
|
|
|
|
// proxy, and an optional set of transformations to perform.
|
2013-12-04 03:37:13 -05:00
|
|
|
type Request struct {
|
2013-12-05 02:12:44 -05:00
|
|
|
URL *url.URL // URL of the image to proxy
|
2014-11-20 00:59:52 -05:00
|
|
|
Options Options // Image transformation to perform
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
2013-12-04 05:55:56 -05:00
|
|
|
|
2015-05-11 19:12:03 -05:00
|
|
|
// String returns the request URL as a string, with r.Options encoded in the
|
|
|
|
// URL fragment.
|
|
|
|
func (r Request) String() string {
|
|
|
|
u := *r.URL
|
|
|
|
u.Fragment = r.Options.String()
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
2014-11-23 18:34:39 -05:00
|
|
|
// NewRequest parses an http.Request into an imageproxy Request. Options and
|
|
|
|
// the remote image URL are specified in the request path, formatted as:
|
|
|
|
// /{options}/{remote_url}. Options may be omitted, so a request path may
|
|
|
|
// simply contian /{remote_url}. The remote URL must be an absolute "http" or
|
|
|
|
// "https" URL, should not be URL encoded, and may contain a query string.
|
|
|
|
//
|
|
|
|
// Assuming an imageproxy server running on localhost, the following are all
|
|
|
|
// valid imageproxy requests:
|
|
|
|
//
|
|
|
|
// http://localhost/100x200/http://example.com/image.jpg
|
|
|
|
// http://localhost/100x200,r90/http://example.com/image.jpg?foo=bar
|
|
|
|
// http://localhost//http://example.com/image.jpg
|
|
|
|
// http://localhost/http://example.com/image.jpg
|
2015-02-20 00:34:22 -05:00
|
|
|
func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
|
2013-12-26 17:38:15 -05:00
|
|
|
var err error
|
|
|
|
req := new(Request)
|
|
|
|
|
|
|
|
path := r.URL.Path[1:] // strip leading slash
|
|
|
|
req.URL, err = url.Parse(path)
|
|
|
|
if err != nil || !req.URL.IsAbs() {
|
2014-11-23 18:34:39 -05:00
|
|
|
// first segment should be options
|
2013-12-26 17:38:15 -05:00
|
|
|
parts := strings.SplitN(path, "/", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, URLError{"too few path segments", r.URL}
|
|
|
|
}
|
|
|
|
|
2015-05-04 12:26:22 -05:00
|
|
|
var err error
|
2013-12-26 17:38:15 -05:00
|
|
|
req.URL, err = url.Parse(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Options = ParseOptions(parts[0])
|
|
|
|
}
|
|
|
|
|
2015-02-20 00:34:22 -05:00
|
|
|
if baseURL != nil {
|
|
|
|
req.URL = baseURL.ResolveReference(req.URL)
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:38:15 -05:00
|
|
|
if !req.URL.IsAbs() {
|
|
|
|
return nil, URLError{"must provide absolute remote URL", r.URL}
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
|
2014-11-21 20:56:59 -05:00
|
|
|
return nil, URLError{"remote URL must have http or https scheme", r.URL}
|
2013-12-26 17:38:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// query string is always part of the remote URL
|
|
|
|
req.URL.RawQuery = r.URL.RawQuery
|
|
|
|
return req, nil
|
|
|
|
}
|