github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/google.golang.org/appengine/image/image.go (about)

     1  // Copyright 2012 Google Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package image provides image services.
     6  package image
     7  
     8  import (
     9  	"fmt"
    10  	"net/url"
    11  
    12  	"golang.org/x/net/context"
    13  
    14  	"google.golang.org/appengine"
    15  	"google.golang.org/appengine/internal"
    16  	pb "google.golang.org/appengine/internal/image"
    17  )
    18  
    19  type ServingURLOptions struct {
    20  	Secure bool // whether the URL should use HTTPS
    21  
    22  	// Size must be between zero and 1600.
    23  	// If Size is non-zero, a resized version of the image is served,
    24  	// and Size is the served image's longest dimension. The aspect ratio is preserved.
    25  	// If Crop is true the image is cropped from the center instead of being resized.
    26  	Size int
    27  	Crop bool
    28  }
    29  
    30  // ServingURL returns a URL that will serve an image from Blobstore.
    31  func ServingURL(c context.Context, key appengine.BlobKey, opts *ServingURLOptions) (*url.URL, error) {
    32  	req := &pb.ImagesGetUrlBaseRequest{
    33  		BlobKey: (*string)(&key),
    34  	}
    35  	if opts != nil && opts.Secure {
    36  		req.CreateSecureUrl = &opts.Secure
    37  	}
    38  	res := &pb.ImagesGetUrlBaseResponse{}
    39  	if err := internal.Call(c, "images", "GetUrlBase", req, res); err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	// The URL may have suffixes added to dynamically resize or crop:
    44  	// - adding "=s32" will serve the image resized to 32 pixels, preserving the aspect ratio.
    45  	// - adding "=s32-c" is the same as "=s32" except it will be cropped.
    46  	u := *res.Url
    47  	if opts != nil && opts.Size > 0 {
    48  		u += fmt.Sprintf("=s%d", opts.Size)
    49  		if opts.Crop {
    50  			u += "-c"
    51  		}
    52  	}
    53  	return url.Parse(u)
    54  }
    55  
    56  // DeleteServingURL deletes the serving URL for an image.
    57  func DeleteServingURL(c context.Context, key appengine.BlobKey) error {
    58  	req := &pb.ImagesDeleteUrlBaseRequest{
    59  		BlobKey: (*string)(&key),
    60  	}
    61  	res := &pb.ImagesDeleteUrlBaseResponse{}
    62  	return internal.Call(c, "images", "DeleteUrlBase", req, res)
    63  }
    64  
    65  func init() {
    66  	internal.RegisterErrorCodeMap("images", pb.ImagesServiceError_ErrorCode_name)
    67  }