func Put(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) url := r.FormValue("url") code := new(int) if err := datastore.RunInTransaction(c, func(c appengine.Context) error { dao := ImgDAO{Context: c} ok, err := dao.Exists(url) if err != nil { return err } c.Infof("REPOST: %v", ok) if ok { *code = http.StatusNotModified return nil } *code = http.StatusNoContent i, err := img.Fetch(url, urlfetch.Client(c)) if err != nil { return err } return dao.Put(url, i) }, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Location", fmt.Sprint("?url=", url)) w.WriteHeader(*code) }
func Post(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) c.Infof("INSIDE POST") url := r.FormValue("url") c.Infof("Requested URL: %v", url) i, err := img.Fetch(url, urlfetch.Client(c)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } dao := ImgDAO{Context: c} if err := dao.Put(url, i); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Location", fmt.Sprint("?url=", url)) w.WriteHeader(http.StatusNoContent) }