api_retention.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "fmt"
  17. "net/http"
  18. "github.com/go-chi/render"
  19. "github.com/drakkan/sftpgo/v2/internal/common"
  20. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  21. )
  22. func getRetentionChecks(w http.ResponseWriter, r *http.Request) {
  23. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  24. render.JSON(w, r, common.RetentionChecks.Get())
  25. }
  26. func startRetentionCheck(w http.ResponseWriter, r *http.Request) {
  27. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  28. username := getURLParam(r, "username")
  29. user, err := dataprovider.GetUserWithGroupSettings(username)
  30. if err != nil {
  31. sendAPIResponse(w, r, err, "", getRespStatus(err))
  32. return
  33. }
  34. var check common.RetentionCheck
  35. err = render.DecodeJSON(r.Body, &check.Folders)
  36. if err != nil {
  37. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  38. return
  39. }
  40. check.Notifications = getCommaSeparatedQueryParam(r, "notifications")
  41. for _, notification := range check.Notifications {
  42. if notification == common.RetentionCheckNotificationEmail {
  43. claims, err := getTokenClaims(r)
  44. if err != nil {
  45. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  46. return
  47. }
  48. admin, err := dataprovider.AdminExists(claims.Username)
  49. if err != nil {
  50. sendAPIResponse(w, r, err, "", getRespStatus(err))
  51. return
  52. }
  53. check.Email = admin.Email
  54. }
  55. }
  56. if err := check.Validate(); err != nil {
  57. sendAPIResponse(w, r, err, "Invalid retention check", http.StatusBadRequest)
  58. return
  59. }
  60. c := common.RetentionChecks.Add(check, &user)
  61. if c == nil {
  62. sendAPIResponse(w, r, err, fmt.Sprintf("Another check is already in progress for user %#v", username),
  63. http.StatusConflict)
  64. return
  65. }
  66. go c.Start() //nolint:errcheck
  67. sendAPIResponse(w, r, err, "Check started", http.StatusAccepted)
  68. }