diff --git a/handlers.go b/handlers.go index 4f76292..10ae25e 100644 --- a/handlers.go +++ b/handlers.go @@ -3,6 +3,7 @@ package main import ( "crypto/rand" "crypto/sha256" + "crypto/subtle" "encoding/hex" "encoding/json" "errors" @@ -271,6 +272,67 @@ func contentHandler(db *gorm.DB, cfg Config) http.HandlerFunc { } } +func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + id := r.PathValue("id") + if !validPasteId(id) { + http.NotFound(w, r) + return + } + var token string + switch r.Method { + case http.MethodDelete: + bearer := r.Header.Get("Authorization") + var scheme string + var found bool + scheme, token, found = strings.Cut(bearer, " ") + if !found || scheme != "Bearer" { + http.Error(w, "invalid token", http.StatusBadRequest) + return + } + case http.MethodPost: + token = r.PostFormValue("delete_token") + } + + path := getStoragePath(cfg.UploadDir, id) + upload, err := getUploadById(db.WithContext(r.Context()), id) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + http.NotFound(w, r) + return + } + log.Println("get upload:", err) + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } + if token == "" || subtle.ConstantTimeCompare([]byte(upload.DeleteToken), []byte(token)) != 1 { + http.Error(w, "invalid token", http.StatusBadRequest) + return + } + + // continue to delete from database IF the file doesnt exist on disk + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { + + log.Println("remove upload file:", err) + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } + + if err := db.WithContext(r.Context()).Delete(upload).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + http.NotFound(w, r) + return + } + + log.Println("remove upload file:", err) + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) + } +} + func detectMimeType(file io.ReadSeeker) (string, error) { if _, err := file.Seek(0, io.SeekStart); err != nil { return "", fmt.Errorf("rewind file: %w", err) diff --git a/main.go b/main.go index 08012c8..a0fbd5b 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,8 @@ func main() { }) mux.HandleFunc("GET /{id}", contentHandler(db, config)) mux.HandleFunc("POST /upload", uploadHandler(db, config)) + mux.HandleFunc("DELETE /{id}", deleteHandler(db, config)) + mux.HandleFunc("POST /{id}/delete", deleteHandler(db, config)) mux.HandleFunc("GET /health", health) log.Println("listening on http://localhost:8080") diff --git a/readme.md b/readme.md index 5cf8835..18b15c2 100644 --- a/readme.md +++ b/readme.md @@ -19,7 +19,7 @@ goals: - [x] content endpoint - [ ] update last accessed at - use a memory cache and batch commit to database to keep performance -- [ ] delete content endpoint +- [x] delete content endpoints - [ ] paste page - [ ] max total storage usage - [ ] normalize mime types (like html)