func handleBlobFile(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) key := datastore.NewKey(c, "File", r.URL.Path, 0, nil) var file File err := datastore.Get(c, key, &file) if err != nil { http.NotFound(w, r) return } w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(file.Name))) _, err = io.Copy(w, blobstore.NewReader(c, appengine.BlobKey(r.URL.Path))) if err != nil { log.Errorf(c, "stream blob: %s", err.Error()) } }
func handleImages(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) switch r.Method { case "POST": blobs, vals, err := blobstore.ParseUpload(r) if err != nil { http.Error(w, "bad request", http.StatusBadRequest) log.Errorf(c, "bad request: %s", err.Error()) return } bucketID := vals.Get("BucketID") if bucketID == "" { http.Error(w, "BucketID query parameter is required", http.StatusBadRequest) return } url, err := blobstore.UploadURL(c, "/admin/images", nil) if err == nil { w.Header().Set("UploadURL", url.String()) } imgs := make([]Image, 0, 20) for _, infos := range blobs { for _, info := range infos { var img Image img.Name = info.Filename imgUrl, err := aimage.ServingURL(c, info.BlobKey, nil) if err != nil { log.Errorf(c, "failed to get serving url for blob '%s': %s", info.BlobKey, err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } img.URL = imgUrl.String() img.ID = string(info.BlobKey) ir := blobstore.NewReader(c, info.BlobKey) cfg, _, err := image.DecodeConfig(ir) if err != nil { log.Warningf(c, "decode image '%s': %s", info.BlobKey, err.Error()) err = blobstore.Delete(c, info.BlobKey) if err != nil { log.Errorf(c, "delete blob '%s': %s", info.BlobKey, err.Error()) } continue } img.Height = cfg.Height img.Width = cfg.Width imgs = append(imgs, img) } } // add images to bucket and dtore err = datastore.RunInTransaction(c, func(c context.Context) error { bucketKey := datastore.NewKey(c, "Bucket", bucketID, 0, nil) var b Bucket err := datastore.Get(c, bucketKey, &b) if err != nil { return err } if b.Images == nil { b.Images = make([]string, 0, len(imgs)) } for _, img := range imgs { _, err = datastore.Put(c, datastore.NewKey(c, "Image", img.ID, 0, nil), &img) if err != nil { return err } b.Images = append(b.Images, img.ID) } _, err = datastore.Put(c, bucketKey, &b) return err }, &datastore.TransactionOptions{XG: true}) if err == datastore.ErrNoSuchEntity { for _, img := range imgs { err = blobstore.Delete(c, appengine.BlobKey(img.ID)) if err != nil { log.Warningf(c, "delete blob '%s': %s", img.ID, err.Error()) } } http.NotFound(w, r) return } if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Errorf(c, "update datastore: %s", err.Error()) return } w.Header().Set("Content-Type", "application/json") err = json.NewEncoder(w).Encode(&imgs) if err != nil { log.Errorf(c, "json encode: %s", err.Error()) } case "GET": var imgs []Image _, err := datastore.NewQuery("Image").GetAll(c, &imgs) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Errorf(c, "query images: %s", err.Error()) return } w.Header().Set("Content-Type", "application/json") if imgs == nil { imgs = []Image{} } err = json.NewEncoder(w).Encode(&imgs) if err != nil { log.Errorf(c, "json encode: %s", err.Error()) } default: http.Error(w, "Valid methods are GET and POST", http.StatusMethodNotAllowed) return } }