Directory.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\FS;
  4. use Sabre\DAV;
  5. /**
  6. * Directory class.
  7. *
  8. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  9. * @author Evert Pot (http://evertpot.com/)
  10. * @license http://sabre.io/license/ Modified BSD License
  11. */
  12. class Directory extends Node implements DAV\ICollection, DAV\IQuota
  13. {
  14. /**
  15. * Creates a new file in the directory.
  16. *
  17. * Data will either be supplied as a stream resource, or in certain cases
  18. * as a string. Keep in mind that you may have to support either.
  19. *
  20. * After successful creation of the file, you may choose to return the ETag
  21. * of the new file here.
  22. *
  23. * The returned ETag must be surrounded by double-quotes (The quotes should
  24. * be part of the actual string).
  25. *
  26. * If you cannot accurately determine the ETag, you should not return it.
  27. * If you don't store the file exactly as-is (you're transforming it
  28. * somehow) you should also not return an ETag.
  29. *
  30. * This means that if a subsequent GET to this new file does not exactly
  31. * return the same contents of what was submitted here, you are strongly
  32. * recommended to omit the ETag.
  33. *
  34. * @param string $name Name of the file
  35. * @param resource|string $data Initial payload
  36. *
  37. * @return string|null
  38. */
  39. public function createFile($name, $data = null)
  40. {
  41. $newPath = $this->path.'/'.$name;
  42. file_put_contents($newPath, $data);
  43. clearstatcache(true, $newPath);
  44. }
  45. /**
  46. * Creates a new subdirectory.
  47. *
  48. * @param string $name
  49. */
  50. public function createDirectory($name)
  51. {
  52. $newPath = $this->path.'/'.$name;
  53. mkdir($newPath);
  54. clearstatcache(true, $newPath);
  55. }
  56. /**
  57. * Returns a specific child node, referenced by its name.
  58. *
  59. * This method must throw DAV\Exception\NotFound if the node does not
  60. * exist.
  61. *
  62. * @param string $name
  63. *
  64. * @throws DAV\Exception\NotFound
  65. *
  66. * @return DAV\INode
  67. */
  68. public function getChild($name)
  69. {
  70. $path = $this->path.'/'.$name;
  71. if (!file_exists($path)) {
  72. throw new DAV\Exception\NotFound('File with name '.$path.' could not be located');
  73. }
  74. if (is_dir($path)) {
  75. return new self($path);
  76. } else {
  77. return new File($path);
  78. }
  79. }
  80. /**
  81. * Returns an array with all the child nodes.
  82. *
  83. * @return DAV\INode[]
  84. */
  85. public function getChildren()
  86. {
  87. $nodes = [];
  88. $iterator = new \FilesystemIterator(
  89. $this->path,
  90. \FilesystemIterator::CURRENT_AS_SELF
  91. | \FilesystemIterator::SKIP_DOTS
  92. );
  93. foreach ($iterator as $entry) {
  94. $nodes[] = $this->getChild($entry->getFilename());
  95. }
  96. return $nodes;
  97. }
  98. /**
  99. * Checks if a child exists.
  100. *
  101. * @param string $name
  102. *
  103. * @return bool
  104. */
  105. public function childExists($name)
  106. {
  107. $path = $this->path.'/'.$name;
  108. return file_exists($path);
  109. }
  110. /**
  111. * Deletes all files in this directory, and then itself.
  112. */
  113. public function delete()
  114. {
  115. foreach ($this->getChildren() as $child) {
  116. $child->delete();
  117. }
  118. rmdir($this->path);
  119. }
  120. /**
  121. * Returns available diskspace information.
  122. *
  123. * @return array
  124. */
  125. public function getQuotaInfo()
  126. {
  127. $absolute = realpath($this->path);
  128. return [
  129. disk_total_space($absolute) - disk_free_space($absolute),
  130. disk_free_space($absolute),
  131. ];
  132. }
  133. }