From 43a2f6b9d5ecbc68a96313f4c8f2950ddff5cc29 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Fri, 1 Jul 2022 03:31:33 +0400 Subject: [PATCH] Added Delete handler to UploadHandlers. --- internal/handlers/handlers.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index e3b9c9e..a307cf7 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -180,10 +180,35 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) { http.ServeContent(w, r, path, stat.ModTime(), fd) } +func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) { + saltedHash := server.GetURLParam(r, "hash") + + path := path.Join(h.conf.Uploads.Directory, saltedHash) + + _, err := os.Stat(path) + if os.IsNotExist(err) { + h.NotFound(w, r) + return + } + + err = os.Remove(path) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprint(w, err) + return + } + + netTyp, _ := utils.NetworkType(r.Host) + + h.logDelete.Printf("| %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, saltedHash, r.UserAgent()) + + w.WriteHeader(http.StatusNoContent) +} + func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) if !strings.Contains(r.UserAgent(), "curl") { - web.Error404(utils.MainSite(r.Host), w) + web.Error404(utils.MainSite(r.Host), w) } else { fmt.Fprintln(w, "deleted") }