discover.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2018 Unrud<unrud@outlook.com>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. import contextlib
  19. import os
  20. from radicale import pathutils
  21. from radicale.log import logger
  22. import posixpath # isort:skip
  23. class CollectionDiscoverMixin:
  24. @classmethod
  25. def discover(cls, path, depth="0", child_context_manager=(
  26. lambda path, href=None: contextlib.ExitStack())):
  27. # Path should already be sanitized
  28. sane_path = pathutils.strip_path(path)
  29. attributes = sane_path.split("/") if sane_path else []
  30. folder = cls._get_collection_root_folder()
  31. # Create the root collection
  32. cls._makedirs_synced(folder)
  33. try:
  34. filesystem_path = pathutils.path_to_filesystem(folder, sane_path)
  35. except ValueError as e:
  36. # Path is unsafe
  37. logger.debug("Unsafe path %r requested from storage: %s",
  38. sane_path, e, exc_info=True)
  39. return
  40. # Check if the path exists and if it leads to a collection or an item
  41. if not os.path.isdir(filesystem_path):
  42. if attributes and os.path.isfile(filesystem_path):
  43. href = attributes.pop()
  44. else:
  45. return
  46. else:
  47. href = None
  48. sane_path = "/".join(attributes)
  49. collection = cls(pathutils.unstrip_path(sane_path, True))
  50. if href:
  51. yield collection._get(href)
  52. return
  53. yield collection
  54. if depth == "0":
  55. return
  56. for href in collection._list():
  57. with child_context_manager(sane_path, href):
  58. yield collection._get(href)
  59. for entry in os.scandir(filesystem_path):
  60. if not entry.is_dir():
  61. continue
  62. href = entry.name
  63. if not pathutils.is_safe_filesystem_path_component(href):
  64. if not href.startswith(".Radicale"):
  65. logger.debug("Skipping collection %r in %r",
  66. href, sane_path)
  67. continue
  68. sane_child_path = posixpath.join(sane_path, href)
  69. child_path = pathutils.unstrip_path(sane_child_path, True)
  70. with child_context_manager(sane_child_path):
  71. yield cls(child_path)