Ejemplo n.º 1
0
// 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)
	loghttp.E(w, r, err, false)
	defer f.Close()

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

	imgRGBA, ok := img.(*image.RGBA)
	loghttp.E(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, "\"> ")

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

	contnt, ok := m["content"].(string)
	loghttp.E(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, GbEntryKind, keyParent(c))
	concreteNewKey, err := ds.Put(c, incomplete, &g)
	loghttp.E(w, r, err, false)
	_ = concreteNewKey // we query entries via keyParent - via parent

}
Ejemplo n.º 3
0
func makeRequest(w http.ResponseWriter, r *http.Request, path string) CGI_Result {

	c := appengine.NewContext(r)
	client := urlfetch.Client(c)

	url_exe := spf(`http://%s%s%s&ts=%s`, dns_cam, path, credentials, urlParamTS())
	url_dis := spf(`http://%s%s&ts=%s`, dns_cam, path, urlParamTS())
	wpf(w, "<div style='font-size:10px; line-height:11px;'>requesting %v<br></div>\n", url_dis)
	resp1, err := client.Get(url_exe)
	loghttp.E(w, r, err, false)

	bcont, err := ioutil.ReadAll(resp1.Body)
	defer resp1.Body.Close()
	loghttp.E(w, r, err, false)

	cgiRes := CGI_Result{}
	xmlerr := xml.Unmarshal(bcont, &cgiRes)
	loghttp.E(w, r, xmlerr, false)

	if cgiRes.Result != "0" {
		wpf(w, "<b>RESPONSE shows bad mood:</b><br>\n")
		psXml := stringspb.IndentedDump(cgiRes)
		dis := strings.Trim(psXml, "{}")
		wpf(w, "<pre style='font-size:10px;line-height:11px;'>%v</pre>", dis)
	}

	if debug {
		scont := string(bcont)
		wpf(w, "<pre style='font-size:10px;line-height:11px;'>%v</pre>", scont)
	}

	return cgiRes

}
Ejemplo n.º 4
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}}` )
	//	loghttp.E(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")
			loghttp.E(w, r, err, false, "could not open static template file")
			v = string(fcontent)
		}

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

	}

	return tder
}
Ejemplo n.º 5
0
func submitUpload(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

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

	w.Header().Set("Content-type", "text/html; charset=utf-8")
	err = upload2.Execute(w, uploadURL)
	loghttp.E(w, r, err, false)
}
Ejemplo n.º 6
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(GbEntryKind).Ancestor(keyParent(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{})
		loghttp.E(w, r, err, true)
		err = nil // ignore this one - it's caused by our deliberate differences between gbsaveEntry and gbEntrieRetr
	}
	loghttp.E(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()

	for _, gbe := range gbEntries {
		s := gbe.Comment1
		if len(s) > 0 {
			if pos := strings.Index(s, "0300"); pos > 1 {
				i1 := util.Max(pos-4, 0)
				i2 := util.Min(pos+24, len(s))
				s1 := s[i1:i2]
				s1 = strings.Replace(s1, "3", "E", -1)
				report = fmt.Sprintf("%v -%v", report, s1)
			}
		}
	}

	return
}
Ejemplo n.º 7
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))
	loghttp.E(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
	loghttp.E(w, r, err, false)

	// create two clones
	// now both containing T0 and T1
	tc_1, err := t_base.Clone()
	loghttp.E(w, r, err, false)
	tc_2, err := t_base.Clone()
	loghttp.E(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}}")
	loghttp.E(w, r, err, false)
	tc_2, err = tc_2.Parse("{{define `T2`}}T2-B  <br>--" + s_if + "--  {{end}}")
	loghttp.E(w, r, err, false)

	// writing both clones to the response writer
	err = tc_1.ExecuteTemplate(w, "str_T0_outmost", nil)
	loghttp.E(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)
	loghttp.E(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()
	loghttp.E(w, r, err, false)
	err = tc_3.ExecuteTemplate(w, "str_T0_outmost", dyndata)
	// NOT logging the error:
	// loghttp.E(w, r, err, false)

}
Ejemplo n.º 8
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 {

	// use INDEX to access certain elements
	funcMap := tt.FuncMap{
		"unescape":   html.UnescapeString,
		"escape":     html.EscapeString,
		"fMult":      fMult,
		"fAdd":       fAdd,
		"fChop":      fChop,
		"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()
	loghttp.E(w, r, err, false)

	return t_derived
}
Ejemplo n.º 9
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)
	loghttp.E(w, r, err, false, "could not send the email")

}
Ejemplo n.º 10
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)
	loghttp.E(w, r, err, false)
	defer f.Close()

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

	switch t := img.(type) {

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

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

		imgXCutout, ok := imgXFull.SubImage(rec).(*image.RGBA)
		loghttp.E(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 GetChartDataFromDatastore(w http.ResponseWriter, r *http.Request, key string) *CData {

	c := appengine.NewContext(r)

	key_combi := "dsu.WrapBlob__" + key

	dsObj, err := dsu.BufGet(c, key_combi)
	loghttp.E(w, r, err, false)

	serializedStruct := bytes.NewBuffer(dsObj.VByte)
	dec := gob.NewDecoder(serializedStruct)
	newCData := new(CData) // hell, it was set to nil above - causing this "unassignable value" error
	err = dec.Decode(newCData)
	loghttp.E(w, r, err, false, "VByte was ", dsObj.VByte[:10])

	return newCData
}
Ejemplo n.º 12
0
func call(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	// 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)
	loghttp.E(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 {
		loghttp.E(w, r, err, false, "twilio response not ok", resp.Status)
	}
}
Ejemplo n.º 13
0
func GetImageFromDatastore(w http.ResponseWriter, r *http.Request, key string) *image.RGBA {

	c := appengine.NewContext(r)

	dsObj, err := dsu.BufGet(c, "dsu.WrapBlob__"+key)
	loghttp.E(w, r, err, false)

	s := string(dsObj.VByte)

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

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

	return i
}
func SaveChartDataToDatastore(w http.ResponseWriter, r *http.Request, cd CData, key string) string {

	c := appengine.NewContext(r)

	internalType := fmt.Sprintf("%T", cd)
	//buffBytes, _	 := StringToVByte(s)  // instead of []byte(s)

	// CData to []byte
	serializedStruct := new(bytes.Buffer)
	enc := gob.NewEncoder(serializedStruct)
	err := enc.Encode(cd)
	loghttp.E(w, r, err, false)

	key_combi, err := dsu.BufPut(c,
		dsu.WrapBlob{Name: key, VByte: serializedStruct.Bytes(), S: internalType}, key)
	loghttp.E(w, r, err, false)

	return key_combi
}
Ejemplo n.º 15
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)
	loghttp.E(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)
		loghttp.E(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})
	}

}
Ejemplo n.º 16
0
func SaveImageToDatastore(w http.ResponseWriter, r *http.Request, i *image.RGBA, key string) string {

	c := appengine.NewContext(r)

	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(c, dsu.WrapBlob{Name: key, VByte: []byte(s), S: internalType}, key)
	loghttp.E(w, r, err, false)

	return key_combi

}
func saveURLWithAncestor(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	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)
	loghttp.E(w, r, err, false)

}
// saving some data by kind and key
//   without ancestor key
func saveURLNoAnc(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	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 {
		c.Errorf("%v", err)
	} else {
		loghttp.E(w, r, err, false)
	}

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

	_, err = ds.Put(c, k, e)
	loghttp.E(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"))

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

	c := appengine.NewContext(r)

	// 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)

	loghttp.E(w, r, err, false)

	http.Redirect(w, r, url.String(), http.StatusFound)
}
Ejemplo n.º 20
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)
		loghttp.E(w, r, err, false)

	}

	return f1, f2
}
Ejemplo n.º 21
0
func processUpload(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	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)
	loghttp.E(w, r, err, false, fmt.Errorf("Fehler beim Parsen: %v", err))

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

	// s2 := stringspb.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)
}
// get all ancestor urls
//  ordering possible
//  distinction to above: *always* consistent
func listURLWithAncestors(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	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)
	loghttp.E(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")

}
Ejemplo 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

	c := appengine.NewContext(r)

	wbl := dsu.WrapBlob{}
	wbl.Category = "print"
	wbl.Name = otherFormFields["title"][0] + " - " + otherFormFields["descr"][0]
	wbl.Name += " - " + stringspb.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(c, wbl, keyX2)
	loghttp.E(w, r, errDS, false)

}
Ejemplo n.º 24
0
func writeMethods(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)

	client := urlfetch.Client(c)

	ii := instance_mgt.Get(r)
	resp2, err := client.Get(spf(`http://%s/write-methods-read`, ii.PureHostname))
	loghttp.E(w, r, err, false)

	bufDemo := new(bytes.Buffer)
	bufDemo.WriteString("end of page")
	defer func() {
		//w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.Write(bufDemo.Bytes())

		resp2.Body.Close()
	}()

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	fmt.Fprint(w, "<pre>")

	//
	//
	fmt.Fprint(w, `Most basic:
		this is written via Fprintln into response writer`+"\n\n\n")

	//
	// byte slice into response writer
	const sz = 20
	var sB []byte
	sB = make([]byte, sz)
	sB[0] = 112
	sB[1] = 111
	sB[2] = '-'
	sB[3] = 112
	sB[4] = 101
	sB[5] = 108
	sB[6] = 32
	for i := 7; i < sz; i++ {
		sB[i] = ' '
	}
	sB[sz-1] = '!'

	w.Write([]byte("Byte slice into response writer: \n\t\t"))
	w.Write(sB)
	w.Write([]byte("\n\n\n"))

	//
	//
	// resp2.Body into byte slice,
	sB2 := make([]byte, sz)
	for i := 0; i < sz; i++ {
		sB2[i] = '-'
	}
	bytesRead, err := resp2.Body.Read(sB2)
	if err == nil {
		fmt.Fprintf(w, "Byte slice - reading %v bytes from response-body\n\t\t%q \n\n\n",
			bytesRead, string(sB2))
	} else {
		fmt.Fprintf(w, "err reading into byte slice  --%v-- \n\n\n", err)
	}

	//
	//
	//
	wpf(w, "operations with a bytes buffer\n")
	var buf1 *bytes.Buffer
	buf1 = new(bytes.Buffer) // not optional on buffer pointer
	buf1.ReadFrom(resp2.Body)

	buf1 = new(bytes.Buffer)
	wpf(buf1, "\t\tbuf1 content %v (filled via Fprintf)\n", 222)

	wpf(w, "FOUR methods of dumping buf1 into resp.w:\n")
	wpf(w, "\tw.Write\n")
	w.Write(buf1.Bytes())
	wpf(w, "\tFprint\n")
	wpf(w, buf1.String())
	wpf(w, "\tio.WriteString\n")
	io.WriteString(w, buf1.String())
	wpf(w, "\tio.Copy \n")
	io.Copy(w, buf1) // copy the bytes.Buffer into w
	wpf(w, " \t\t\tio.copy exhausts buf1 - Fprinting again yields %q ", buf1.String())
	wpf(w, buf1.String())
	wpf(w, "\n\n\n")

	//
	//
	//
	wpf(w, "ioutil.ReadAll\n")
	var content []byte
	resp3, err := client.Get(spf(`http://%s/write-methods-read`, ii.Hostname))
	loghttp.E(w, r, err, false)
	content, _ = ioutil.ReadAll(resp3.Body)
	scont := string(content)
	scont = stringspb.Ellipsoider(scont, 20)
	w.Write([]byte(scont))

	fmt.Fprint(w, "</pre>")

}
Ejemplo n.º 25
0
/*

	Domain is different!!!

	appspotMAIL.com

	not

	appspot.com

	peter@[email protected]


  https://developers.google.com/appengine/docs/python/mail/receivingmail

 	email-address:	 [email protected]
 	is routed to
   /_ah/mail/[email protected]

   [email protected]
 	is routed to
   /_ah/mail/[email protected]
*/
func emailReceiveAndStore(w http.ResponseWriter, r *http.Request, mx map[string]interface{}) {

	c := appengine.NewContext(r)
	defer r.Body.Close()

	msg, err := go_mail.ReadMessage(r.Body)
	loghttp.E(w, r, err, false, "could not do ReadMessage")
	if msg == nil {
		aelog.Warningf(c, "-empty msg- "+r.URL.Path)
		return
	}

	// see http://golang.org/pkg/net/mail/#Message
	b1 := new(bytes.Buffer)
	// for i, m1 := range msg.Header {
	// 	aelog.Infof(c,"--msg header %q : %v", i, m1)
	// }

	from := msg.Header.Get("from") + "\n"
	b1.WriteString("from: " + from)

	to := msg.Header.Get("to") + "\n"
	b1.WriteString("to: " + to)

	subject := msg.Header.Get("subject") + "\n"
	b1.WriteString("subject: " + subject)

	when, _ := msg.Header.Date()
	swhen := when.Format("2006-01-02 - 15:04 \n")
	b1.WriteString("when: " + swhen)

	ctype := msg.Header.Get("Content-Type")
	aelog.Infof(c, "content type header: %q", ctype)
	boundary := ""
	// [multipart/mixed; boundary="------------060002090509030608020402"]
	if strings.HasPrefix(ctype, "[multipart/mixed") ||
		strings.HasPrefix(ctype, "multipart/mixed") {
		vT1 := strings.Split(ctype, ";")
		if len(vT1) > 1 {
			aelog.Infof(c, "substring 1: %q", vT1[1])
			sT11 := vT1[1]
			sT11 = strings.TrimSpace(sT11)
			sT11 = strings.TrimPrefix(sT11, "boundary=")
			sT11 = strings.Trim(sT11, `"`)
			boundary = sT11
			aelog.Infof(c, "substring 2: %q", boundary)
		}
	}

	b1.WriteString("\n\n")
	b1.ReadFrom(msg.Body)

	dsu.McacheSet(c, keyLatest, dsu.WrapBlob{Name: subject, S: boundary, VByte: b1.Bytes()})
	if strings.HasPrefix(to, "foscam") {
		// send confirmation to sender
		var m map[string]string = nil
		m = make(map[string]string)
		m["sender"] = from
		m["subject"] = "confirmation: " + subject
		emailSend(w, r, m)

		parseFurther(w, r, true)
		call(w, r, mx)
	} else {
		blob := dsu.WrapBlob{Name: subject + "from " + from + "to " + to,
			S: boundary, VByte: b1.Bytes()}
		blob.VVByte, _ = conv.String_to_VVByte(b1.String())
		dsu.BufPut(c, blob, "email-"+util.TimeMarker())
	}

}
Ejemplo n.º 26
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)
	loghttp.E(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 = stringspb.LowerCasedUnderscored(vv[1])
						}
					}
					s = s[start+len(sepHeaderContent):]
					if posSemicol := strings.Index(h, ";"); posSemicol > 0 {
						ctype = h[0:posSemicol]
					}
				}
			}

			if ctype == "" {
				b.WriteString("unparseable: " + stringspb.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++
					aelog.Infof(c, "Put image into reservoir %v %v", fn, ctype)
				}
			}

		}

	}

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

	c := appengine.NewContext(r)

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

	var vVSrc [][]byte
	dsObj1, err := dsu.BufGet(c, "dsu.WrapBlob__res_processed_01")
	loghttp.E(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 := stringspb.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 := sortmap.StringKeysToSortedArray(distinctPeriods)
	sortedLangs := sortmap.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)
		}
	*/

}
Ejemplo 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)

	// The following client will be authorized by the App Engine
	// app's service account for the provided scopes.
	// "https://www.googleapis.com/auth/bigquery"
	// "https://www.googleapis.com/auth/devstorage.full_control"

	// 2015-06: instead of oauth2.NoContext we get a new type of context
	var ctx context.Context = appengine.NewContext(r)
	oauthHttpClient, err := google.DefaultClient(
		ctx, "https://www.googleapis.com/auth/bigquery")

	if err != nil {
		log.Fatal(err)
	}

	bigqueryService, err := bq.New(oauthHttpClient)

	loghttp.E(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()
	loghttp.E(w, r, err, false)

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

	aelog.Errorf(c, "%#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(c, dsu.WrapBlob{Name: "bq_res1", VVByte: vVDest}, "bq_res1")
	dsObj, _ := dsu.BufGet(c, key_combi)

	printPlaintextTable(w, r, dsObj.VVByte)

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

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

	c := appengine.NewContext(r)

	q := ds.NewQuery(gbp.GbEntryKind)
	q.Order("-Date")

	b1 := new(bytes.Buffer)

	cur_start, err := memcache.Get(c, "greeting_cursor")
	if err == nil {
		str_curs := string(cur_start.Value)
		if len(cur_start.Value) > 0 {
			cursor, err := ds.DecodeCursor(str_curs) //  inverse is string()
			loghttp.E(w, r, err, false)
			if err == nil {
				b1.WriteString("found cursor from memcache -" + stringspb.Ellipsoider(str_curs, 10) + "-<br>\n")
				q = q.Start(cursor)
			}
		}
	}

	iter := q.Run(c)
	var cntr int = 0
	for {
		var g gbp.GbEntryRetr
		cntr++
		if cntr > 2 {
			b1.WriteString("  batch complete -" + string(cntr) + "-<br>\n")
			break

		}

		_, err := iter.Next(&g)
		if err == ds.Done {
			b1.WriteString("scan complete -" + string(cntr) + "-<br>\n")
			break
		}

		if fmt.Sprintf("%T", err) == fmt.Sprintf("%T", new(ds.ErrFieldMismatch)) {
			err = nil // ignore this one - it's caused by our deliberate differences between gbsaveEntry and gbEntrieRetr
		}

		if err != nil {
			b1.WriteString("error fetching next: " + err.Error() + "<br>\n")
			break
		}

		b1.WriteString("  - " + g.String())
	}

	// Get updated cursor and store it for next time.
	if cur_end, err := iter.Cursor(); err == nil {

		str_c_end := cur_end.String() //  inverse is decode()
		val := []byte(str_c_end)

		mi_save := &memcache.Item{
			Key:        "greeting_cursor",
			Value:      val,
			Expiration: 60 * time.Second,
		}

		if err := memcache.Set(c, mi_save); err != nil {
			b1.WriteString("error adding memcache item " + err.Error() + "<br>\n")
		} else {
			b1.WriteString("wrote cursor to memcache -" + stringspb.Ellipsoider(str_c_end, 10) + "-<br>\n")
		}

	} else {
		b1.WriteString("could not retrieve cursor_end " + err.Error() + "<br>\n")
	}

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

	w.Write([]byte("<br>----<br>"))

}
Ejemplo n.º 30
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)
	loghttp.E(w, r, err, false)
	defer f.Close()

	img, whichFormat, err := image.Decode(f)
	loghttp.E(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:
		loghttp.E(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

	}
}