func (a *Handler) PostSaved(ctx context.Context, w http.ResponseWriter, r *http.Request) error { c, err := rellenv.FromContext(ctx) if err != nil { return err } if !rellenv.IsEmployee(ctx) { return ctxerr.Wrap(ctx, errSaveDisabled) } if !a.Xsrf.Validate(r.FormValue(paramName), w, r, savedPath) { return ctxerr.Wrap(ctx, errTokenMismatch) } content := strings.TrimSpace(r.FormValue("code")) content = strings.Replace(content, "\x13", "", -1) // remove CR id := examples.ContentID(content) db := a.ExampleStore.DB example, ok := db.Reverse[id] if ok { http.Redirect(w, r, c.ViewURL(example.URL), 302) return nil } err = a.ExampleStore.Save(id, content) if err != nil { return err } http.Redirect(w, r, c.ViewURL(savedPath+id), 302) return nil }
func Saved(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" && r.URL.Path == savedPath { c, err := context.FromRequest(r) if err != nil { view.Error(w, r, err) return } if !xsrf.Validate(r.FormValue(paramName), w, r, savedPath) { service.Stats.Inc(savedPath + " xsrf failure") view.Error(w, r, errTokenMismatch) return } content := bytes.TrimSpace([]byte(r.FormValue("code"))) content = bytes.Replace(content, []byte{13}, nil, -1) // remove CR id := examples.ContentID(content) db := examples.GetDB(c.Version) example, ok := db.Reverse[id] if ok { http.Redirect(w, r, c.ViewURL(example.URL), 302) return } err = exampleStore.Save(id, content) if err != nil { view.Error(w, r, err) return } service.Stats.Inc("saved example") http.Redirect(w, r, c.ViewURL(savedPath+id), 302) return } else { context, example, err := parse(r) if err != nil { view.Error(w, r, err) return } service.Stats.Inc("viewed saved example") view.Write(w, r, &page{ Writer: w, Request: r, Context: context, Example: example, }) } }