Пример #1
0
func RespondWithHeader(url string, c appengine.Context, w http.ResponseWriter, r *http.Request) error {
	w.Header().Set("Content-Type", "image/jpeg")
	blobKey, err := blobstore.BlobKeyForFile(c, blobFileName(url))
	if err == nil {
		w.Header().Set("X-AppEngine-BlobKey", string(blobKey))
		fmt.Fprintln(w, "")
	}
	return err
}
Пример #2
0
func (d *gcsDriver) Serve(w http.ResponseWriter, id string, rng driver.Range) (bool, error) {
	if rng.IsValid() {
		w.Header().Set("X-AppEngine-BlobRange", rng.String())
	}
	key, err := blobstore.BlobKeyForFile(d.c, d.path(id))
	if err != nil {
		return false, err
	}
	blobstore.Send(w, key)
	return true, nil
}
Пример #3
0
func gcsDirectPublicUrl(ctx appengine.Context, bucket string, filename string) string {
	if appengine.IsDevAppServer() {
		// When running on devappserver, GCS files are stored locally in the blobstore
		gcsPath := gcsFilesApiPath(bucket, filename)
		blobKey, err := blobstore.BlobKeyForFile(ctx, gcsPath)
		if err != nil {
			panic(err)
		}
		return "http://localhost:8000/blobstore/blob/" + string(blobKey) + "?display=inline"
	}

	return "https://storage.googleapis.com/" + bucket + "/" + filename
}
Пример #4
0
// GCSに保存された画像の公開URLを取得します。
func GetServingURL(c appengine.Context, filename string) (servingURL *url.URL, err error) {
	blobKey, err := blobstore.BlobKeyForFile(c, filename)
	if err != nil {
		c.Errorf("serve.go:14: %s", err.Error())
		return nil, err
	}

	opts := &image.ServingURLOptions{Secure: true}
	servingURL, err = image.ServingURL(c, blobKey, opts)
	if err != nil {
		c.Errorf("serve.go:25: %s", err.Error())
		return nil, err
	}

	return servingURL, nil
}
// Get the image link for an object.
func api_imageLinkObject(w http.ResponseWriter, r *http.Request) {
	// We use the blob store + images to generate a serving url for the file
	c := appengine.NewContext(r)
	vars := mux.Vars(r)

	bk, err := blobstore.BlobKeyForFile(c, "/gs/"+bucketName+"/"+vars["key"])

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if imageUrl, err := image.ServingURL(c, bk, nil); err == nil {
		fmt.Fprint(w, imageUrl)
	} else {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Пример #6
0
func jobOutputHandler(w http.ResponseWriter, r *http.Request) {
	path := r.URL.Path
	jobID := path[12:]

	c := appengine.NewContext(r)

	state := &job.State{ID: jobID}
	if err := datastore.Get(c, state.GetKey(c), state); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	if state.OutputData == "" {
		http.Error(w, "No processing output", http.StatusBadRequest)
	}

	blobKey, err := blobstore.BlobKeyForFile(c, state.OutputData)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	w.Header().Set("Cache-Control", "public,max-age:60000")
	blobstore.Send(w, blobKey)
}
Пример #7
0
func renameOrDelete(c appengine.Context, w http.ResponseWriter, r *http.Request) {

	b1 := new(bytes.Buffer)
	s1 := ""

	defer func() {
		w.Header().Set("Content-type", "text/html; charset=utf-8")
		w.Write(b1.Bytes())
	}()

	// c := appengine.NewContext(r)

	bk := r.FormValue("blobkey")
	if bk == "" {
		b1.WriteString("No blob key given<br>")
		return
	} else {
		s1 = fmt.Sprintf("Blob key given %q<br>", bk)
		b1.WriteString(s1)
	}

	dsKey := datastore.NewKey(c, "__BlobInfo__", bk, 0, nil)

	q := datastore.NewQuery("__BlobInfo__").Filter("__key__=", dsKey)

	var bi BlobInfo
	var found bool

	for t := q.Run(c); ; {
		_, err := t.Next(&bi)
		if err == datastore.Done {
			c.Infof("   No Results (any more), blob-rename-delete %v", err)
			break
		}
		// other err
		if err != nil {
			util_err.Err_log(err)
			return
		}
		found = true
		break
	}

	if found {
		ac := r.FormValue("action")
		if ac == "delete" {
			b1.WriteString("deletion  ")

			// first the binary data
			keyBlob, err := blobstore.BlobKeyForFile(c, bi.Filename)
			util_err.Err_log(err)

			if err != nil {
				b1.WriteString(fmt.Sprintf(" ... failed (1) %v", err))
			} else {
				err = blobstore.Delete(c, keyBlob)
				util_err.Err_log(err)
				if err != nil {
					b1.WriteString(fmt.Sprintf(" ... failed (2) %v<br>", err))
				} else {
					// now the datastore record
					err = datastore.Delete(c, dsKey)
					util_err.Err_log(err)
					if err != nil {
						b1.WriteString(fmt.Sprintf(" ... failed (3) %v<br>%#v<br>", err, dsKey))
					} else {
						b1.WriteString(" ... succeeded<br>")
					}

				}
			}
		}

		if ac == "rename" {
			b1.WriteString("renaming ")

			nfn := r.FormValue("filename")
			if nfn == "" || len(nfn) < 4 {
				b1.WriteString(" ... failed - at LEAST 4 chars required<br>")
				return
			}
			nfn = strings.ToLower(nfn)
			bi.Filename = nfn
			_, err := datastore.Put(c, dsKey, &bi)
			util_err.Err_log(err)
			if err != nil {
				b1.WriteString(fmt.Sprintf(" ... failed. %v", err))
			} else {
				b1.WriteString(" ... succeeded<br>")
			}
		}

	} else {
		b1.WriteString("no blob found for given blobkey<br>")
	}
	b1.WriteString("<a href='/blob2'>Back to list</a><br>")

}
Пример #8
0
func handleUploadImage(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	file, fileHeader, err := r.FormFile(uploadImageKey)
	if err != nil {
		c.Infof("Failed to get uploaded image: %v", err)
		http.Error(w, "Failed to get uploaded image", http.StatusBadRequest)
		return
	}
	defer file.Close()
	data, err := ioutil.ReadAll(file)
	if err != nil {
		c.Errorf("Failed to read uploaded image: %v", err)
		http.Error(w, "Failed to get uploaded image", http.StatusBadRequest)
		return
	}
	ext := path.Ext(fileHeader.Filename)
	mimeType := mime.TypeByExtension(ext)

	// create context for Google Cloud Storage and upload file
	gc := gappengine.NewContext(r)
	hc := &http.Client{
		Transport: &oauth2.Transport{
			Source: google.AppEngineTokenSource(gc, storage.ScopeFullControl),
			Base:   &urlfetch.Transport{Context: gc},
		},
	}
	ctx := cloud.NewContext(gappengine.AppID(gc), hc)
	c.Infof("Demo GCS Application running from Version: %v\n", appengine.VersionID(c))

	filePath := path.Join(tempImagePath, createTempImagePath, fileHeader.Filename)
	c.Infof("file: %v, size: %d, MIME: %v, path: %v", fileHeader.Filename, len(data), mimeType, filePath)
	wc := storage.NewWriter(ctx, bucketName, filePath)
	wc.ContentType = mimeType
	_, err = wc.Write(data)
	if err != nil {
		c.Errorf("Failed to upload image: %v", err)
		http.Error(w, "Failed to upload image", http.StatusInternalServerError)
		return
	}
	err = wc.Close()
	if err != nil {
		c.Errorf("Failed to close uploaded image: %v", err)
		http.Error(w, "Failed to upload image", http.StatusInternalServerError)
		return
	}
	obj, err := storage.StatObject(ctx, bucketName, filePath)
	if err != nil {
		c.Errorf("Failed to stat object: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	// c.Infof("obj: %v", obj)
	// get blob key for GCS file
	// obj := wc.Object()
	objName := path.Join("/gs", bucketName, obj.Name)
	c.Infof("Getting blob key from path: %v", objName)
	imgKey, err := blobstore.BlobKeyForFile(c, objName)
	if err != nil {
		c.Errorf("Failed to get image blob key: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	url, err := getImageUrl(c, imgKey)
	if err != nil {
		c.Errorf("Failed to get room image url: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	info := ImageInfo{
		Key: imgKey,
		Url: url.String(),
	}
	outBuf, err := json.Marshal(&info)
	if err != nil {
		c.Errorf("%s", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	_, err = w.Write(outBuf)
	if err != nil {
		c.Errorf("%s", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}