create_collection.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2021 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. import os
  20. from tempfile import TemporaryDirectory
  21. from typing import Iterable, Optional, cast
  22. import radicale.item as radicale_item
  23. from radicale import pathutils
  24. from radicale.log import logger
  25. from radicale.storage import multifilesystem
  26. from radicale.storage.multifilesystem.base import StorageBase
  27. class StoragePartCreateCollection(StorageBase):
  28. def create_collection(self, href: str,
  29. items: Optional[Iterable[radicale_item.Item]] = None,
  30. props=None) -> "multifilesystem.Collection":
  31. folder = self._get_collection_root_folder()
  32. # Path should already be sanitized
  33. sane_path = pathutils.strip_path(href)
  34. filesystem_path = pathutils.path_to_filesystem(folder, sane_path)
  35. logger.debug("Create collection: %r" % filesystem_path)
  36. if not props:
  37. self._makedirs_synced(filesystem_path)
  38. return self._collection_class(
  39. cast(multifilesystem.Storage, self),
  40. pathutils.unstrip_path(sane_path, True))
  41. parent_dir = os.path.dirname(filesystem_path)
  42. self._makedirs_synced(parent_dir)
  43. # Create a temporary directory with an unsafe name
  44. try:
  45. with TemporaryDirectory(prefix=".Radicale.tmp-", dir=parent_dir
  46. ) as tmp_dir:
  47. # The temporary directory itself can't be renamed
  48. tmp_filesystem_path = os.path.join(tmp_dir, "collection")
  49. os.makedirs(tmp_filesystem_path)
  50. col = self._collection_class(
  51. cast(multifilesystem.Storage, self),
  52. pathutils.unstrip_path(sane_path, True),
  53. filesystem_path=tmp_filesystem_path)
  54. col.set_meta(props)
  55. if items is not None:
  56. if props.get("tag") == "VCALENDAR":
  57. col._upload_all_nonatomic(items, suffix=".ics")
  58. elif props.get("tag") == "VADDRESSBOOK":
  59. col._upload_all_nonatomic(items, suffix=".vcf")
  60. if os.path.lexists(filesystem_path):
  61. pathutils.rename_exchange(tmp_filesystem_path, filesystem_path)
  62. else:
  63. os.rename(tmp_filesystem_path, filesystem_path)
  64. self._sync_directory(parent_dir)
  65. except Exception as e:
  66. raise ValueError("Failed to create collection %r as %r %s" %
  67. (href, filesystem_path, e)) from e
  68. return self._collection_class(
  69. cast(multifilesystem.Storage, self),
  70. pathutils.unstrip_path(sane_path, True))