1
0

AssemblyStream.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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->pos) {
  64. return true;
  65. }
  66. if ($offset > $this->size) {
  67. return false;
  68. }
  69. $nodeIndex = 0;
  70. $nodeStart = 0;
  71. while (true) {
  72. if (!isset($this->nodes[$nodeIndex + 1])) {
  73. break;
  74. }
  75. $node = $this->nodes[$nodeIndex];
  76. if ($nodeStart + $node->getSize() > $offset) {
  77. break;
  78. }
  79. $nodeIndex++;
  80. $nodeStart += $node->getSize();
  81. }
  82. $stream = $this->getStream($this->nodes[$nodeIndex]);
  83. $nodeOffset = $offset - $nodeStart;
  84. if ($nodeOffset > 0 && fseek($stream, $nodeOffset) === -1) {
  85. return false;
  86. }
  87. $this->currentNode = $nodeIndex;
  88. $this->currentNodeRead = $nodeOffset;
  89. $this->currentStream = $stream;
  90. $this->pos = $offset;
  91. return true;
  92. }
  93. /**
  94. * @return int
  95. */
  96. public function stream_tell() {
  97. return $this->pos;
  98. }
  99. /**
  100. * @param int $count
  101. * @return string
  102. */
  103. public function stream_read($count) {
  104. if (is_null($this->currentStream)) {
  105. if ($this->currentNode < count($this->nodes)) {
  106. $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
  107. } else {
  108. return '';
  109. }
  110. }
  111. $collectedData = '';
  112. // read data until we either got all the data requested or there is no more stream left
  113. while ($count > 0 && !is_null($this->currentStream)) {
  114. $data = fread($this->currentStream, $count);
  115. $read = strlen($data);
  116. $count -= $read;
  117. $collectedData .= $data;
  118. $this->currentNodeRead += $read;
  119. if (feof($this->currentStream)) {
  120. fclose($this->currentStream);
  121. $currentNodeSize = $this->nodes[$this->currentNode]->getSize();
  122. if ($this->currentNodeRead < $currentNodeSize) {
  123. throw new \Exception('Stream from assembly node shorter than expected, got ' . $this->currentNodeRead . ' bytes, expected ' . $currentNodeSize);
  124. }
  125. $this->currentNode++;
  126. $this->currentNodeRead = 0;
  127. if ($this->currentNode < count($this->nodes)) {
  128. $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
  129. } else {
  130. $this->currentStream = null;
  131. }
  132. }
  133. }
  134. // update position
  135. $this->pos += strlen($collectedData);
  136. return $collectedData;
  137. }
  138. /**
  139. * @param string $data
  140. * @return int
  141. */
  142. public function stream_write($data) {
  143. return false;
  144. }
  145. /**
  146. * @param int $option
  147. * @param int $arg1
  148. * @param int $arg2
  149. * @return bool
  150. */
  151. public function stream_set_option($option, $arg1, $arg2) {
  152. return false;
  153. }
  154. /**
  155. * @param int $size
  156. * @return bool
  157. */
  158. public function stream_truncate($size) {
  159. return false;
  160. }
  161. /**
  162. * @return array
  163. */
  164. public function stream_stat() {
  165. return [
  166. 'size' => $this->size,
  167. ];
  168. }
  169. /**
  170. * @param int $operation
  171. * @return bool
  172. */
  173. public function stream_lock($operation) {
  174. return false;
  175. }
  176. /**
  177. * @return bool
  178. */
  179. public function stream_flush() {
  180. return false;
  181. }
  182. /**
  183. * @return bool
  184. */
  185. public function stream_eof() {
  186. return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null);
  187. }
  188. /**
  189. * @return bool
  190. */
  191. public function stream_close() {
  192. return true;
  193. }
  194. /**
  195. * Load the source from the stream context and return the context options
  196. *
  197. * @param string $name
  198. * @return array
  199. * @throws \BadMethodCallException
  200. */
  201. protected function loadContext($name) {
  202. $context = stream_context_get_options($this->context);
  203. if (isset($context[$name])) {
  204. $context = $context[$name];
  205. } else {
  206. throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
  207. }
  208. if (isset($context['nodes']) and is_array($context['nodes'])) {
  209. $this->nodes = $context['nodes'];
  210. } else {
  211. throw new \BadMethodCallException('Invalid context, nodes not set');
  212. }
  213. return $context;
  214. }
  215. /**
  216. * @param IFile[] $nodes
  217. * @return resource
  218. *
  219. * @throws \BadMethodCallException
  220. */
  221. public static function wrap(array $nodes) {
  222. $context = stream_context_create([
  223. 'assembly' => [
  224. 'nodes' => $nodes
  225. ]
  226. ]);
  227. stream_wrapper_register('assembly', self::class);
  228. try {
  229. $wrapped = fopen('assembly://', 'r', false, $context);
  230. } catch (\BadMethodCallException $e) {
  231. stream_wrapper_unregister('assembly');
  232. throw $e;
  233. }
  234. stream_wrapper_unregister('assembly');
  235. return $wrapped;
  236. }
  237. /**
  238. * @param IFile $node
  239. * @return resource
  240. */
  241. private function getStream(IFile $node) {
  242. $data = $node->get();
  243. if (is_resource($data)) {
  244. return $data;
  245. } else {
  246. $tmp = fopen('php://temp', 'w+');
  247. fwrite($tmp, $data);
  248. rewind($tmp);
  249. return $tmp;
  250. }
  251. }
  252. }