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.
|
|
|
|
|
2013-12-26 14:14:28 -05:00
|
|
|
// Package data provides common shared data structures for imageproxy.
|
2013-12-26 15:33:22 -05:00
|
|
|
package proxy
|
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"
|
2013-12-04 05:55:56 -05:00
|
|
|
"time"
|
2013-12-04 03:37:13 -05:00
|
|
|
)
|
|
|
|
|
2013-12-05 02:12:44 -05:00
|
|
|
// Options specifies transformations that can be performed on a
|
2013-12-04 03:37:13 -05:00
|
|
|
// requested image.
|
2013-12-05 02:12:44 -05:00
|
|
|
type Options struct {
|
2013-12-06 18:03:17 -05:00
|
|
|
Width float64 // requested width, in pixels
|
|
|
|
Height float64 // requested height, in pixels
|
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
|
|
|
|
|
|
|
// Rotate image the specified degrees counter-clockwise. Valid values are 90, 180, 270.
|
|
|
|
Rotate int
|
2013-12-07 01:18:44 -05:00
|
|
|
|
|
|
|
FlipVertical bool
|
|
|
|
FlipHorizontal bool
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
|
2013-12-26 18:04:14 -05:00
|
|
|
var emptyOptions = new(Options)
|
|
|
|
|
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)
|
|
|
|
fmt.Fprintf(buf, "%vx%v", o.Width, o.Height)
|
|
|
|
if o.Fit {
|
|
|
|
buf.WriteString(",fit")
|
|
|
|
}
|
|
|
|
if o.Rotate != 0 {
|
|
|
|
fmt.Fprintf(buf, ",r%d", o.Rotate)
|
|
|
|
}
|
|
|
|
if o.FlipVertical {
|
|
|
|
buf.WriteString(",fv")
|
|
|
|
}
|
|
|
|
if o.FlipHorizontal {
|
|
|
|
buf.WriteString(",fh")
|
|
|
|
}
|
|
|
|
return buf.String()
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
|
2013-12-06 14:01:34 -05:00
|
|
|
func ParseOptions(str string) *Options {
|
|
|
|
o := new(Options)
|
2013-12-04 03:37:13 -05:00
|
|
|
|
2013-12-06 14:01:34 -05:00
|
|
|
parts := strings.Split(str, ",")
|
2013-12-26 16:35:23 -05:00
|
|
|
for _, part := range parts {
|
2013-12-06 14:01:34 -05:00
|
|
|
if part == "fit" {
|
|
|
|
o.Fit = true
|
2013-12-06 21:03:16 -05:00
|
|
|
continue
|
|
|
|
}
|
2013-12-07 01:18:44 -05:00
|
|
|
if part == "fv" {
|
|
|
|
o.FlipVertical = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if part == "fh" {
|
|
|
|
o.FlipHorizontal = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-12-26 16:35:23 -05:00
|
|
|
if len(part) > 2 && part[:1] == "r" {
|
2013-12-07 20:06:36 -05:00
|
|
|
o.Rotate, _ = strconv.Atoi(part[1:])
|
2013-12-06 21:03:16 -05:00
|
|
|
continue
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
2013-12-26 16:35:23 -05:00
|
|
|
|
|
|
|
if strings.ContainsRune(part, 'x') {
|
|
|
|
var h, w string
|
|
|
|
size := strings.SplitN(part, "x", 2)
|
|
|
|
w = size[0]
|
|
|
|
if len(size) > 1 {
|
|
|
|
h = size[1]
|
|
|
|
} else {
|
|
|
|
h = w
|
|
|
|
}
|
|
|
|
|
|
|
|
if w != "" {
|
|
|
|
o.Width, _ = strconv.ParseFloat(w, 64)
|
|
|
|
}
|
|
|
|
if h != "" {
|
|
|
|
o.Height, _ = strconv.ParseFloat(h, 64)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if size, err := strconv.ParseFloat(part, 64); err == nil {
|
|
|
|
o.Width = size
|
|
|
|
o.Height = size
|
|
|
|
continue
|
|
|
|
}
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
|
2013-12-06 14:01:34 -05:00
|
|
|
return o
|
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
|
|
|
|
Options *Options // Image transformation to perform
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
2013-12-04 05:55:56 -05:00
|
|
|
|
2013-12-26 17:38:15 -05:00
|
|
|
// NewRequest parses an http.Request into an image request.
|
|
|
|
func NewRequest(r *http.Request) (*Request, error) {
|
|
|
|
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() {
|
|
|
|
// first segment is likely options
|
|
|
|
parts := strings.SplitN(path, "/", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, URLError{"too few path segments", r.URL}
|
|
|
|
}
|
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
|
|
|
if !req.URL.IsAbs() {
|
|
|
|
return nil, URLError{"must provide absolute remote URL", r.URL}
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
|
|
|
|
return nil, URLError{"remote URL must have http or https URL", r.URL}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query string is always part of the remote URL
|
|
|
|
req.URL.RawQuery = r.URL.RawQuery
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2013-12-04 05:55:56 -05:00
|
|
|
// Image represents a remote image that is being proxied. It tracks where
|
|
|
|
// the image was originally retrieved from and how long the image can be cached.
|
|
|
|
type Image struct {
|
|
|
|
// URL of original remote image.
|
|
|
|
URL string
|
|
|
|
|
|
|
|
// Expires is the cache expiration time for the original image, as
|
|
|
|
// returned by the remote server.
|
|
|
|
Expires time.Time
|
|
|
|
|
|
|
|
// Etag returned from server when fetching image.
|
|
|
|
Etag string
|
|
|
|
|
|
|
|
// Bytes contains the actual image.
|
|
|
|
Bytes []byte
|
|
|
|
}
|