appengine/v2/image/image.go

67 строки
2.0 KiB
Go
Исходник Обычный вид История

Merge QA into the main branch (#247) * Removing old, unsupported APIs (#230) * Remove the old Search API. * Remove the old Remote API. * Remove the old sockets API. * Delete legacy appengine code (#231) - Delete all files behind `+build appengine` - Merge the `*_vm.go` variants with the base files - `go fix` for context update (TODO: Fix internal/api.go's use of context) - Update travis config to test new versions * Remove Query APIs from appengine.log (#233) Remove deprecated log.Query api from appengine.log * Update docs (#234) * Remove the old Search API. * Remove the old Remote API. * Remove the old sockets API. * Temporarily remove blobstore API for v2 preview. * Revert "Temporarily remove blobstore API for v2 preview." This reverts commit 169bbdd4674a0fc862fd6a12a843eb2b3bba01cd. * Update docs for QA. * Update module API docs to indicate manual_scaling must be used for NumInstances, etc. * Update module API docs to indicate manual_scaling must be used for NumInstances, etc. * Remove old interfaces and interfaces that are not recommended (#236) * Remove the old deprecated channel interface. * Remove the old deprecated file interface. * Remove the old deprecated xmpp interface. * Removing the cloudsql interface, which is not recommended. * Removing the aebundler interface, which has been deprecated for some time. * Remove obsolete demo code (#238) * Remove obsolete demo code. Official appengine sample code referred by doc is in https://github.com/GoogleCloudPlatform/golang-samples * Replace Logs API with Stdout (#240) * Removed Logs API. * Remove dead code. * Added JSON encoding tests. * Emit single character severity in JSON message to trigger structured logs handling. * Prevent timestamp from being emitted on deployed logs. * Remove superfluous apis (#241) * Removed protos for channel/search/xmpp. * Remove protos for socket. * Add traceID/spanID to structured logs and chunk long logs (#242) * Moved logging related logic to separate file. * Added traceID/spanID to structured logs and log chunking. * Changes from code review. * Update go.mod for v2 * Revert v2 in the base directory. * Create the v2 in /v2 and revert the base directory back to 1.6.7 (#243) * Create a v2 directory for v2+ releases. * Update AppEngine imports to v2. * Update AppEngine imports to v2. * Clean up travis. We only currently support go 1.11+ * Temporarily disable testing go111+ with gopath until we can tag v2 properly. * Tidy v2/go.mod (#244) * Create a v2 directory for v2+ releases. * Update AppEngine imports to v2. * Update AppEngine imports to v2. * Clean up travis. We only currently support go 1.11+ * Temporarily disable testing go111+ with gopath until we can tag v2 properly. * Tidy go.sum * go mod tidy (#245) * Create a v2 directory for v2+ releases. * Update AppEngine imports to v2. * Update AppEngine imports to v2. * Clean up travis. We only currently support go 1.11+ * Temporarily disable testing go111+ with gopath until we can tag v2 properly. * Tidy go.sum * go mod tidy Co-authored-by: Steven Buss <sbuss@google.com> Co-authored-by: Kayla Nguyen <kaylanguyen@google.com> Co-authored-by: Jason Collins <jason.a.collins@gmail.com>
2021-06-15 20:57:49 +03:00
// Copyright 2012 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Package image provides image services.
package image // import "google.golang.org/appengine/v2/image"
import (
"context"
"fmt"
"net/url"
"google.golang.org/appengine/v2"
"google.golang.org/appengine/v2/internal"
pb "google.golang.org/appengine/v2/internal/image"
)
type ServingURLOptions struct {
Secure bool // whether the URL should use HTTPS
// Size must be between zero and 1600.
// If Size is non-zero, a resized version of the image is served,
// and Size is the served image's longest dimension. The aspect ratio is preserved.
// If Crop is true the image is cropped from the center instead of being resized.
Size int
Crop bool
}
// ServingURL returns a URL that will serve an image from Blobstore.
func ServingURL(c context.Context, key appengine.BlobKey, opts *ServingURLOptions) (*url.URL, error) {
req := &pb.ImagesGetUrlBaseRequest{
BlobKey: (*string)(&key),
}
if opts != nil && opts.Secure {
req.CreateSecureUrl = &opts.Secure
}
res := &pb.ImagesGetUrlBaseResponse{}
if err := internal.Call(c, "images", "GetUrlBase", req, res); err != nil {
return nil, err
}
// The URL may have suffixes added to dynamically resize or crop:
// - adding "=s32" will serve the image resized to 32 pixels, preserving the aspect ratio.
// - adding "=s32-c" is the same as "=s32" except it will be cropped.
u := *res.Url
if opts != nil && opts.Size > 0 {
u += fmt.Sprintf("=s%d", opts.Size)
if opts.Crop {
u += "-c"
}
}
return url.Parse(u)
}
// DeleteServingURL deletes the serving URL for an image.
func DeleteServingURL(c context.Context, key appengine.BlobKey) error {
req := &pb.ImagesDeleteUrlBaseRequest{
BlobKey: (*string)(&key),
}
res := &pb.ImagesDeleteUrlBaseResponse{}
return internal.Call(c, "images", "DeleteUrlBase", req, res)
}
func init() {
internal.RegisterErrorCodeMap("images", pb.ImagesServiceError_ErrorCode_name)
}