2013-12-04 03:37:13 -05:00
|
|
|
// Package data provides common shared data structures for go-imageproxy.
|
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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-04 05:55:56 -05:00
|
|
|
Width int // requested width, in pixels
|
|
|
|
Height int // 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-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
|
2013-12-05 02:12:44 -05:00
|
|
|
func (o Options) String() string {
|
2013-12-04 03:37:13 -05:00
|
|
|
return fmt.Sprintf("%dx%d", o.Width, o.Height)
|
|
|
|
}
|
|
|
|
|
2013-12-06 14:01:34 -05:00
|
|
|
func ParseOptions(str string) *Options {
|
|
|
|
o := new(Options)
|
2013-12-04 03:37:13 -05:00
|
|
|
var h, w string
|
|
|
|
|
2013-12-06 14:01:34 -05:00
|
|
|
parts := strings.Split(str, ",")
|
|
|
|
|
|
|
|
// parse size
|
|
|
|
size := strings.SplitN(parts[0], "x", 2)
|
2013-12-04 03:37:13 -05:00
|
|
|
w = size[0]
|
|
|
|
if len(size) > 1 {
|
|
|
|
h = size[1]
|
|
|
|
} else {
|
|
|
|
h = w
|
|
|
|
}
|
|
|
|
|
|
|
|
if w != "" {
|
2013-12-06 14:01:34 -05:00
|
|
|
o.Width, _ = strconv.Atoi(w)
|
2013-12-04 03:37:13 -05:00
|
|
|
}
|
|
|
|
if h != "" {
|
2013-12-06 14:01:34 -05:00
|
|
|
o.Height, _ = strconv.Atoi(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, part := range parts[1:] {
|
|
|
|
if part == "fit" {
|
|
|
|
o.Fit = true
|
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
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|