create_collection.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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-2024 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. with TemporaryDirectory(prefix=".Radicale.tmp-", dir=parent_dir
  45. ) as tmp_dir:
  46. # The temporary directory itself can't be renamed
  47. tmp_filesystem_path = os.path.join(tmp_dir, "collection")
  48. os.makedirs(tmp_filesystem_path)
  49. col = self._collection_class(
  50. cast(multifilesystem.Storage, self),
  51. pathutils.unstrip_path(sane_path, True),
  52. filesystem_path=tmp_filesystem_path)
  53. col.set_meta(props)
  54. if items is not None:
  55. if props.get("tag") == "VCALENDAR":
  56. col._upload_all_nonatomic(items, suffix=".ics")
  57. elif props.get("tag") == "VADDRESSBOOK":
  58. col._upload_all_nonatomic(items, suffix=".vcf")
  59. if os.path.lexists(filesystem_path):
  60. pathutils.rename_exchange(tmp_filesystem_path, filesystem_path)
  61. else:
  62. os.rename(tmp_filesystem_path, filesystem_path)
  63. self._sync_directory(parent_dir)
  64. return self._collection_class(
  65. cast(multifilesystem.Storage, self),
  66. pathutils.unstrip_path(sane_path, True))