File.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\FSExt;
  4. use Sabre\DAV;
  5. use Sabre\DAV\FS\Node;
  6. /**
  7. * File class.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. class File extends Node implements DAV\PartialUpdate\IPatchSupport
  14. {
  15. /**
  16. * Updates the data.
  17. *
  18. * Data is a readable stream resource.
  19. *
  20. * @param resource|string $data
  21. *
  22. * @return string
  23. */
  24. public function put($data)
  25. {
  26. file_put_contents($this->path, $data);
  27. clearstatcache(true, $this->path);
  28. return $this->getETag();
  29. }
  30. /**
  31. * Updates the file based on a range specification.
  32. *
  33. * The first argument is the data, which is either a readable stream
  34. * resource or a string.
  35. *
  36. * The second argument is the type of update we're doing.
  37. * This is either:
  38. * * 1. append (default)
  39. * * 2. update based on a start byte
  40. * * 3. update based on an end byte
  41. *;
  42. * The third argument is the start or end byte.
  43. *
  44. * After a successful put operation, you may choose to return an ETag. The
  45. * ETAG must always be surrounded by double-quotes. These quotes must
  46. * appear in the actual string you're returning.
  47. *
  48. * Clients may use the ETag from a PUT request to later on make sure that
  49. * when they update the file, the contents haven't changed in the mean
  50. * time.
  51. *
  52. * @param resource|string $data
  53. * @param int $rangeType
  54. * @param int $offset
  55. *
  56. * @return string|null
  57. */
  58. public function patch($data, $rangeType, $offset = null)
  59. {
  60. switch ($rangeType) {
  61. case 1:
  62. $f = fopen($this->path, 'a');
  63. break;
  64. case 2:
  65. $f = fopen($this->path, 'c');
  66. fseek($f, $offset);
  67. break;
  68. case 3:
  69. $f = fopen($this->path, 'c');
  70. fseek($f, $offset, SEEK_END);
  71. break;
  72. default:
  73. $f = fopen($this->path, 'a');
  74. break;
  75. }
  76. if (is_string($data)) {
  77. fwrite($f, $data);
  78. } else {
  79. stream_copy_to_stream($data, $f);
  80. }
  81. fclose($f);
  82. clearstatcache(true, $this->path);
  83. return $this->getETag();
  84. }
  85. /**
  86. * Returns the data.
  87. *
  88. * @return resource
  89. */
  90. public function get()
  91. {
  92. return fopen($this->path, 'r');
  93. }
  94. /**
  95. * Delete the current file.
  96. *
  97. * @return bool
  98. */
  99. public function delete()
  100. {
  101. return unlink($this->path);
  102. }
  103. /**
  104. * Returns the ETag for a file.
  105. *
  106. * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
  107. * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
  108. *
  109. * Return null if the ETag can not effectively be determined
  110. *
  111. * @return string|null
  112. */
  113. public function getETag()
  114. {
  115. return '"'.sha1(
  116. fileinode($this->path).
  117. filesize($this->path).
  118. filemtime($this->path)
  119. ).'"';
  120. }
  121. /**
  122. * Returns the mime-type for a file.
  123. *
  124. * If null is returned, we'll assume application/octet-stream
  125. *
  126. * @return string|null
  127. */
  128. public function getContentType()
  129. {
  130. return null;
  131. }
  132. /**
  133. * Returns the size of the file, in bytes.
  134. *
  135. * @return int
  136. */
  137. public function getSize()
  138. {
  139. return filesize($this->path);
  140. }
  141. }