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") }