inbound_services:
- channel_presence
handlers:
- url: /_ah/channel/connected/
script: _go_app
func init() {
// ...
http.HandleFunc("/_ah/channel/connected/", connected)
}
func connected(w http.ResponseWriter, r *http.Request) {
clientId := r.FormValue("from")
c := appengine.NewContext(r)
c.Infof("connected '%v'", clientId)
images(clientId, c)
}
type Request struct {
ClientId string
}
type Thumbnail struct {
ThumbnailURL string
Blob appengine.BlobKey // BlobKey is a string
}
const rootNode = "image-scrap-request"
const thumbnailLeaf = "image-scrap-thumbnail"
const rubbish = "rubbish"
func start(w http.ResponseWriter, r *http.Request) {
// ...
isr := Request{clientId}
key, err := datastore.Put(c, datastore.NewIncompleteKey(c, rootNode, nil), &isr)
if error3(err, c, w) { return }
for _, image := range images {
addr := string(image[1])
if strings.Index(addr, "http") == 0 {
task := taskqueue.NewPOSTTask("/fetch", url.Values{"clientId": {clientId}, "image": {addr}, "key": {key.Encode()}})
_, err := taskqueue.Add(c, task, "default")
if error3(err, c, w) { return }
}
}
// ...
}
func fetch(w http.ResponseWriter, r *http.Request) {
// ...
key, err := datastore.DecodeKey(r.FormValue("key"))
if error3(err, c, w) { return }
// ...
thumbnail := thumbnailUrl.String()
// ...
ist := Thumbnail{thumbnail, blobkey}
_, err = datastore.Put(c, datastore.NewIncompleteKey(c, thumbnailLeaf, key), &ist)
if error3(err, c, w) { return }
}
func images(clientId string, c appengine.Context) {
c.Infof("images for '%v'", clientId)
iterate(clientId, c)
}
func iterate(clientId string, c appengine.Context) {
root := datastore.NewQuery(rootNode).Filter("ClientId = ", clientId).Run(c)
for {
var parent Request
rootKey, err := root.Next(&parent)
if err == datastore.Done {
break
}
if error2(err, c) { return }
thumbnails := datastore.NewQuery(thumbnailLeaf).Ancestor(rootKey).Run(c)
for {
var thumbnail Thumbnail
key, err := thumbnails.Next(&thumbnail)
if err == datastore.Done {
break
}
if error2(err, c) { return }
c.Infof("pushing from db '%v'", thumbnail.ThumbnailURL)
channel.Send(c, clientId, thumbnail.ThumbnailURL)
}
}
}