1
0
mirror of https://github.com/taigrr/pastebin synced 2026-04-16 23:34:58 -07:00

Fix pb utility (#16)

* Update dependencies and go compile target version

* Adds deletion support to the server

* Add form around delete button

* Add support for returning the url in curl

* remove -config option for now

* Upload as form value instead of file

* remove dependencies from go mod

* update to README re: new curl usage
This commit is contained in:
2021-04-20 06:20:39 -07:00
committed by GitHub
parent 1c454dc629
commit 5ade7a0642
7 changed files with 74 additions and 23 deletions

View File

@@ -115,6 +115,15 @@ func (s *Server) PasteHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s.counters.Inc("n_paste")
accepts, err := accept.Negotiate(
r.Header.Get("Accept"), AcceptedTypes...,
)
if err != nil {
log.Printf("error negotiating: %s", err)
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
blob := r.FormValue("blob")
if len(blob) == 0 {
@@ -129,7 +138,14 @@ func (s *Server) PasteHandler() httprouter.Handle {
if err != nil {
http.Error(w, "Internal Error", http.StatusInternalServerError)
}
http.Redirect(w, r, r.URL.ResolveReference(u).String(), http.StatusFound)
switch accepts {
case "text/html":
http.Redirect(w, r, r.URL.ResolveReference(u).String(), http.StatusFound)
case "text/plain":
fallthrough
default:
w.Write([]byte(r.Host + r.URL.ResolveReference(u).String()))
}
}
}
@@ -160,6 +176,35 @@ func (s *Server) DownloadHandler() httprouter.Handle {
}
}
// DeleteHandler
func (s *Server) DeleteHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s.counters.Inc("n_delete")
_, err := accept.Negotiate(
r.Header.Get("Accept"), AcceptedTypes...,
)
if err != nil {
log.Printf("error negotiating: %s", err)
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
uuid := p.ByName("uuid")
if uuid == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
_, ok := s.store.Get(uuid)
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
s.store.Delete(uuid)
http.Error(w, "Deleted", http.StatusOK)
}
}
// ViewHandler ...
func (s *Server) ViewHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@@ -202,7 +247,7 @@ func (s *Server) ViewHandler() httprouter.Handle {
},
)
case "text/plain":
w.Write([]byte(blob))
fallthrough
default:
w.Write([]byte(blob))
}
@@ -246,6 +291,11 @@ func (s *Server) initRoutes() {
s.router.POST("/", s.PasteHandler())
s.router.GET("/download/:uuid", s.DownloadHandler())
s.router.GET("/p/:uuid", s.ViewHandler())
// Enable DELETE from curl/wget/cli
s.router.DELETE("/p/:uuid", s.DeleteHandler())
// Add alternate path since form actions don't support method=DELETE
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE
s.router.POST("/p/:uuid/delete", s.DeleteHandler())
}
// NewServer ...