Ejemplo n.º 1
0
// GetServingURL gets URL to serve GCS file to users.
func GetServingURL(c context.Context, filename string) (servingURL *url.URL, err error) {

	blobKey, err := blobstore.BlobKeyForFile(c, filename)
	if err != nil {
		log.Errorf(c, "error for file %s:  %s", filename, err.Error())
		return nil, err
	}

	opts := &image.ServingURLOptions{Secure: true}
	servingURL, err = image.ServingURL(c, blobKey, opts)
	if err != nil {
		log.Errorf(c, "failed to get serving URL: %s", err.Error())
		return nil, err
	}

	return servingURL, nil
}
Ejemplo n.º 2
0
func ToBlobKey(c *gin.Context, key string) appengine.BlobKey {
	bk, _ := blobstore.BlobKeyForFile(c, key)
	return bk
}
Ejemplo n.º 3
0
func renameOrDelete(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	lg, b := loghttp.BuffLoggerUniversal(w, r)
	_ = b

	c := appengine.NewContext(r)

	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 {
			aelog.Infof(c, "   No Results (any more), blob-rename-delete %v", err)
			break
		}
		// other err
		if err != nil {
			lg(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)
			lg(err)

			if err != nil {
				b1.WriteString(fmt.Sprintf(" ... failed (1) %v", err))
			} else {
				err = blobstore.Delete(c, keyBlob)
				lg(err)
				if err != nil {
					b1.WriteString(fmt.Sprintf(" ... failed (2) %v<br>", err))
				} else {
					// now the datastore record
					err = datastore.Delete(c, dsKey)
					lg(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)
			lg(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>")

}