From 61f6715be94eab71b9922f638060e0607cc92140 Mon Sep 17 00:00:00 2001 From: Ada Date: Thu, 12 Oct 2023 21:46:30 +0200 Subject: [PATCH] add(backend): support paste deletion --- main.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/main.go b/main.go index eda667a..34e05d9 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { if urlExist(clearPath) { if strings.HasSuffix(path, "/raw") { pasteContent := db.HGet(ctx, clearPath, "content").Val() + w.Header().Set("Content-Type", "text/plain") _, err := io.WriteString(w, pasteContent) if err != nil { log.Println(err) @@ -62,6 +63,26 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { } else { w.WriteHeader(http.StatusMethodNotAllowed) } + case "DELETE": + if strings.HasPrefix(path, "/delete") { + urlItem := strings.Split(path, "/") + if urlExist("/" + urlItem[2]) { + secret := r.URL.Query().Get("secret") + if secret == db.HGet(ctx, "/"+urlItem[2], "secret").Val() { + err := db.Del(ctx, "/"+urlItem[2]) + if err != nil { + log.Println(err) + } + w.WriteHeader(http.StatusNoContent) + } else { + w.WriteHeader(http.StatusForbidden) + } + } else { + w.WriteHeader(http.StatusNotFound) + } + } else { + w.WriteHeader(http.StatusMethodNotAllowed) + } } }