2015-10-28 16:52:45 +03:00
|
|
|
<?php
|
2016-05-26 20:56:05 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Markus Goetz <markus@woboq.com>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2017-06-22 19:36:34 +03:00
|
|
|
|
2015-10-28 16:52:45 +03:00
|
|
|
namespace OCA\DAV\Upload;
|
|
|
|
|
|
|
|
use Sabre\DAV\IFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AssemblyStream
|
|
|
|
*
|
|
|
|
* The assembly stream is a virtual stream that wraps multiple chunks.
|
|
|
|
* Reading from the stream transparently accessed the underlying chunks and
|
|
|
|
* give a representation as if they were already merged together.
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\Upload
|
|
|
|
*/
|
|
|
|
class AssemblyStream implements \Icewind\Streams\File {
|
|
|
|
|
|
|
|
/** @var resource */
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
/** @var IFile[] */
|
|
|
|
private $nodes;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $pos = 0;
|
|
|
|
|
|
|
|
/** @var int */
|
2017-06-22 19:36:34 +03:00
|
|
|
private $size = 0;
|
2015-10-28 16:52:45 +03:00
|
|
|
|
2016-09-08 12:50:04 +03:00
|
|
|
/** @var resource */
|
|
|
|
private $currentStream = null;
|
|
|
|
|
2017-06-22 19:36:34 +03:00
|
|
|
/** @var int */
|
|
|
|
private $currentNode = 0;
|
|
|
|
|
2018-09-28 16:40:08 +03:00
|
|
|
/** @var int */
|
|
|
|
private $currentNodeRead = 0;
|
|
|
|
|
2015-10-28 16:52:45 +03:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $mode
|
|
|
|
* @param int $options
|
|
|
|
* @param string &$opened_path
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function stream_open($path, $mode, $options, &$opened_path) {
|
|
|
|
$this->loadContext('assembly');
|
|
|
|
|
|
|
|
$nodes = $this->nodes;
|
|
|
|
// http://stackoverflow.com/a/10985500
|
2017-06-22 19:36:34 +03:00
|
|
|
@usort($nodes, function (IFile $a, IFile $b) {
|
2017-01-20 19:46:04 +03:00
|
|
|
return strnatcmp($a->getName(), $b->getName());
|
2015-10-28 16:52:45 +03:00
|
|
|
});
|
2017-06-22 19:36:34 +03:00
|
|
|
$this->nodes = array_values($nodes);
|
|
|
|
$this->size = array_reduce($this->nodes, function ($size, IFile $file) {
|
|
|
|
return $size + $file->getSize();
|
|
|
|
}, 0);
|
2015-10-28 16:52:45 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $offset
|
|
|
|
* @param int $whence
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function stream_seek($offset, $whence = SEEK_SET) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function stream_tell() {
|
|
|
|
return $this->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $count
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function stream_read($count) {
|
2017-06-22 19:36:34 +03:00
|
|
|
if (is_null($this->currentStream)) {
|
2018-10-22 16:54:37 +03:00
|
|
|
if ($this->currentNode < count($this->nodes)) {
|
|
|
|
$this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2017-06-22 19:36:34 +03:00
|
|
|
}
|
2016-09-08 12:50:04 +03:00
|
|
|
|
2017-06-22 19:36:34 +03:00
|
|
|
do {
|
2016-09-08 12:50:04 +03:00
|
|
|
$data = fread($this->currentStream, $count);
|
2017-06-22 19:36:34 +03:00
|
|
|
$read = strlen($data);
|
2018-09-28 16:40:08 +03:00
|
|
|
$this->currentNodeRead += $read;
|
2016-09-08 12:50:04 +03:00
|
|
|
|
|
|
|
if (feof($this->currentStream)) {
|
|
|
|
fclose($this->currentStream);
|
2018-09-28 16:40:08 +03:00
|
|
|
$currentNodeSize = $this->nodes[$this->currentNode]->getSize();
|
|
|
|
if ($this->currentNodeRead < $currentNodeSize) {
|
|
|
|
throw new \Exception('Stream from assembly node shorter than expected, got ' . $this->currentNodeRead . ' bytes, expected ' . $currentNodeSize);
|
|
|
|
}
|
2017-06-22 19:36:34 +03:00
|
|
|
$this->currentNode++;
|
2018-09-28 16:40:08 +03:00
|
|
|
$this->currentNodeRead = 0;
|
2017-06-22 19:36:34 +03:00
|
|
|
if ($this->currentNode < count($this->nodes)) {
|
|
|
|
$this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
|
|
|
|
} else {
|
|
|
|
$this->currentStream = null;
|
|
|
|
}
|
2016-09-08 12:50:04 +03:00
|
|
|
}
|
|
|
|
// if no data read, try again with the next node because
|
|
|
|
// returning empty data can make the caller think there is no more
|
|
|
|
// data left to read
|
2017-06-22 19:36:34 +03:00
|
|
|
} while ($read === 0 && !is_null($this->currentStream));
|
2015-10-28 16:52:45 +03:00
|
|
|
|
|
|
|
// update position
|
|
|
|
$this->pos += $read;
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $data
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function stream_write($data) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $option
|
|
|
|
* @param int $arg1
|
|
|
|
* @param int $arg2
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function stream_set_option($option, $arg1, $arg2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $size
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function stream_truncate($size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function stream_stat() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $operation
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function stream_lock($operation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function stream_flush() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function stream_eof() {
|
2018-10-22 16:54:37 +03:00
|
|
|
return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null);
|
2015-10-28 16:52:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function stream_close() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the source from the stream context and return the context options
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function loadContext($name) {
|
|
|
|
$context = stream_context_get_options($this->context);
|
|
|
|
if (isset($context[$name])) {
|
|
|
|
$context = $context[$name];
|
|
|
|
} else {
|
|
|
|
throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
|
|
|
|
}
|
|
|
|
if (isset($context['nodes']) and is_array($context['nodes'])) {
|
|
|
|
$this->nodes = $context['nodes'];
|
|
|
|
} else {
|
|
|
|
throw new \BadMethodCallException('Invalid context, nodes not set');
|
|
|
|
}
|
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IFile[] $nodes
|
|
|
|
* @return resource
|
|
|
|
*
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
*/
|
|
|
|
public static function wrap(array $nodes) {
|
|
|
|
$context = stream_context_create([
|
|
|
|
'assembly' => [
|
2017-06-22 19:36:34 +03:00
|
|
|
'nodes' => $nodes
|
|
|
|
]
|
2015-10-28 16:52:45 +03:00
|
|
|
]);
|
2017-06-22 19:36:34 +03:00
|
|
|
stream_wrapper_register('assembly', self::class);
|
2015-10-28 16:52:45 +03:00
|
|
|
try {
|
|
|
|
$wrapped = fopen('assembly://', 'r', null, $context);
|
|
|
|
} catch (\BadMethodCallException $e) {
|
|
|
|
stream_wrapper_unregister('assembly');
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
stream_wrapper_unregister('assembly');
|
|
|
|
return $wrapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IFile $node
|
|
|
|
* @return resource
|
|
|
|
*/
|
|
|
|
private function getStream(IFile $node) {
|
|
|
|
$data = $node->get();
|
|
|
|
if (is_resource($data)) {
|
|
|
|
return $data;
|
2017-06-22 19:36:34 +03:00
|
|
|
} else {
|
|
|
|
$tmp = fopen('php://temp', 'w+');
|
|
|
|
fwrite($tmp, $data);
|
|
|
|
rewind($tmp);
|
|
|
|
return $tmp;
|
2015-10-28 16:52:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|