func UserImageHandler(ctx *app.Context) { if ImageHandler != nil { ImageHandler(ctx) return } id := ctx.IndexValue(0) format := ctx.IndexValue(1) if lower := strings.ToLower(format); lower != format { ctx.MustRedirectReverse(true, ImageHandlerName, id, lower) return } ctx.SetHeader("Content-Type", "image/"+format) httpserve.NeverExpires(ctx) bs := ctx.Blobstore() if err := bs.Serve(ctx, id, nil); err != nil { panic(err) } }
func mightFetchImage(ctx *app.Context, url string, prevId string, prevFormat string, prevURL string) (string, string, string) { if url == prevURL { return prevId, prevFormat, prevURL } if url == "" { if prevId != "" { ctx.Blobstore().Remove(prevId) } return "", "", "" } id, format, err := getImage(ctx, url) if err != nil { // Keep previous return prevId, prevFormat, prevURL } if prevId != "" { ctx.Blobstore().Remove(prevId) } return id, format, url }
func catFile(ctx *app.Context) { var id string ctx.MustParseIndexValue(0, &id) f, err := ctx.Blobstore().Open(id) if err != nil { panic(err) } defer f.Close() var meta bool ctx.ParseParamValue("meta", &meta) if meta { var m interface{} if err := f.GetMeta(&m); err != nil { panic(err) } fmt.Println(m) } else { io.Copy(os.Stdout, f) } }
func defaultFetchImage(ctx *app.Context, url string) (string, string, error) { resp, err := httpclient.New(ctx).Get(url) if err != nil { return "", "", err } defer resp.Close() data, err := resp.ReadAll() if err != nil { return "", "", err } _, format, err := image.DecodeConfig(bytes.NewReader(data)) if err != nil { return "", "", err } bs := ctx.Blobstore() id, err := bs.Store(data, nil) if err != nil { return "", "", err } return id, strings.ToLower(format), nil }