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

}
Example #2
0
func mockDateIntoDatastore(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)

	rand.Seed(time.Now().UnixNano())

	row_max := 100
	col_max := 3

	var languages []string = []string{"C", "C++", "Rambucto"}

	var vVDest [][]byte = make([][]byte, row_max)
	for i0 := 0; i0 < row_max; i0++ {

		vVDest[i0] = make([]byte, col_max)

		b_row := new(bytes.Buffer)
		b_row.WriteString(fmt.Sprintf("r%0.2d -- ", i0))

		for i1 := 0; i1 < col_max; i1++ {
			if i1 == 0 {
				val := languages[i0/10%3]
				b_row.WriteString(fmt.Sprintf(" c%0.2d: %-10.8v  ", i1, val))
			} else if i1 == 2 {
				val := rand.Intn(300)
				b_row.WriteString(fmt.Sprintf(" c%0.2d: %10v  ", i1, val))
			} else {

				f2 := "2006-01-02 15:04:05"
				f2 = "2006-01"
				tn := time.Now()
				//tn  = tn.Add( - time.Hour * 85 *24 )
				tn = tn.Add(-time.Hour * time.Duration(i0) * 24)
				val := tn.Format(f2)
				b_row.WriteString(fmt.Sprintf(" c%0.2d: %v  ", i1, val))
			}
		}
		vVDest[i0] = []byte(b_row.Bytes())

	}

	key_combi, _ := dsu.BufPut(c, dsu.WrapBlob{Name: "bq_res_test", VVByte: vVDest}, "bq_res_test")
	dsObj, _ := dsu.BufGet(c, key_combi)

	printPlaintextTable(w, r, dsObj.VVByte)

}
Example #3
0
func regroupFromDatastore01(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

	if util_appengine.IsLocalEnviron() {
		vVSrc = bq_statified_res1
	} else {
		dsObj1, _ := dsu.BufGet(c, "dsu.WrapBlob__bq_res1")
		vVSrc = dsObj1.VVByte
	}

	if r.FormValue("mock") == "1" {
		dsObj1, _ := dsu.BufGet(c, "dsu.WrapBlob__bq_res_test")
		vVSrc = dsObj1.VVByte
	}

	var vVDest [][]byte = make([][]byte, len(vVSrc))

	for i0 := 0; i0 < len(vVSrc); i0++ {

		s_row := string(vVSrc[i0])
		v_row := stringspb.SplitByWhitespace(s_row)
		b_row := new(bytes.Buffer)

		b_row.WriteString(fmt.Sprintf("%16.12s   ", v_row[3])) // leading spaces
		b_row.WriteString(fmt.Sprintf("%16.12s   ", v_row[5]))
		b_row.WriteString(fmt.Sprintf("%16.8s", v_row[7]))

		vVDest[i0] = []byte(b_row.Bytes())

	}

	key_combi, _ := dsu.BufPut(c, dsu.WrapBlob{Name: "res_processed_01", S: "[][]byte", VVByte: vVDest}, "res_processed_01")
	dsObj2, _ := dsu.BufGet(c, key_combi)

	printPlaintextTable(w, r, dsObj2.VVByte)

}
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
}
Example #5
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)

}
Example #6
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")

}
Example #7
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())
	}

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

	c := appengine.NewContext(r)

	nx := 24

	// without custom implementation
	// everything is encoded/decoded except field unexported
	orig := CData{
		M:          map[string]map[string]float64{"lang1": map[string]float64{"2012-09": 0.2}},
		VPeriods:   []string{"2011-11", "2014-11"},
		VLangs:     []string{"C", "++"},
		F_max:      44.2,
		unexported: "val of unexported",
	}
	fmt.Fprintf(w, "orig\n%#v\n", &orig)

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

	sx := serializedStruct.String()
	lx := len(sx)
	fmt.Fprintf(w, "byte data: \n%#v...%#v\n", sx[0:nx], sx[lx-nx:])

	// saving to ds
	key_combi, err := dsu.BufPut(c,
		dsu.WrapBlob{Name: "chart_data_test_1", VByte: serializedStruct.Bytes(), S: "chart data"}, "chart_data_test_1")
	loghttp.E(w, r, err, false)
	// restoring from ds
	dsObj, err := dsu.BufGet(c, key_combi)
	loghttp.E(w, r, err, false)

	p := r.FormValue("p")

	// reading
	rest1 := new(CData)
	if p == "" {
		sx := string(dsObj.VByte)
		lx := len(sx)
		fmt.Fprintf(w, "byte data: \n%#v...%#v\n", sx[0:nx], sx[lx-nx:])

		readr := bytes.NewBuffer(dsObj.VByte)
		dec := gob.NewDecoder(readr)
		err = dec.Decode(rest1)
		loghttp.E(w, r, err, false)
	} else {
		readr := bytes.NewBuffer(serializedStruct.Bytes())
		dec := gob.NewDecoder(readr)
		err = dec.Decode(rest1)
		loghttp.E(w, r, err, false)
	}

	fmt.Fprintf(w, "resl\n%#v\n", rest1)

	fmt.Fprintf(w, "\n\n")
	SaveChartDataToDatastore(w, r, orig, "chart_data_test_2")

	dsObj2, err := dsu.BufGet(c, "dsu.WrapBlob__chart_data_test_2")
	loghttp.E(w, r, err, false)
	{

		rest2 := new(CData)

		sx := string(dsObj2.VByte)
		lx := len(sx)
		fmt.Fprintf(w, "byte data: \n%#v...%#v\n", sx[0:nx], sx[lx-nx:])

		readr := bytes.NewBuffer(dsObj2.VByte)
		dec := gob.NewDecoder(readr)
		err = dec.Decode(rest2)
		loghttp.E(w, r, err, false)

		fmt.Fprintf(w, "res2\n%#v\n", rest2)

	}

	f1 := GetChartDataFromDatastore(w, r, "chart_data_test_2")
	fmt.Fprintf(w, "resl\n%#v\n", f1)

}
Example #9
0
/*

https://developers.coinbase.com/docs/merchants/callbacks


id				Order number used to uniquely identify an order on Coinbase
completed_at	ISO 8601 timestamp when the order completed
status			[completed, mispaid, expired]
event			[completed, mispayment]. If mispayment => check key mispayment_id. Distinction from status ...
total_btc		Total amount of the order in ‘satoshi’ (1 BTC = 100,000,000 Satoshi). Note the use of the word ‘cents’ in the callback really means satoshi in this context. The btc amount will be calculated at the current exchange rate at the time the order is placed (current to within 15 minutes).
total_native	Units of local currency. 1 unit = 100 cents. Equal to the price from creating the button.
total_payout	Units of local currency deposited using instant payout.
custom			Custom parameter from data-custom attribute of button. Usually an Order, User, or Product ID
receive_address	Bitcoin address associated with this order. This is where the payment was sent.
button			Button details. ID matches the data-code parameter in your embedded HTML code.
transaction		Hash and number of confirmations of underlying transaction.
				Number of confirmations typically zero at the time of the first callback.
customer		Customer information from order form. Can include email xor shipping address.
refund_address	Experimental parameter that is subject to change.


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

	lg, b := loghttp.BuffLoggerUniversal(w, r)
	closureOverBuf := func(bUnused *bytes.Buffer) {
		// loghttp.Pf(w, r, b.String())
	}
	defer closureOverBuf(b) // the argument is ignored,
	r.Header.Set("X-Custom-Header-Counter", "nocounter")

	htmlfrag.SetNocacheHeaders(w)

	//____________________________________________________________________

	bts, err := ioutil.ReadAll(r.Body)
	if err != nil {
		lg("cannot read resp body: %v", err)
		return
	}
	defer r.Body.Close()

	// lg("bytes are -%s-", stringspb.ToLen(string(bts), 20))

	if len(bts) < 1 {
		lg("lo empty post body")
		w.WriteHeader(http.StatusOK)
		b = new(bytes.Buffer)
		return
	}

	var mp map[string]interface{}
	err = json.Unmarshal(bts, &mp)
	lg(err)

	mpPayout := submap(mp, "payout", lg)
	if len(mpPayout) > 0 {
		lg("lo " + stringspb.IndentedDump(mpPayout))
	}
	mpAddress := submap(mp, "address", lg)
	if len(mpAddress) > 0 {
		lg("lo " + stringspb.IndentedDump(mpAddress))
	}

	var cents, BTC float64
	var status string

	mpOrder := submap(mp, "order", lg)
	if len(mpOrder) < 1 {
		w.WriteHeader(http.StatusLengthRequired)
		lg("mpOrder not present %v", status)
		return
	} else {
		lg("lo " + stringspb.IndentedDump(mpOrder))

		mpBTC := submap(mpOrder, "total_btc", lg)
		// lg("lo " + stringspb.IndentedDump(mpBTC))

		if icents, ok := mpBTC["cents"]; ok {
			cents, ok = icents.(float64)
			if !ok {
				lg(" mpBTC[cents] is of unexpected type %T ", mpBTC["cents"])
			}
			BTC = cents / (1000 * 1000 * 100)

		} else {
			lg(" mpBTC[cents] not present")
		}
		lg("received %18.2f satoshi, %2.9v BTC ", cents, BTC)

		if _, ok := mpOrder["status"]; ok {
			status, ok = mpOrder["status"].(string)
			if !ok {
				lg(" mpOrder[status] is of unexpected type %T ", mpOrder["status"])
			}
		}

		lg("status    %v  ", status)
		lg("custom   %#v  ", mpOrder["custom"])
		lg("customer %#v - mostly empty", mpOrder["customer"])

		var values url.Values
		if _, ok := mpOrder["custom"]; ok {
			if mpOrder["custom"] == "123456789" {
				lg("test request recognized")
				values = url.Values{}
				values.Add("uID", "testUser123")
				values.Add("productID", "/member/somearticle")
			} else {
				var err error
				values, err = url.ParseQuery(mpOrder["custom"].(string))
				lg(err)
				if err != nil {
					w.WriteHeader(http.StatusLengthRequired)
					lg("unsatisfactory query in custom string %v", mpOrder["custom"])
					return
				}
			}
		} else {
			w.WriteHeader(http.StatusLengthRequired)
			lg("custom string not present")
			return
		}

		//  save
		if status == "completed" {
			lg("status 'completed'")
			blob := dsu.WrapBlob{
				VByte: stringspb.IndentedDumpBytes(mpOrder),
			}
			blob.Name = values.Get("uID")
			blob.Category = "invoice"
			blob.S = values.Get("productID")
			blob.Desc = status
			blob.F = BTC
			blob.I = int(time.Now().Unix())

			// blob.VVByte, _ = conv.String_to_VVByte(string(blob.VByte)) // just to make it readable

			newKey, err := dsu.BufPut(appengine.NewContext(r), blob, blob.Name+blob.S)
			lg("key is %v", newKey)
			lg(err)

			retrieveAgain, err := dsu.BufGet(appengine.NewContext(r), "dsu.WrapBlob__"+blob.Name+blob.S)
			lg(err)
			lg("retrieved %v %v %v", retrieveAgain.Name, retrieveAgain.Desc, retrieveAgain.F)

		} else {
			w.WriteHeader(http.StatusLengthRequired)
			lg("unsatisfactory status %v", status)
			return
		}

	}
	w.WriteHeader(http.StatusOK)
	b = new(bytes.Buffer)

}