Fix paste handler and uuid collision (Fixes #9)

This commit is contained in:
James Mills 2021-04-09 21:35:57 +10:00
parent 10645aa3a5
commit 93cce3bc87
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
@ -23,7 +22,6 @@ import (
rice "github.com/GeertJohan/go.rice"
"github.com/julienschmidt/httprouter"
"github.com/patrickmn/go-cache"
"github.com/renstrom/shortuuid"
"github.com/timewasted/go-accept-headers"
)
@ -117,20 +115,15 @@ func (s *Server) PasteHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s.counters.Inc("n_paste")
body, err := ioutil.ReadAll(r.Body)
log.Printf("body: %v", body)
if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
blob := r.FormValue("blob")
if len(body) == 0 {
if len(blob) == 0 {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
uuid := shortuuid.NewWithNamespace(s.config.fqdn)
s.store.Set(uuid, string(body), cache.DefaultExpiration)
uuid := RandomString(8)
s.store.Set(uuid, blob, cache.DefaultExpiration)
u, err := url.Parse(fmt.Sprintf("./p/%s", uuid))
if err != nil {