Exemplo n.º 1
0
Arquivo: anif.go Projeto: hamax/avy
// Get a file that is part of a module
func getModuleFile(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	vars := mux.Vars(r)

	// Get module object
	accKey, _, err := model.GetAccountByDevname(c, vars["devname"])
	if err != nil {
		common.ServeError(c, w, err)
		return
	}
	if accKey == nil {
		common.Serve404(w)
		return
	}
	key := datastore.NewKey(c, "module", vars["name"], 0, accKey)

	var e model.Module
	err = datastore.Get(c, key, &e)
	if err != nil {
		if err == datastore.ErrNoSuchEntity {
			common.Serve404(w)
			return
		}
		common.ServeError(c, w, err)
		return
	}

	// Find blob key
	filename := vars["filename"]
	for i := range e.Files {
		if e.Files[i].Filename == filename {
			blobstore.Send(w, appengine.BlobKey(e.Files[i].BlobKey))
			return
		}
	}

	common.Serve404(w)
}
Exemplo n.º 2
0
Arquivo: modules.go Projeto: hamax/avy
// Get details about a module
func getModule(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	vars := mux.Vars(r)

	accKey, _, err := model.GetAccountByDevname(c, vars["devname"])
	if err != nil {
		if err == datastore.ErrNoSuchEntity {
			common.Serve404(w)
			return
		}
		common.ServeError(c, w, err)
		return
	}
	key := datastore.NewKey(c, "module", vars["name"], 0, accKey)

	var e model.Module
	err = datastore.Get(c, key, &e)
	if err != nil {
		if err == datastore.ErrNoSuchEntity {
			common.Serve404(w)
			return
		}
		common.ServeError(c, w, err)
		return
	}

	res := map[string]interface{}{
		"Owner":   key.Parent().StringID(),
		"Devname": e.Devname,
		"Name":    e.Name,
		"Date":    e.Date,
		"Files":   e.Files,
	}

	common.WriteJson(c, w, res)
}
Exemplo n.º 3
0
Arquivo: modules.go Projeto: hamax/avy
// Upload a file to a module
func uploadModuleFile(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	vars := mux.Vars(r)
	u := user.Current(c)

	// Check if user is logged in
	if u == nil {
		common.Serve401(w)
		return
	}

	accKey, _, err := model.GetAccountByDevname(c, vars["devname"])
	if err != nil {
		if err == datastore.ErrNoSuchEntity {
			common.Serve404(w)
			return
		}
		common.ServeError(c, w, err)
		return
	}
	key := datastore.NewKey(c, "module", vars["name"], 0, accKey)

	// Check if user is the owner
	if key.Parent().StringID() != u.ID {
		common.Serve403(w)
		return
	}

	// Start a datastore transaction
	var e model.Module
	err = datastore.RunInTransaction(c, func(c appengine.Context) error {
		// Get the visualization object
		err := datastore.Get(c, key, &e)
		if err != nil {
			return err
		}

		if vars["action"] == "delete" {
			e.Files, err = deleteFile(c, r, e.Files)
			if err != nil {
				return err
			}
		} else {
			e.Files, err = uploadFile(c, r, e.Files)
			if err != nil {
				return err
			}
		}

		// Save the visualization object
		key, err = datastore.Put(c, key, &e)
		return err
	}, nil)
	if err != nil {
		if err == datastore.ErrNoSuchEntity {
			common.Serve404(w)
			return
		}
		common.ServeError(c, w, err)
		return
	}

	res := map[string]interface{}{
		"Owner":   key.Parent().StringID(),
		"Devname": e.Devname,
		"Name":    e.Name,
		"Date":    e.Date,
		"Files":   e.Files,
	}

	common.WriteJson(c, w, res)
}