Esempio n. 1
0
func SaveEntry(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	contnt, ok := m["content"].(string)
	util_err.Err_http(w, r, ok, false, "need a key 'content' with a string")

	c := appengine.NewContext(r)

	g := GbsaveEntry{
		Content:           contnt,
		Date:              time.Now(),
		TypeShiftingField: 2,
	}
	if u := user.Current(c); u != nil {
		g.Author = u.String()
	}
	g.Comment1 = "comment"

	/* We set the same parent key on every GB entry entity
	   to ensure each GB entry is in the same entity group.

	   Queries across the single entity group will be consistent.
	   However, we should limit write rate to single entity group ~1/sec.

		NewIncompleteKey(appengine.Context, kind string , parent *Key)
			has neither a string key, nor an integer key
		 	only a "kind" (classname) and a parent
		Upon usage the datastore generates an integer key
	*/

	incomplete := ds.NewIncompleteKey(c, DSKindGBEntry, key_entity_group_key_parent(c))
	concreteNewKey, err := ds.Put(c, incomplete, &g)
	util_err.Err_http(w, r, err, false)
	_ = concreteNewKey // we query entries via key_entity_group_key_parent - via parent

}
Esempio n. 2
0
// m contains defaults or overwritten template contents
func templatesExtend(w http.ResponseWriter, r *http.Request, subtemplates map[string]string) *tt.Template {

	var err error = nil
	tder := cloneFromBase(w, r)

	//for k,v := range furtherDefinitions{
	//	tder, err = tder.Parse( `{{define "` + k  +`"}}`   + v + `{{end}}` )
	//	util_err.Err_http(w,r,err,false)
	//}

	for k, v := range subtemplates {

		if len(v) > lenLff && v[0:lenLff] == PrefixLff {
			fileName := v[lenLff:]
			fcontent, err := ioutil.ReadFile("templates/" + fileName + ".html")
			util_err.Err_http(w, r, err, false, "could not open static template file")
			v = string(fcontent)
		}

		tder, err = tder.Parse(`{{define "` + k + `"}}` + v + `{{end}}`)
		util_err.Err_http(w, r, err, false)

	}

	return tder
}
// output static file as base64 string
//   image must exist as file in /static
func imagefileAsBase64(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)

	p := r.FormValue("p")
	if p == "" {
		p = "static/chartbg_400x960__480x1040__12x10.png"
	}

	f, err := os.Open(p)
	util_err.Err_http(w, r, err, false)
	defer f.Close()

	img, whichFormat, err := image.Decode(f)
	util_err.Err_http(w, r, err, false)
	c.Infof("format %v - subtype %T\n", whichFormat, img)

	imgRGBA, ok := img.(*image.RGBA)
	util_err.Err_http(w, r, ok, true, "source image was not convertible to image.RGBA - gifs or jpeg?")

	// => header with mime prefix always prepended
	//   and its always image/PNG
	str_b64 := conv.Rgba_img_to_base64_str(imgRGBA)

	w.Header().Set("Content-Type", "text/html")
	io.WriteString(w, "<p>Image embedded in HTML as Base64:</p><img width=200px src=\"")
	io.WriteString(w, str_b64)
	io.WriteString(w, "\"> ")

}
Esempio n. 4
0
func templatesCompileDemo(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	w.Header().Set("Content-Type", "text/html")

	funcMap := tt.FuncMap{
		"unescape": html.UnescapeString,
		"escape":   html.EscapeString,
	}

	var t_base *tt.Template
	var err error = nil

	// creating T0 - naming it - adding func map
	t_base = tt.Must(tt.New("str_T0_outmost").Funcs(funcMap).Parse(T0))
	util_err.Err_http(w, r, err, false)

	// adding the definition of T1 - introducing reference to T2 - undefined yet
	t_base, err = t_base.Parse(T1) // definitions must appear at top level - but not at the start
	util_err.Err_http(w, r, err, false)

	// create two clones
	// now both containing T0 and T1
	tc_1, err := t_base.Clone()
	util_err.Err_http(w, r, err, false)
	tc_2, err := t_base.Clone()
	util_err.Err_http(w, r, err, false)

	// adding different T2 definitions
	s_if := "{{if .}}{{.}}{{else}}no dyn data{{end}}"
	tc_1, err = tc_1.Parse("{{define `T2`}}T2-A  <br>--" + s_if + "--  {{end}}")
	util_err.Err_http(w, r, err, false)
	tc_2, err = tc_2.Parse("{{define `T2`}}T2-B  <br>--" + s_if + "--  {{end}}")
	util_err.Err_http(w, r, err, false)

	// writing both clones to the response writer
	err = tc_1.ExecuteTemplate(w, "str_T0_outmost", nil)
	util_err.Err_http(w, r, err, false)

	// second clone is written with dynamic data on two levels
	dyndata := map[string]string{"key1": "dyn val 1", "key2": "dyn val 2"}
	err = tc_2.ExecuteTemplate(w, "str_T0_outmost", dyndata)
	util_err.Err_http(w, r, err, false)

	// Note: it is important to pass the DOT
	//		 {{template "T1" .}}
	//		 {{template "T2" .key2 }}
	//						 ^
	// otherwise "dyndata" can not be accessed by the inner templates...

	// leaving T2 undefined => error
	tc_3, err := t_base.Clone()
	util_err.Err_http(w, r, err, false)
	err = tc_3.ExecuteTemplate(w, "str_T0_outmost", dyndata)
	util_err.Err_http(w, r, err, false)

}
Esempio n. 5
0
func submitUpload(c appengine.Context, w http.ResponseWriter, r *http.Request) {

	// c := appengine.NewContext(r)
	uploadURL, err := blobstore.UploadURL(c, "/blob2/processing-new-upload", nil)
	util_err.Err_http(w, r, err, false)

	w.Header().Set("Content-type", "text/html; charset=utf-8")
	err = upload2.Execute(w, uploadURL)
	util_err.Err_http(w, r, err, false)
}
Esempio n. 6
0
// a clone factory
//  as t_base is a "app scope" variable
//  it will live as long as the application runs
func cloneFromBase(w http.ResponseWriter, r *http.Request) *tt.Template {

	funcMap := tt.FuncMap{
		"unescape":         html.UnescapeString,
		"escape":           html.EscapeString,
		"fMult":            fMult,
		"fAdd":             fAdd,
		"fChop":            fChop,
		"fAccessElement":   fAccessElement,
		"fAccessElementB2": fAccessElementB2,
		"fMakeRange":       fMakeRange,
		"fNumCols":         fNumCols,
		"df": func(g time.Time) string {
			return g.Format("2006-01-02 (Jan 02)")
		},
	}

	if t_base == nil {
		t_base = tt.Must(tt.New("n_page_scaffold_01").Funcs(funcMap).Parse(c_page_scaffold_01))
	}

	t_derived, err := t_base.Clone()
	util_err.Err_http(w, r, err, false)

	return t_derived
}
Esempio n. 7
0
func emailSend(w http.ResponseWriter, r *http.Request, m map[string]string) {

	c := appengine.NewContext(r)
	//addr := r.FormValue("email")

	if _, ok := m["subject"]; !ok {
		m["subject"] = "empty subject line"
	}

	email_thread_id := []string{"3223"}

	msg := &ae_mail.Message{
		//Sender:  "Peter Buchmann <*****@*****.**",
		//		Sender: "*****@*****.**",
		Sender: "*****@*****.**",
		//To:	   []string{addr},
		To: []string{"*****@*****.**"},

		Subject: m["subject"],
		Body:    "some_body some_body2",
		Headers: go_mail.Header{"References": email_thread_id},
	}
	err := ae_mail.Send(c, msg)
	util_err.Err_http(w, r, err, false, "could not send the email")

}
Esempio n. 8
0
func guestEntry(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)
	path := m["dir"].(string) + m["base"].(string)

	err := sc.Increment(c, path)
	util_err.Err_http(w, r, err, false)

	cntr, err := sc.Count(w, r, path)
	util_err.Err_http(w, r, err, false)

	tplAdder, tplExec := tpl_html.FuncTplBuilder(w, r)
	tplAdder("n_html_title", "New guest book entry", nil)
	tplAdder("n_cont_0", c_new_gbe, cntr)
	tplExec(w, r)

}
Esempio n. 9
0
func imgServingExample3(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)

	p := r.FormValue("p")
	if p == "" {
		p = "static/chartbg_400x960__480x1040__12x10.png"
	}
	if p == "" {
		p = "static/pberg1.png"
	}
	// try p=static/unmodifiable_format.jpg

	// prepare a cutout rect
	var p1, p2 image.Point
	p1.X, p1.Y = 10, 60
	p2.X, p2.Y = 400, 255
	var rec image.Rectangle = image.Rectangle{Min: p1, Max: p2}

	f, err := os.Open(p)
	util_err.Err_http(w, r, err, false)
	defer f.Close()

	img, whichFormat, err := image.Decode(f)
	util_err.Err_http(w, r, err, false, "only jpeg and png are 'activated' ")
	c.Infof("serving format %v %T\n", whichFormat, img)

	switch t := img.(type) {

	default:
		util_err.Err_http(w, r, false, false, "internal color formats image.YCbCr and image.RGBA are understood")

	case *image.RGBA, *image.YCbCr:
		imgXFull, ok := t.(*image.RGBA)
		util_err.Err_http(w, r, ok, false, "image.YCbCr can not be typed to image.RGBA - this will panic")

		imgXCutout, ok := imgXFull.SubImage(rec).(*image.RGBA)
		util_err.Err_http(w, r, ok, false, "cutout operation failed")

		// we serve it as JPEG
		w.Header().Set("Content-Type", "image/jpeg")
		jpeg.Encode(w, imgXCutout, &jpeg.Options{Quality: jpeg.DefaultQuality})

	}

}
func GetImageFromDatastore(w http.ResponseWriter, r *http.Request, key string) *image.RGBA {

	c := appengine.NewContext(r)

	dsObj, err := dsu.BufGet(w, r, "dsu.WrapBlob__"+key)
	util_err.Err_http(w, r, err, false)

	s := string(dsObj.VByte)

	img, whichFormat := conv.Base64_str_to_img(s)
	c.Infof("retrieved img from base64: format %v - subtype %T\n", whichFormat, img)

	i, ok := img.(*image.RGBA)
	util_err.Err_http(w, r, ok, false, "saved image needs to be reconstructible into a format png of subtype *image.RGBA")

	return i
}
Esempio n. 11
0
func call(w http.ResponseWriter, r *http.Request) {

	// params trial: https://www.twilio.com/user/account/developer-tools/api-explorer/call-create
	// params docu:  https://www.twilio.com/docs/api/rest/making-calls
	v := url.Values{}
	v.Set("To", phoneTo)
	v.Set("From", phoneFrom)
	v.Set("Url", callbackUrl)
	v.Set("Method", "GET")
	v.Set("FallbackMethod", "GET")
	v.Set("StatusCallbackMethod", "GET")
	// v.Set("SendDigits", "32168")
	v.Set("Timeout", "4")
	v.Set("Record", "false")
	rb := *strings.NewReader(v.Encode())

	// Create Client
	// client := &http.Client{}  // local, not on appengine

	c := appengine.NewContext(r)
	// following appengine method, derived from big-query is non working:
	// config := oauth2_google.NewAppEngineConfig(c, []string{twilioURLPrefix})
	// client := &http.Client{Transport: config.NewTransport()}
	clientClassic := urlfetch.Client(c)
	//clientTwilio := twilio.NewClient(accountSid, authToken, clientClassic)  // not needed

	req, _ := http.NewRequest("POST", twilioFullURL, &rb)
	req.SetBasicAuth(accountSid, authToken)
	req.Header.Add("Accept", "application/json")
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	resp, err := clientClassic.Do(req)
	util_err.Err_http(w, r, err, false, "something wrong with the http client")

	if resp.StatusCode >= 200 && resp.StatusCode < 300 {
		var data map[string]interface{}
		bodyBytes, _ := ioutil.ReadAll(resp.Body)
		err := json.Unmarshal(bodyBytes, &data)
		if err == nil {
			fmt.Println(data["sid"])
		}
		fmt.Fprintf(w, "%#v", resp)
	} else {
		util_err.Err_http(w, r, err, false, "twilio response not ok", resp.Status)
	}
}
Esempio n. 12
0
func imgServingExample1(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	p := r.FormValue("p")
	if p == "" {
		p = "static/chartbg_400x960__480x1040__12x10.png"
	}
	if p == "" {
		p = "static/pberg1.png"
	}

	f, err := os.Open(p)
	util_err.Err_http(w, r, err, false)
	defer f.Close()

	mode := r.FormValue("mode")
	if mode == "" {
		mode = "direct"
	}

	if mode == "direct" {

		// file reader directly to http writer

		w.Header().Set("Content-Type", "image/png")
		c.Infof("serving directly %v \n", p)
		io.Copy(w, f)

	} else {

		// file to memory image - memory image to http writer

		img, whichFormat, err := image.Decode(f)
		util_err.Err_http(w, r, err, false)
		c.Infof("serving as memory image %v - format %v - type %T\n", p, whichFormat, img)

		// initially a png - we now encode it to jpg
		w.Header().Set("Content-Type", "image/jpeg")
		jpeg.Encode(w, img, &jpeg.Options{Quality: jpeg.DefaultQuality})
	}

}
func SaveImageToDatastore(w http.ResponseWriter, r *http.Request, i *image.RGBA, key string) string {

	s := conv.Rgba_img_to_base64_str(i)
	internalType := fmt.Sprintf("%T", i)
	//buffBytes, _     := StringToVByte(s)  // instead of []byte(s)
	key_combi, err := dsu.BufPut(w, r, dsu.WrapBlob{Name: key, VByte: []byte(s), S: internalType}, key)
	util_err.Err_http(w, r, err, false)

	return key_combi

}
Esempio n. 14
0
func ListEntries(w http.ResponseWriter, r *http.Request) (gbEntries []GbEntryRetr, report string) {

	c := appengine.NewContext(r)
	/* High Replication Datastore:
	Ancestor queries are strongly consistent.
	Queries spanning MULTIPLE entity groups are EVENTUALLY consistent.
	If .Ancestor was omitted from this query, there would be slight chance
	that recent GB entry would not show up in a query.
	*/
	q := ds.NewQuery(DSKindGBEntry).Ancestor(key_entity_group_key_parent(c)).Order("-Date").Limit(10)
	gbEntries = make([]GbEntryRetr, 0, 10)
	keys, err := q.GetAll(c, &gbEntries)

	if fmt.Sprintf("%T", err) == fmt.Sprintf("%T", new(ds.ErrFieldMismatch)) {
		//s := fmt.Sprintf("%v %T  vs %v %T <br>\n",err,err,ds.ErrFieldMismatch{},ds.ErrFieldMismatch{})
		util_err.Err_http(w, r, err, true)
		err = nil // ignore this one - it's caused by our deliberate differences between gbsaveEntry and gbEntrieRetr
	}
	util_err.Err_http(w, r, err, false)

	// for investigative purposes,
	// we
	var b1 bytes.Buffer
	var sw string
	var descrip []string = []string{"class", "path", "key_int_guestbk"}
	for i0, v0 := range keys {
		sKey := fmt.Sprintf("%v", v0)
		v1 := strings.Split(sKey, ",")
		sw = fmt.Sprintf("key %v", i0)
		b1.WriteString(sw)
		for i2, v2 := range v1 {
			d := descrip[i2]
			sw = fmt.Sprintf(" \t %v:  %q ", d, v2)
			b1.WriteString(sw)
		}
		b1.WriteString("\n")
	}
	report = b1.String()

	return
}
Esempio n. 15
0
func guestView(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)
	path := m["dir"].(string) + m["base"].(string)

	err := sc.Increment(c, path)
	util_err.Err_http(w, r, err, false)

	cntr, err := sc.Count(w, r, path)
	util_err.Err_http(w, r, err, false)

	gbEntries, report := gbp.ListEntries(w, r)

	tplAdder, tplExec := tpl_html.FuncTplBuilder(w, r)
	tplAdder("n_html_title", "List of guest book entries", nil)
	tplAdder("n_cont_0", c_view_gbe, gbEntries)
	tplAdder("n_cont_1", "<pre>{{.}}</pre>", report)
	tplAdder("n_cont_2", "Visitors: {{.}}", cntr)
	tplExec(w, r)

}
func saveURLWithAncestor(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	k := ds.NewKey(c, kindUrl, "", 0, ancKey(c))

	s := util.TimeMarker()
	ls := len(s)
	lc := len("09-26 17:29:25")
	lastURL_fictitious_1 := LastURL{"with_anc " + s[ls-lc:ls-3]}
	_, err := ds.Put(c, k, &lastURL_fictitious_1)
	util_err.Err_http(w, r, err, false)

}
Esempio n. 17
0
func serveThumb(c appengine.Context, w http.ResponseWriter, r *http.Request) {

	// c := appengine.NewContext(r)
	k := appengine.BlobKey(r.FormValue("blobkey"))

	var o image.ServingURLOptions = *new(image.ServingURLOptions)
	o.Size = 200
	o.Crop = true
	url, err := image.ServingURL(c, k, &o)

	util_err.Err_http(w, r, err, false)

	http.Redirect(w, r, url.String(), http.StatusFound)
}
// saving some data by kind and key
//   without ancestor key
func saveURLNoAnc(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	k := ds.NewKey(c, kindUrl, keyUrl, 0, nil)
	e := new(LastURL)
	err := ds.Get(c, k, e)
	if err == ds.ErrNoSuchEntity {
		util_err.Err_log(err)
	} else {
		util_err.Err_http(w, r, err, false)
	}

	old := e.Value
	e.Value = r.URL.Path + r.URL.RawQuery

	_, err = ds.Put(c, k, e)
	util_err.Err_http(w, r, err, false)

	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	w.Write([]byte("old=" + old + "\n"))
	w.Write([]byte("new=" + e.Value + "\n"))

}
Esempio n. 19
0
func backend2(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)

	path := m["dir"].(string) + m["base"].(string)

	err := sc.Increment(c, path)
	util_err.Err_http(w, r, err, false)

	cntr, err := sc.Count(w, r, path)
	util_err.Err_http(w, r, err, false)

	add, tplExec := tpl_html.FuncTplBuilder(w, r)
	add("n_html_title", "Backend", nil)
	//add("n_cont_0", c_link, links)
	add("n_cont_0", tpl_html.PrefixLff+"backend_body", blocks2)
	add("tpl_legend", tpl_html.PrefixLff+"backend_body_embed01", "")

	//add("n_cont_1", "<pre>{{.}}</pre>", "pure text")
	add("n_cont_2", "<p>{{.}} views</p>", cntr)

	tplExec(w, r)

}
Esempio n. 20
0
func processUpload(c appengine.Context, w http.ResponseWriter, r *http.Request) {

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

	blobs, otherFormFields, err := MyParseUpload(r)
	util_err.Err_http(w, r, err, false, fmt.Errorf("Fehler beim Parsen: %v", err))

	// s1 := util.IndentedDump(blobs)
	// if len(s1) > 2 {
	// 	b1.WriteString("<br>blob: " + s1)
	// }

	// s2 := util.IndentedDump(otherFormFields)
	// if len(s2) > 2 {
	// 	b1.WriteString("<br>otherF: " + s2+"<br>")
	// }

	numFiles := len(blobs["post_field_file"]) // this always yields (int)1
	numOther := len(otherFormFields["post_field_file"])
	if numFiles == 0 && numOther == 0 {
		//http.Redirect(w, r, "/blob2/upload", http.StatusFound)
		b1.WriteString("<a href='/blob2/upload' >No file uploaded? Try again.</a><br>")
		b1.WriteString("<a href='/blob2' >List</a><br>")
		return
	}

	if numFiles > 0 {

		blob0 := blobs["post_field_file"][0]

		dataStoreClone(w, r, blob0, otherFormFields)

		urlSuccessX := "/blob2/serve-full?blobkey=" + string(blob0.BlobKey)
		urlThumb := "/blob2/thumb?blobkey=" + string(blob0.BlobKey)
		b1.WriteString("<a href='/blob2' >List</a><br>")
		b1.WriteString("<a href='" + urlSuccessX + "' >View Full: " + fmt.Sprintf("%v", blob0) + " - view it</a><br>\n")
		b1.WriteString("<a href='" + urlThumb + "' >View Thumbnail</a><br>\n")

	}

	//http.Redirect(w, r, urlSuccess, http.StatusFound)
}
Esempio n. 21
0
func FuncTplBuilder(w http.ResponseWriter, r *http.Request) (f1 func(string, string, interface{}),
	f2 func(http.ResponseWriter, *http.Request)) {

	// prepare collections
	mtc := map[string]string{}      // map template content
	mtd := map[string]interface{}{} // map template data

	// template key - template content, template data
	f1 = func(tk string, tc string, td interface{}) {
		mtc[tk] = tc
		mtd[tk] = td
	}

	f2 = func(w http.ResponseWriter, r *http.Request) {

		// merge arguments with defaults
		map_result := map[string]string{}
		for k, v := range map_default {
			if _, ok := mtc[k]; ok {
				map_result[k] = mtc[k]
				delete(mtc, k)
			} else {
				map_result[k] = v
			}
			if dbg {
				w.Write([]byte(fmt.Sprintf("  %q  %q \n", map_result[k], v)))
			}
		}

		// additional templates beyond the default
		for k, v := range mtc {
			map_result[k] = v
			if dbg {
				w.Write([]byte(fmt.Sprintf("  %q  %q \n", map_result[k], v)))
			}
		}

		tpl_extended := templatesExtend(w, r, map_result)

		err := tpl_extended.ExecuteTemplate(w, "n_page_scaffold_01", mtd)
		util_err.Err_http(w, r, err, false)

	}

	return f1, f2
}
// get all ancestor urls
//  ordering possible
//  distinction to above: *always* consistent
func listURLWithAncestors(w http.ResponseWriter, r *http.Request) {

	w.Header().Set("Content-Type", "text/plain; charset=utf-8")

	c := appengine.NewContext(r)

	q := ds.NewQuery(kindUrl).
		Ancestor(ancKey(c)).
		Order("-Value")
	var vURLs []LastURL
	keys, err := q.GetAll(c, &vURLs)
	util_err.Err_http(w, r, err, false)

	for i, v := range vURLs {
		io.WriteString(w, fmt.Sprint("q loop ", i, "\n"))
		io.WriteString(w, fmt.Sprintf("\tKey %64s  \n\tVal %64s\n", keys[i], v.Value))
	}
	io.WriteString(w, "\nds.Done\n")

}
Esempio n. 23
0
// This was an attempt, to "catch" the uploaded blob
// and store it by myself into the datastore -
// where I would be able to delete it.
// But this failed - the actual blob data does not even reach the appengine.
// Only the blob-info data.
func dataStoreClone(w http.ResponseWriter, r *http.Request,
	blob0 *BlobInfo, otherFormFields url.Values) {

	return

	wbl := dsu.WrapBlob{}
	wbl.Category = "print"
	wbl.Name = otherFormFields["title"][0] + " - " + otherFormFields["descr"][0]
	wbl.Name += " - " + util.LowerCasedUnderscored(blob0.Filename)
	wbl.Desc = fmt.Sprintf("%v", blob0.BlobKey)
	wbl.S = blob0.ContentType
	if len(otherFormFields["post_field_file"]) > 0 {
		filecontent := otherFormFields["post_field_file"][0]
		wbl.VByte = []byte(filecontent)
	}
	keyX2 := "bl" + time.Now().Format("060102_1504-05")
	_, errDS := dsu.BufPut(w, r, wbl, keyX2)
	util_err.Err_http(w, r, errDS, false)

}
Esempio n. 24
0
func formRedirector(c appengine.Context, w http.ResponseWriter, r *http.Request) {

	var msg, cntnt, rURL string

	w.Header().Set("Content-type", "text/html; charset=utf-8")
	// w.Header().Set("Content-type", "text/html; charset=latin-1")

	rURL = r.FormValue("redirect-to")
	//util_err.LogAndShow(w, r, "url: %q <br>\n", rURL)

	u, err := url.Parse(rURL)
	util_err.Err_http(w, r, err, false)

	host, port, err = net.SplitHostPort(u.Host)
	util_err.Err_http(w, r, err, true)
	if err != nil {
		host = u.Host
	}
	//util_err.LogAndShow(w, r, "host and port: %q : %q of %q<br>\n", host, port, rURL)
	//util_err.LogAndShow(w, r, " &nbsp;  &nbsp;  &nbsp; standalone %q <br>\n", u.Host)

	client := urlfetch.Client(c)

	if len(r.PostForm) > 0 {
		// util_err.LogAndShow(w, r, "post unimplemented:<br> %#v <br>\n", r.PostForm)
		// return
		msg += fmt.Sprintf("post converted to get<br>")
	}

	rURL = fmt.Sprintf("%v?1=2&", rURL)
	for key, vals := range r.Form {
		if key == "redirect-to" {
			continue
		}
		val := vals[0]
		if !util_appengine.IsLocalEnviron() {
			val = strings.Replace(val, " ", "%20", -1)
		}
		rURL = fmt.Sprintf("%v&%v=%v", rURL, key, val)
	}

	resp, err := client.Get(rURL)
	util_err.Err_http(w, r, err, false)

	if resp.StatusCode != http.StatusOK {
		fmt.Fprintf(w, "HTTP GET returned status %v<br>\n\n%v<br>\n\n", resp.Status, rURL)
		return
	}

	defer resp.Body.Close()
	byteContent, err := ioutil.ReadAll(resp.Body)
	util_err.Err_http(w, r, err, false)
	if err != nil {
		return
	} else {
		msg += fmt.Sprintf("%v bytes read<br>", len(byteContent))
		cntnt = string(byteContent)
	}

	cntnt = insertNewlines.Replace(cntnt)
	cntnt = undouble.Replace(cntnt)

	cntnt = ModifyHTML(r, cntnt)

	fmt.Fprintf(w, "%s \n\n", cntnt)
	fmt.Fprintf(w, "%s \n\n", msg)

}
Esempio n. 25
0
func parseFurther(w http.ResponseWriter, r *http.Request, saveImages bool) {

	c := appengine.NewContext(r)

	b := new(bytes.Buffer)
	defer func() {
		w.Header().Set("Content-type", "text/plain; charset=utf-8")
		w.Write(b.Bytes())
	}()

	// Get the item from the memcache
	wb1 := new(dsu.WrapBlob)
	ok := dsu.McacheGet(c, keyLatest, wb1)
	util_err.Err_http(w, r, ok, true)

	if ok {
		b.WriteString(sp("name %v\n", wb1.Name))
		b.WriteString(sp("S (boundary): %q\n", wb1.S))

		// dumps the entire body
		// b.WriteString(sp("B: %v\n", string(wb1.VByte)))

		// instead we split it by multipart mime
		vb := bytes.Split(wb1.VByte, []byte("--"+wb1.S))
		for i, v := range vb {
			h := ""  // header
			fn := "" // filename
			s := string(v)
			s = strings.Trim(s, "\r \n")
			ctype := ""

			b.WriteString(sp("\n___________mime boundary index %v___________\n", i))
			if strings.HasPrefix(s, "Content-Type: image/png;") ||
				strings.HasPrefix(s, "Content-Type: image/jpeg;") {

				if start := strings.Index(s, sepHeaderContent); start > 0 {
					h = s[:start]
					vh := strings.Split(h, "\r\n")
					for _, v := range vh {
						v := strings.TrimSpace(v)
						// b.WriteString("\t\t" + v + "\n")
						if strings.HasPrefix(v, "name=") {
							vv := strings.Split(v, "=")
							fn = util.LowerCasedUnderscored(vv[1])
						}
					}
					s = s[start+len(sepHeaderContent):]
					if posSemicol := strings.Index(h, ";"); posSemicol > 0 {
						ctype = h[0:posSemicol]
					}
				}
			}

			if ctype == "" {
				b.WriteString("unparseable: " + util.Ellipsoider(s, 400))
			} else {
				b.WriteString(sp("\n\tctype=%v\n\t------------", ctype))
				if fn != "" {
					b.WriteString(sp("\n\tfilename=%v\n\t------------", fn))
				}
				if saveImages {
					rE := resEntry{}
					rE.when = util.TimeMarker()
					rE.contentType = ctype
					rE.fn = fn
					rE.b64Img = &s
					Images[reservoirRevolver%reservoirSize] = rE
					reservoirRevolver++
					c.Infof("Put image into reservoir %v %v", fn, ctype)
				}
			}

		}

	}

}
Esempio n. 26
0
// working differently as in Python
//		//blob2s := blobstore.BlobInfo.gql("ORDER BY creation DESC")
func blobList(c appengine.Context, w http.ResponseWriter, r *http.Request) {

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

	errFormParse := r.ParseForm()
	if errFormParse != nil {
		b1.WriteString(fmt.Sprintf("Form parse Error %v", errFormParse))
		return
	}

	s1 := ""
	b1.WriteString(openHTML)
	b1.WriteString(restrictForm)

	nameFilter := r.PostFormValue("nameFilter")
	nameFilter = strings.TrimSpace(nameFilter)

	if nameFilter == "" {
		return
	} else {
		tn := time.Now()
		f := "2006-01-02 15:04:05"
		f = "January 02"
		prefix := tn.Format(f)
		// nameFilter = fmt.Sprintf("%v %s", prefix,nameFilter )
		if !strings.HasPrefix(nameFilter, prefix) || len(nameFilter) == len(prefix) {
			b1.WriteString(fmt.Sprintf("cannot filter by %v", nameFilter))
			return
		} else {
			nameFilter = nameFilter[len(prefix):]
		}
	}

	u := user.Current(c)
	if u != nil {
		b1.WriteString(fmt.Sprintf("%v %v %v<br>\n", u.ID, u.Email, u.FederatedIdentity))
	} else {
		b1.WriteString("nobody calling on the phone<br>")
	}

	// c := appengine.NewContext(r)
	q := datastore.NewQuery("__BlobInfo__")
	if nameFilter != "" {

		// nameFilter = strings.ToLower(nameFilter)
		b1.WriteString(fmt.Sprintf("Filtering by %v-%v<br>", nameFilter, util.IncrementString(nameFilter)))

		q = datastore.NewQuery("__BlobInfo__").Filter("filename >=", nameFilter)
		q = q.Filter("filename <", util.IncrementString(nameFilter))

	}
	for t := q.Run(c); ; {
		var bi BlobInfo
		dsKey, err := t.Next(&bi)

		if err == datastore.Done {
			// c.Infof("   No Results (any more) blob-list %v", err)
			break
		}
		// other err
		if err != nil {
			util_err.Err_http(w, r, err, false)
			return
		}

		//s1 = fmt.Sprintf("key %v %v %v %v %v %v<br>\n", dsKey.AppID(),dsKey.Namespace() , dsKey.Parent(), dsKey.Kind(), dsKey.StringID(), dsKey.IntID())
		//b1.WriteString( s1 )

		//s1 = fmt.Sprintf("blobinfo: %v %v<br>\n", bi.Filename, bi.Size)
		//b1.WriteString( s1 )

		ext := path.Ext(bi.Filename)
		base := path.Base(bi.Filename)
		base = base[:len(base)-len(ext)]

		//b1.WriteString( fmt.Sprintf("-%v-  -%v-",base, ext) )

		base = strings.Replace(base, "_", " ", -1)
		base = strings.Title(base)
		ext = strings.ToLower(ext)

		titledFilename := base + ext
		if strings.HasPrefix(titledFilename, "Backup") {
			showBackup := r.FormValue("backup")
			if len(showBackup) < 1 {
				continue
			}
		}

		s1 = fmt.Sprintf("<a class='ib' style='width:280px;margin-right:20px' target='_view' href='/blob2/serve-full?blobkey=%v'>%v</a> &nbsp; &nbsp; \n", dsKey.StringID(), titledFilename)
		b1.WriteString(s1)

		if bi.ContentType == "image/png" || bi.ContentType == "image/jpeg" {
			s1 = fmt.Sprintf("<img class='ib' style='width:40px;' src='/_ah/img/%v%v' />\n",
				dsKey.StringID(), "=s200-c")
			b1.WriteString(s1)

			s1 = fmt.Sprintf("<a class='ib' target='_view' href='/_ah/img/%v%v'>Thumb</a>\n",
				dsKey.StringID(), "=s200-c")
			b1.WriteString(s1)

		} else {
			s1 = fmt.Sprintf("<span class='ib' style='width:145px;'> &nbsp; no thb</span>")
			b1.WriteString(s1)
		}

		s1 = fmt.Sprintf("<a class='ib' target='_rename_delete' href='/blob2/rename-delete?action=delete&blobkey=%v'>Delete</a>\n",
			dsKey.StringID())
		b1.WriteString(s1)

		s1 = fmt.Sprintf(`
			<span class='ib' style='width:450px; border: 1px solid #aaa'>
				<form target='_rename_delete' action='/blob2/rename-delete' >
					<input name='blobkey'  value='%v'     type='hidden'/>
					<input name='action'   value='rename' type='hidden'/>
					<input name='filename' value='%v' size='42'/>
					<input type='submit'   value='Rename' />
				</form>
			</span>
			`, dsKey.StringID(), bi.Filename)
		b1.WriteString(s1)

		b1.WriteString("<br><br>\n\n")

	}

	b1.WriteString("<a accesskey='u' href='/blob2/upload' ><b>U</b>pload</a><br>")
	b1.WriteString(`<a  href='https://appengine.google.com/blobstore/explorer?&app_id=s~libertarian-islands' 
		>Delete via Console</a><br>`)

}
Esempio n. 27
0
func regroupFromDatastore02(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	b1 := new(bytes.Buffer)
	defer func() {
		w.Header().Set("Content-Type", "text/html")
		w.Write(b1.Bytes())
	}()

	var vVSrc [][]byte
	dsObj1, err := dsu.BufGet(w, r, "dsu.WrapBlob__res_processed_01")
	util_err.Err_http(w, r, err, false)
	vVSrc = dsObj1.VVByte

	d := make(map[string]map[string]float64)

	distinctLangs := make(map[string]interface{})
	distinctPeriods := make(map[string]interface{})
	f_max := 0.0
	for i0 := 0; i0 < len(vVSrc); i0++ {
		//vVDest[i0] = []byte( b_row.Bytes() )
		s_row := string(vVSrc[i0])
		v_row := util.SplitByWhitespace(s_row)

		lang := v_row[0]
		period := v_row[1]
		count := v_row[2]
		fCount := util.Stof(count)
		if fCount > f_max {
			f_max = fCount
		}

		distinctLangs[lang] = 1
		distinctPeriods[period] = 1

		if _, ok := d[period]; !ok {
			d[period] = map[string]float64{}
		}
		d[period][lang] = fCount

	}
	//fmt.Fprintf(w,"%#v\n",d2)
	//fmt.Fprintf(w,"%#v\n",f_max)

	sortedPeriods := util.StringKeysToSortedArray(distinctPeriods)
	sortedLangs := util.StringKeysToSortedArray(distinctLangs)

	cd := CData{}
	_ = cd

	cd.M = d
	cd.VPeriods = sortedPeriods
	cd.VLangs = sortedLangs
	cd.F_max = f_max

	SaveChartDataToDatastore(w, r, cd, "chart_data_01")

	/*
		if r.FormValue("f") == "table" {
			showAsTable(w,r,cd)
		} else {
			showAsChart(w,r,cd)
		}
	*/

}
Esempio n. 28
0
func queryIntoDatastore(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	limitUpper := util.MonthsBack(1)
	limitLower := util.MonthsBack(25)

	var q bq.QueryRequest = bq.QueryRequest{}
	q.Query = `
		SELECT
		  repository_language
		, LEFT(repository_pushed_at,7) monthx
		, CEIL( count(*)/1000) Tausend
		FROM githubarchive:github.timeline
		where 1=1
			AND  LEFT(repository_pushed_at,7) >= '` + limitLower + `'
			AND  LEFT(repository_pushed_at,7) <= '` + limitUpper + `'
			AND  repository_language in ('Go','go','Golang','golang','C','Java','PHP','JavaScript','C++','Python','Ruby')
			AND  type="PushEvent"
		group by monthx, repository_language
		order by repository_language   , monthx
		;
	`

	c := appengine.NewContext(r)
	config := oauth2_google.NewAppEngineConfig(c, []string{
		"https://www.googleapis.com/auth/bigquery",
	})
	// The following client will be authorized by the App Engine
	// app's service account for the provided scopes.
	client := http.Client{Transport: config.NewTransport()}
	//client.Get("...")

	//oauthHttpClient := &http.Client{}
	bigqueryService, err := bq.New(&client)
	util_err.Err_http(w, r, err, false)

	fmt.Fprint(w, "s1<br>\n")

	// Create a query statement and query request object
	//  query_data = {'query':'SELECT TOP(title, 10) as title, COUNT(*) as revision_count FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;'}
	//  query_request = bigquery_service.jobs()
	// Make a call to the BigQuery API
	//  query_response = query_request.query(projectId=PROJECT_NUMBER, body=query_data).execute()

	js := bq.NewJobsService(bigqueryService)
	jqc := js.Query("347979071940", &q)

	fmt.Fprint(w, "s2 "+util.TimeMarker()+" <br>\n")
	resp, err := jqc.Do()
	util_err.Err_http(w, r, err, false)

	rows := resp.Rows
	var vVDest [][]byte = make([][]byte, len(rows))

	c.Errorf("%#v", rows)

	for i0, v0 := range rows {

		cells := v0.F

		b_row := new(bytes.Buffer)
		b_row.WriteString(fmt.Sprintf("r%0.2d -- ", i0))
		for i1, v1 := range cells {
			val1 := v1.V
			b_row.WriteString(fmt.Sprintf("c%0.2d: %v  ", i1, val1))
		}
		vVDest[i0] = []byte(b_row.Bytes())
	}

	key_combi, _ := dsu.BufPut(w, r, dsu.WrapBlob{Name: "bq_res1", VVByte: vVDest}, "bq_res1")
	dsObj, _ := dsu.BufGet(w, r, key_combi)

	printPlaintextTable(w, r, dsObj.VVByte)

	fmt.Fprint(w, "s3 "+util.TimeMarker()+" <br>\n")

}
Esempio n. 29
0
func drawLinesOverGrid(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)

	// prepare a line color
	lineCol := color.RGBA{}
	lineCol.R, lineCol.G, lineCol.B, lineCol.A = 255, 244, 22, 0
	c.Infof("brush color %#v \n", lineCol)

	p := r.FormValue("p")
	if p == "" {
		p = "static/chartbg_400x960__480x1040__12x10.png"
	}
	if p == "" {
		p = "static/pberg1.png"
	}

	f, err := os.Open(p)
	util_err.Err_http(w, r, err, false)
	defer f.Close()

	img, whichFormat, err := image.Decode(f)
	util_err.Err_http(w, r, err, false, "only jpeg and png are 'activated' ")
	c.Infof("serving img format %v %T\n", whichFormat, img)

	switch imgXFull := img.(type) {

	default:
		util_err.Err_http(w, r, false, true, "convertibility into internal color format image.RGBA required")

	case *image.RGBA:

		drawLine := FuncDrawLiner(lineCol, imgXFull)

		xb, yb := 40, 440
		P0 := image.Point{xb + 0, yb - 0}
		drawLine(P0, lineCol, imgXFull)

		for i := 0; i < 1; i++ {

			P1 := image.Point{xb + 80, yb - 80}
			drawLine(P1, lineCol, imgXFull)
			P1.X = xb + 160
			P1.Y = yb - 160
			drawLine(P1, lineCol, imgXFull)
			P1.X = xb + 240
			P1.Y = yb - 240
			drawLine(P1, lineCol, imgXFull)
			P1.X = xb + 320
			P1.Y = yb - 320
			drawLine(P1, lineCol, imgXFull)
			P1.X = xb + 400
			P1.Y = yb - 400
			drawLine(P1, lineCol, imgXFull)

			drawLine = FuncDrawLiner(lineCol, imgXFull)
			yb = 440
			P0 = image.Point{xb + 0, yb - 0}
			drawLine(P0, lineCol, imgXFull)

			P1 = image.Point{xb + 80, yb - 40}
			drawLine(P1, lineCol, imgXFull)
			P1.X = xb + 160
			P1.Y = yb - 90
			drawLine(P1, lineCol, imgXFull)
			P1.X = xb + 240
			P1.Y = yb - 120
			drawLine(P1, lineCol, imgXFull)
			P1.X = xb + 320
			P1.Y = yb - 300
			drawLine(P1, lineCol, imgXFull)
			P1.X = xb + 400
			P1.Y = yb - 310
			drawLine(P1, lineCol, imgXFull)

		}

		SaveImageToDatastore(w, r, imgXFull, "chart1")
		img := GetImageFromDatastore(w, r, "chart1")

		w.Header().Set("Content-Type", "image/png")
		png.Encode(w, img)

		// end case

	}
}
Esempio n. 30
0
func Get(w http.ResponseWriter, r *http.Request, m map[string]interface{}) *Instance {

	c := appengine.NewContext(r)

	startFunc := time.Now()

	if !ii.LastUpdated.IsZero() {

		age := startFunc.Sub(ii.LastUpdated)

		if age < 200*time.Millisecond {
			c.Infof("instance info update too recently: %v, skipping.\n", age)
			return ii
		}

		if age < 1*time.Hour {
			if len(ii.Hostname) > 2 {
				return ii
			}

		}

		c.Infof("instance info update too old: %v, recomputing.\n", age)
	}

	ii.ModuleName = appengine.ModuleName(c)
	ii.InstanceID = appengine.InstanceID()
	ii.VersionFull = appengine.VersionID(c)

	majorMinor := strings.Split(ii.VersionFull, ".")
	if len(majorMinor) != 2 {
		panic("we need a version string of format X.Y")
	}

	ii.VersionMajor = majorMinor[0]
	ii.VersionMinor = majorMinor[1]

	var err = errors.New("dummy creation error message")

	ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionFull)
	if err != nil {
		// this never works with version full
		// we do not log this - but try version major
		err = nil

		if !util_appengine.IsLocalEnviron() {
			ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionMajor)
			util_err.Err_http(w, r, err, true, "get num instances works only live and without autoscale")
		}

	}

	// in auto scaling, google reports "zero" - which can not be true
	// we assume at least 1
	if ii.NumInstances == 0 {
		ii.NumInstances = 1
	}

	// http://[0-2].1.default.libertarian-islands.appspot.com/instance-info

	ii.Hostname, err = appengine.ModuleHostname(c, ii.ModuleName,
		ii.VersionMajor, "")
	util_err.Err_http(w, r, err, false)

	if !util_appengine.IsLocalEnviron() {
		ii.HostnameInst0, err = appengine.ModuleHostname(c, ii.ModuleName,
			ii.VersionMajor, "0")
		if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) {
			c.Infof("inst 0: " + autoScalingErrMsg)
			err = nil
		}
		util_err.Err_http(w, r, err, true)

		ii.HostnameInst1, err = appengine.ModuleHostname(c, ii.ModuleName,
			ii.VersionMajor, "1")
		if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) {
			c.Infof("inst 1: " + autoScalingErrMsg)
			err = nil
		}
		util_err.Err_http(w, r, err, true)

		ii.HostnameMod02, err = appengine.ModuleHostname(c, "mod02", "", "")
		util_err.Err_http(w, r, err, true)

	}

	ii.LastUpdated = time.Now()

	c.Infof("collectInfo() completed, %v.%v.%v.%v, took %v",
		util.Ellipsoider(ii.InstanceID, 4), ii.VersionMajor, ii.ModuleName,
		ii.Hostname,
		ii.LastUpdated.Sub(startFunc))

	return ii
}