create_collection.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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-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 os
  19. from tempfile import TemporaryDirectory
  20. from typing import Iterable, Optional, cast
  21. import radicale.item as radicale_item
  22. from radicale import pathutils
  23. from radicale.storage import multifilesystem
  24. from radicale.storage.multifilesystem.base import StorageBase
  25. class StoragePartCreateCollection(StorageBase):
  26. def create_collection(self, href: str,
  27. items: Optional[Iterable[radicale_item.Item]] = None,
  28. props=None) -> "multifilesystem.Collection":
  29. folder = self._get_collection_root_folder()
  30. # Path should already be sanitized
  31. sane_path = pathutils.strip_path(href)
  32. filesystem_path = pathutils.path_to_filesystem(folder, sane_path)
  33. if not props:
  34. self._makedirs_synced(filesystem_path)
  35. return self._collection_class(
  36. cast(multifilesystem.Storage, self),
  37. pathutils.unstrip_path(sane_path, True))
  38. parent_dir = os.path.dirname(filesystem_path)
  39. self._makedirs_synced(parent_dir)
  40. # Create a temporary directory with an unsafe name
  41. with TemporaryDirectory(prefix=".Radicale.tmp-", dir=parent_dir
  42. ) as tmp_dir:
  43. # The temporary directory itself can't be renamed
  44. tmp_filesystem_path = os.path.join(tmp_dir, "collection")
  45. os.makedirs(tmp_filesystem_path)
  46. col = self._collection_class(
  47. cast(multifilesystem.Storage, self),
  48. pathutils.unstrip_path(sane_path, True),
  49. filesystem_path=tmp_filesystem_path)
  50. col.set_meta(props)
  51. if items is not None:
  52. if props.get("tag") == "VCALENDAR":
  53. col._upload_all_nonatomic(items, suffix=".ics")
  54. elif props.get("tag") == "VADDRESSBOOK":
  55. col._upload_all_nonatomic(items, suffix=".vcf")
  56. if os.path.lexists(filesystem_path):
  57. pathutils.rename_exchange(tmp_filesystem_path, filesystem_path)
  58. else:
  59. os.rename(tmp_filesystem_path, filesystem_path)
  60. self._sync_directory(parent_dir)
  61. return self._collection_class(
  62. cast(multifilesystem.Storage, self),
  63. pathutils.unstrip_path(sane_path, True))