image.go 2.0 KB

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