AssemblyStream.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Upload;
  8. use Sabre\DAV\IFile;
  9. /**
  10. * Class AssemblyStream
  11. *
  12. * The assembly stream is a virtual stream that wraps multiple chunks.
  13. * Reading from the stream transparently accessed the underlying chunks and
  14. * give a representation as if they were already merged together.
  15. *
  16. * @package OCA\DAV\Upload
  17. */
  18. class AssemblyStream implements \Icewind\Streams\File {
  19. /** @var resource */
  20. private $context;
  21. /** @var IFile[] */
  22. private $nodes;
  23. /** @var int */
  24. private $pos = 0;
  25. /** @var int */
  26. private $size = 0;
  27. /** @var resource */
  28. private $currentStream = null;
  29. /** @var int */
  30. private $currentNode = 0;
  31. /** @var int */
  32. private $currentNodeRead = 0;
  33. /**
  34. * @param string $path
  35. * @param string $mode
  36. * @param int $options
  37. * @param string &$opened_path
  38. * @return bool
  39. */
  40. public function stream_open($path, $mode, $options, &$opened_path) {
  41. $this->loadContext('assembly');
  42. $nodes = $this->nodes;
  43. usort($nodes, function (IFile $a, IFile $b) {
  44. return strnatcmp($a->getName(), $b->getName());
  45. });
  46. $this->nodes = array_values($nodes);
  47. $this->size = array_reduce($this->nodes, function ($size, IFile $file) {
  48. return $size + $file->getSize();
  49. }, 0);
  50. return true;
  51. }
  52. /**
  53. * @param int $offset
  54. * @param int $whence
  55. * @return bool
  56. */
  57. public function stream_seek($offset, $whence = SEEK_SET) {
  58. if ($whence === SEEK_CUR) {
  59. $offset = $this->stream_tell() + $offset;
  60. } elseif ($whence === SEEK_END) {
  61. $offset = $this->size + $offset;
  62. }
  63. if ($offset > $this->size) {
  64. return false;
  65. }
  66. $nodeIndex = 0;
  67. $nodeStart = 0;
  68. while (true) {
  69. if (!isset($this->nodes[$nodeIndex + 1])) {
  70. break;
  71. }
  72. $node = $this->nodes[$nodeIndex];
  73. if ($nodeStart + $node->getSize() > $offset) {
  74. break;
  75. }
  76. $nodeIndex++;
  77. $nodeStart += $node->getSize();
  78. }
  79. $stream = $this->getStream($this->nodes[$nodeIndex]);
  80. $nodeOffset = $offset - $nodeStart;
  81. if (fseek($stream, $nodeOffset) === -1) {
  82. return false;
  83. }
  84. $this->currentNode = $nodeIndex;
  85. $this->currentNodeRead = $nodeOffset;
  86. $this->currentStream = $stream;
  87. $this->pos = $offset;
  88. return true;
  89. }
  90. /**
  91. * @return int
  92. */
  93. public function stream_tell() {
  94. return $this->pos;
  95. }
  96. /**
  97. * @param int $count
  98. * @return string
  99. */
  100. public function stream_read($count) {
  101. if (is_null($this->currentStream)) {
  102. if ($this->currentNode < count($this->nodes)) {
  103. $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
  104. } else {
  105. return '';
  106. }
  107. }
  108. do {
  109. $data = fread($this->currentStream, $count);
  110. $read = strlen($data);
  111. $this->currentNodeRead += $read;
  112. if (feof($this->currentStream)) {
  113. fclose($this->currentStream);
  114. $currentNodeSize = $this->nodes[$this->currentNode]->getSize();
  115. if ($this->currentNodeRead < $currentNodeSize) {
  116. throw new \Exception('Stream from assembly node shorter than expected, got ' . $this->currentNodeRead . ' bytes, expected ' . $currentNodeSize);
  117. }
  118. $this->currentNode++;
  119. $this->currentNodeRead = 0;
  120. if ($this->currentNode < count($this->nodes)) {
  121. $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
  122. } else {
  123. $this->currentStream = null;
  124. }
  125. }
  126. // if no data read, try again with the next node because
  127. // returning empty data can make the caller think there is no more
  128. // data left to read
  129. } while ($read === 0 && !is_null($this->currentStream));
  130. // update position
  131. $this->pos += $read;
  132. return $data;
  133. }
  134. /**
  135. * @param string $data
  136. * @return int
  137. */
  138. public function stream_write($data) {
  139. return false;
  140. }
  141. /**
  142. * @param int $option
  143. * @param int $arg1
  144. * @param int $arg2
  145. * @return bool
  146. */
  147. public function stream_set_option($option, $arg1, $arg2) {
  148. return false;
  149. }
  150. /**
  151. * @param int $size
  152. * @return bool
  153. */
  154. public function stream_truncate($size) {
  155. return false;
  156. }
  157. /**
  158. * @return array
  159. */
  160. public function stream_stat() {
  161. return [
  162. 'size' => $this->size,
  163. ];
  164. }
  165. /**
  166. * @param int $operation
  167. * @return bool
  168. */
  169. public function stream_lock($operation) {
  170. return false;
  171. }
  172. /**
  173. * @return bool
  174. */
  175. public function stream_flush() {
  176. return false;
  177. }
  178. /**
  179. * @return bool
  180. */
  181. public function stream_eof() {
  182. return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null);
  183. }
  184. /**
  185. * @return bool
  186. */
  187. public function stream_close() {
  188. return true;
  189. }
  190. /**
  191. * Load the source from the stream context and return the context options
  192. *
  193. * @param string $name
  194. * @return array
  195. * @throws \BadMethodCallException
  196. */
  197. protected function loadContext($name) {
  198. $context = stream_context_get_options($this->context);
  199. if (isset($context[$name])) {
  200. $context = $context[$name];
  201. } else {
  202. throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
  203. }
  204. if (isset($context['nodes']) and is_array($context['nodes'])) {
  205. $this->nodes = $context['nodes'];
  206. } else {
  207. throw new \BadMethodCallException('Invalid context, nodes not set');
  208. }
  209. return $context;
  210. }
  211. /**
  212. * @param IFile[] $nodes
  213. * @return resource
  214. *
  215. * @throws \BadMethodCallException
  216. */
  217. public static function wrap(array $nodes) {
  218. $context = stream_context_create([
  219. 'assembly' => [
  220. 'nodes' => $nodes
  221. ]
  222. ]);
  223. stream_wrapper_register('assembly', self::class);
  224. try {
  225. $wrapped = fopen('assembly://', 'r', false, $context);
  226. } catch (\BadMethodCallException $e) {
  227. stream_wrapper_unregister('assembly');
  228. throw $e;
  229. }
  230. stream_wrapper_unregister('assembly');
  231. return $wrapped;
  232. }
  233. /**
  234. * @param IFile $node
  235. * @return resource
  236. */
  237. private function getStream(IFile $node) {
  238. $data = $node->get();
  239. if (is_resource($data)) {
  240. return $data;
  241. } else {
  242. $tmp = fopen('php://temp', 'w+');
  243. fwrite($tmp, $data);
  244. rewind($tmp);
  245. return $tmp;
  246. }
  247. }
  248. }