api_metadata.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. "github.com/drakkan/sftpgo/v2/internal/logger"
  22. )
  23. func getMetadataChecks(w http.ResponseWriter, r *http.Request) {
  24. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  25. render.JSON(w, r, common.ActiveMetadataChecks.Get())
  26. }
  27. func startMetadataCheck(w http.ResponseWriter, r *http.Request) {
  28. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  29. user, err := dataprovider.GetUserWithGroupSettings(getURLParam(r, "username"))
  30. if err != nil {
  31. sendAPIResponse(w, r, err, "", getRespStatus(err))
  32. return
  33. }
  34. if !common.ActiveMetadataChecks.Add(user.Username) {
  35. sendAPIResponse(w, r, err, fmt.Sprintf("Another check is already in progress for user %#v", user.Username),
  36. http.StatusConflict)
  37. return
  38. }
  39. go doMetadataCheck(user) //nolint:errcheck
  40. sendAPIResponse(w, r, err, "Check started", http.StatusAccepted)
  41. }
  42. func doMetadataCheck(user dataprovider.User) error {
  43. defer common.ActiveMetadataChecks.Remove(user.Username)
  44. err := user.CheckMetadataConsistency()
  45. if err != nil {
  46. logger.Warn(logSender, "", "error checking metadata for user %#v: %v", user.Username, err)
  47. return err
  48. }
  49. logger.Debug(logSender, "", "metadata check completed for user: %#v", user.Username)
  50. return nil
  51. }