refactor chunked assembly stream

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2017-06-22 18:36:34 +02:00 committed by Roeland Jago Douma
parent 86d2fe516e
commit 89b747d066
No known key found for this signature in database
GPG Key ID: F941078878347C0C
1 changed files with 32 additions and 56 deletions

View File

@ -20,6 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
* *
*/ */
namespace OCA\DAV\Upload; namespace OCA\DAV\Upload;
use Sabre\DAV\IFile; use Sabre\DAV\IFile;
@ -44,15 +45,15 @@ class AssemblyStream implements \Icewind\Streams\File {
/** @var int */ /** @var int */
private $pos = 0; private $pos = 0;
/** @var array */
private $sortedNodes;
/** @var int */ /** @var int */
private $size; private $size = 0;
/** @var resource */ /** @var resource */
private $currentStream = null; private $currentStream = null;
/** @var int */
private $currentNode = 0;
/** /**
* @param string $path * @param string $path
* @param string $mode * @param string $mode
@ -63,24 +64,18 @@ class AssemblyStream implements \Icewind\Streams\File {
public function stream_open($path, $mode, $options, &$opened_path) { public function stream_open($path, $mode, $options, &$opened_path) {
$this->loadContext('assembly'); $this->loadContext('assembly');
// sort the nodes
$nodes = $this->nodes; $nodes = $this->nodes;
// http://stackoverflow.com/a/10985500 // http://stackoverflow.com/a/10985500
@usort($nodes, function(IFile $a, IFile $b) { @usort($nodes, function (IFile $a, IFile $b) {
return strnatcmp($a->getName(), $b->getName()); return strnatcmp($a->getName(), $b->getName());
}); });
$this->nodes = $nodes; $this->nodes = array_values($nodes);
if (count($this->nodes) > 0) {
// build additional information $this->currentStream = $this->getStream($this->nodes[0]);
$this->sortedNodes = [];
$start = 0;
foreach($this->nodes as $node) {
$size = $node->getSize();
$name = $node->getName();
$this->sortedNodes[$name] = ['node' => $node, 'start' => $start, 'end' => $start + $size];
$start += $size;
$this->size = $start;
} }
$this->size = array_reduce($this->nodes, function ($size, IFile $file) {
return $size + $file->getSize();
}, 0);
return true; return true;
} }
@ -105,36 +100,27 @@ class AssemblyStream implements \Icewind\Streams\File {
* @return string * @return string
*/ */
public function stream_read($count) { public function stream_read($count) {
do { if (is_null($this->currentStream)) {
if ($this->currentStream === null) {
list($node, $posInNode) = $this->getNodeForPosition($this->pos);
if (is_null($node)) {
// reached last node, no more data
return ''; return '';
} }
$this->currentStream = $this->getStream($node);
fseek($this->currentStream, $posInNode);
}
do {
$data = fread($this->currentStream, $count); $data = fread($this->currentStream, $count);
// isset is faster than strlen
if (isset($data[$count - 1])) {
// we read the full count
$read = $count;
} else {
// reaching end of stream, which happens less often so strlen is ok
$read = strlen($data); $read = strlen($data);
}
if (feof($this->currentStream)) { if (feof($this->currentStream)) {
fclose($this->currentStream); fclose($this->currentStream);
$this->currentNode = null; $this->currentNode++;
if ($this->currentNode < count($this->nodes)) {
$this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
} else {
$this->currentStream = null; $this->currentStream = null;
} }
}
// if no data read, try again with the next node because // if no data read, try again with the next node because
// returning empty data can make the caller think there is no more // returning empty data can make the caller think there is no more
// data left to read // data left to read
} while ($read === 0); } while ($read === 0 && !is_null($this->currentStream));
// update position // update position
$this->pos += $read; $this->pos += $read;
@ -235,9 +221,10 @@ class AssemblyStream implements \Icewind\Streams\File {
public static function wrap(array $nodes) { public static function wrap(array $nodes) {
$context = stream_context_create([ $context = stream_context_create([
'assembly' => [ 'assembly' => [
'nodes' => $nodes] 'nodes' => $nodes
]
]); ]);
stream_wrapper_register('assembly', '\OCA\DAV\Upload\AssemblyStream'); stream_wrapper_register('assembly', self::class);
try { try {
$wrapped = fopen('assembly://', 'r', null, $context); $wrapped = fopen('assembly://', 'r', null, $context);
} catch (\BadMethodCallException $e) { } catch (\BadMethodCallException $e) {
@ -248,19 +235,6 @@ class AssemblyStream implements \Icewind\Streams\File {
return $wrapped; return $wrapped;
} }
/**
* @param $pos
* @return IFile | null
*/
private function getNodeForPosition($pos) {
foreach($this->sortedNodes as $node) {
if ($pos >= $node['start'] && $pos < $node['end']) {
return [$node['node'], $pos - $node['start']];
}
}
return null;
}
/** /**
* @param IFile $node * @param IFile $node
* @return resource * @return resource
@ -269,9 +243,11 @@ class AssemblyStream implements \Icewind\Streams\File {
$data = $node->get(); $data = $node->get();
if (is_resource($data)) { if (is_resource($data)) {
return $data; return $data;
} else {
$tmp = fopen('php://temp', 'w+');
fwrite($tmp, $data);
rewind($tmp);
return $tmp;
} }
return fopen('data://text/plain,' . $data,'r');
} }
} }