api_retention.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package httpd
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/go-chi/render"
  6. "github.com/drakkan/sftpgo/v2/common"
  7. "github.com/drakkan/sftpgo/v2/dataprovider"
  8. )
  9. func getRetentionChecks(w http.ResponseWriter, r *http.Request) {
  10. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  11. render.JSON(w, r, common.RetentionChecks.Get())
  12. }
  13. func startRetentionCheck(w http.ResponseWriter, r *http.Request) {
  14. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  15. username := getURLParam(r, "username")
  16. user, err := dataprovider.GetUserWithGroupSettings(username)
  17. if err != nil {
  18. sendAPIResponse(w, r, err, "", getRespStatus(err))
  19. return
  20. }
  21. var check common.RetentionCheck
  22. err = render.DecodeJSON(r.Body, &check.Folders)
  23. if err != nil {
  24. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  25. return
  26. }
  27. check.Notifications = getCommaSeparatedQueryParam(r, "notifications")
  28. for _, notification := range check.Notifications {
  29. if notification == common.RetentionCheckNotificationEmail {
  30. claims, err := getTokenClaims(r)
  31. if err != nil {
  32. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  33. return
  34. }
  35. admin, err := dataprovider.AdminExists(claims.Username)
  36. if err != nil {
  37. sendAPIResponse(w, r, err, "", getRespStatus(err))
  38. return
  39. }
  40. check.Email = admin.Email
  41. }
  42. }
  43. if err := check.Validate(); err != nil {
  44. sendAPIResponse(w, r, err, "Invalid retention check", http.StatusBadRequest)
  45. return
  46. }
  47. c := common.RetentionChecks.Add(check, &user)
  48. if c == nil {
  49. sendAPIResponse(w, r, err, fmt.Sprintf("Another check is already in progress for user %#v", username),
  50. http.StatusConflict)
  51. return
  52. }
  53. go c.Start()
  54. sendAPIResponse(w, r, err, "Check started", http.StatusAccepted)
  55. }