AssemblyStream.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\Upload;
  24. use Sabre\DAV\IFile;
  25. /**
  26. * Class AssemblyStream
  27. *
  28. * The assembly stream is a virtual stream that wraps multiple chunks.
  29. * Reading from the stream transparently accessed the underlying chunks and
  30. * give a representation as if they were already merged together.
  31. *
  32. * @package OCA\DAV\Upload
  33. */
  34. class AssemblyStream implements \Icewind\Streams\File {
  35. /** @var resource */
  36. private $context;
  37. /** @var IFile[] */
  38. private $nodes;
  39. /** @var int */
  40. private $pos = 0;
  41. /** @var array */
  42. private $sortedNodes;
  43. /** @var int */
  44. private $size;
  45. /** @var resource */
  46. private $currentStream = null;
  47. /**
  48. * @param string $path
  49. * @param string $mode
  50. * @param int $options
  51. * @param string &$opened_path
  52. * @return bool
  53. */
  54. public function stream_open($path, $mode, $options, &$opened_path) {
  55. $this->loadContext('assembly');
  56. // sort the nodes
  57. $nodes = $this->nodes;
  58. // http://stackoverflow.com/a/10985500
  59. @usort($nodes, function(IFile $a, IFile $b) {
  60. return strcmp($a->getName(), $b->getName());
  61. });
  62. $this->nodes = $nodes;
  63. // build additional information
  64. $this->sortedNodes = [];
  65. $start = 0;
  66. foreach($this->nodes as $node) {
  67. $size = $node->getSize();
  68. $name = $node->getName();
  69. $this->sortedNodes[$name] = ['node' => $node, 'start' => $start, 'end' => $start + $size];
  70. $start += $size;
  71. $this->size = $start;
  72. }
  73. return true;
  74. }
  75. /**
  76. * @param string $offset
  77. * @param int $whence
  78. * @return bool
  79. */
  80. public function stream_seek($offset, $whence = SEEK_SET) {
  81. return false;
  82. }
  83. /**
  84. * @return int
  85. */
  86. public function stream_tell() {
  87. return $this->pos;
  88. }
  89. /**
  90. * @param int $count
  91. * @return string
  92. */
  93. public function stream_read($count) {
  94. do {
  95. if ($this->currentStream === null) {
  96. list($node, $posInNode) = $this->getNodeForPosition($this->pos);
  97. if (is_null($node)) {
  98. // reached last node, no more data
  99. return '';
  100. }
  101. $this->currentStream = $this->getStream($node);
  102. fseek($this->currentStream, $posInNode);
  103. }
  104. $data = fread($this->currentStream, $count);
  105. // isset is faster than strlen
  106. if (isset($data[$count - 1])) {
  107. // we read the full count
  108. $read = $count;
  109. } else {
  110. // reaching end of stream, which happens less often so strlen is ok
  111. $read = strlen($data);
  112. }
  113. if (feof($this->currentStream)) {
  114. fclose($this->currentStream);
  115. $this->currentNode = null;
  116. $this->currentStream = null;
  117. }
  118. // if no data read, try again with the next node because
  119. // returning empty data can make the caller think there is no more
  120. // data left to read
  121. } while ($read === 0);
  122. // update position
  123. $this->pos += $read;
  124. return $data;
  125. }
  126. /**
  127. * @param string $data
  128. * @return int
  129. */
  130. public function stream_write($data) {
  131. return false;
  132. }
  133. /**
  134. * @param int $option
  135. * @param int $arg1
  136. * @param int $arg2
  137. * @return bool
  138. */
  139. public function stream_set_option($option, $arg1, $arg2) {
  140. return false;
  141. }
  142. /**
  143. * @param int $size
  144. * @return bool
  145. */
  146. public function stream_truncate($size) {
  147. return false;
  148. }
  149. /**
  150. * @return array
  151. */
  152. public function stream_stat() {
  153. return [];
  154. }
  155. /**
  156. * @param int $operation
  157. * @return bool
  158. */
  159. public function stream_lock($operation) {
  160. return false;
  161. }
  162. /**
  163. * @return bool
  164. */
  165. public function stream_flush() {
  166. return false;
  167. }
  168. /**
  169. * @return bool
  170. */
  171. public function stream_eof() {
  172. return $this->pos >= $this->size;
  173. }
  174. /**
  175. * @return bool
  176. */
  177. public function stream_close() {
  178. return true;
  179. }
  180. /**
  181. * Load the source from the stream context and return the context options
  182. *
  183. * @param string $name
  184. * @return array
  185. * @throws \Exception
  186. */
  187. protected function loadContext($name) {
  188. $context = stream_context_get_options($this->context);
  189. if (isset($context[$name])) {
  190. $context = $context[$name];
  191. } else {
  192. throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
  193. }
  194. if (isset($context['nodes']) and is_array($context['nodes'])) {
  195. $this->nodes = $context['nodes'];
  196. } else {
  197. throw new \BadMethodCallException('Invalid context, nodes not set');
  198. }
  199. return $context;
  200. }
  201. /**
  202. * @param IFile[] $nodes
  203. * @return resource
  204. *
  205. * @throws \BadMethodCallException
  206. */
  207. public static function wrap(array $nodes) {
  208. $context = stream_context_create([
  209. 'assembly' => [
  210. 'nodes' => $nodes]
  211. ]);
  212. stream_wrapper_register('assembly', '\OCA\DAV\Upload\AssemblyStream');
  213. try {
  214. $wrapped = fopen('assembly://', 'r', null, $context);
  215. } catch (\BadMethodCallException $e) {
  216. stream_wrapper_unregister('assembly');
  217. throw $e;
  218. }
  219. stream_wrapper_unregister('assembly');
  220. return $wrapped;
  221. }
  222. /**
  223. * @param $pos
  224. * @return IFile | null
  225. */
  226. private function getNodeForPosition($pos) {
  227. foreach($this->sortedNodes as $node) {
  228. if ($pos >= $node['start'] && $pos < $node['end']) {
  229. return [$node['node'], $pos - $node['start']];
  230. }
  231. }
  232. return null;
  233. }
  234. /**
  235. * @param IFile $node
  236. * @return resource
  237. */
  238. private function getStream(IFile $node) {
  239. $data = $node->get();
  240. if (is_resource($data)) {
  241. return $data;
  242. }
  243. return fopen('data://text/plain,' . $data,'r');
  244. }
  245. }