Skip to content

Commit

Permalink
fix: improve assembly stream
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Dec 5, 2024
1 parent ca547bb commit e4d0882
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
22 changes: 14 additions & 8 deletions apps/dav/lib/Upload/AssemblyStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function stream_seek($offset, $whence = SEEK_SET) {
$offset = $this->size + $offset;
}

if ($offset === $this->pos) {
return true;
}

if ($offset > $this->size) {
return false;
}
Expand All @@ -95,7 +99,7 @@ public function stream_seek($offset, $whence = SEEK_SET) {

$stream = $this->getStream($this->nodes[$nodeIndex]);
$nodeOffset = $offset - $nodeStart;
if (fseek($stream, $nodeOffset) === -1) {
if ($nodeOffset > 0 && fseek($stream, $nodeOffset) === -1) {
return false;
}
$this->currentNode = $nodeIndex;
Expand Down Expand Up @@ -126,9 +130,14 @@ public function stream_read($count) {
}
}

do {
$collectedData = '';
// read data until we either got all the data requested or there is no more stream left
while ($count > 0 && !is_null($this->currentStream)) {
$data = fread($this->currentStream, $count);
$read = strlen($data);

$count -= $read;
$collectedData .= $data;
$this->currentNodeRead += $read;

if (feof($this->currentStream)) {
Expand All @@ -145,14 +154,11 @@ public function stream_read($count) {
$this->currentStream = null;
}
}
// 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
} while ($read === 0 && !is_null($this->currentStream));
}

// update position
$this->pos += $read;
return $data;
$this->pos += strlen($collectedData);
return $collectedData;
}

/**
Expand Down
22 changes: 19 additions & 3 deletions apps/dav/tests/unit/Upload/AssemblyStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ public function testGetContents($expected, $nodes): void {
/**
* @dataProvider providesNodes()
*/
public function testGetContentsFread($expected, $nodes): void {
public function testGetContentsFread($expected, $nodes, $chunkLength = 3): void {
$stream = AssemblyStream::wrap($nodes);

$content = '';
while (!feof($stream)) {
$content .= fread($stream, 3);
$chunk = fread($stream, $chunkLength);
$content .= $chunk;
if ($chunkLength !== 3) {
$this->assertEquals($chunkLength, strlen($chunk));
}
}

$this->assertEquals($expected, $content);
Expand Down Expand Up @@ -103,7 +107,19 @@ public function providesNodes() {
]],
'a ton of nodes' => [
$tonofdata, $tonofnodes
]
],
'one read over multiple nodes' => [
'1234567890', [
$this->buildNode('0', '1234'),
$this->buildNode('1', '5678'),
$this->buildNode('2', '90'),
], 10],
'two reads over multiple nodes' => [
'1234567890', [
$this->buildNode('0', '1234'),
$this->buildNode('1', '5678'),
$this->buildNode('2', '90'),
], 5],
];
}

Expand Down

0 comments on commit e4d0882

Please sign in to comment.