Skip to content

Commit 693754f

Browse files
committed
make assemblystream seekable
Signed-off-by: Robin Appelman <[email protected]>
1 parent aff13a2 commit 693754f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

apps/dav/lib/Upload/AssemblyStream.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,47 @@ public function stream_open($path, $mode, $options, &$opened_path) {
8383
}
8484

8585
/**
86-
* @param string $offset
86+
* @param int $offset
8787
* @param int $whence
8888
* @return bool
8989
*/
9090
public function stream_seek($offset, $whence = SEEK_SET) {
91-
return false;
91+
if ($whence === SEEK_CUR) {
92+
$offset = $this->stream_tell() + $offset;
93+
} else if ($whence === SEEK_END) {
94+
$offset = $this->size + $offset;
95+
}
96+
97+
if ($offset > $this->size) {
98+
throw new \Exception('invalid offset');
99+
return false;
100+
}
101+
102+
$nodeIndex = 0;
103+
$nodeStart = 0;
104+
while (true) {
105+
if (!isset($this->nodes[$nodeIndex + 1])) {
106+
break;
107+
}
108+
$node = $this->nodes[$nodeIndex];
109+
if ($nodeStart + $node->getSize() > $offset) {
110+
break;
111+
}
112+
$nodeIndex++;
113+
$nodeStart += $node->getSize();
114+
}
115+
116+
$stream = $this->getStream($this->nodes[$nodeIndex]);
117+
$nodeOffset = $offset - $nodeStart;
118+
if(fseek($stream, $nodeOffset) === -1) {
119+
return false;
120+
}
121+
$this->currentNode = $nodeIndex;
122+
$this->currentNodeRead = $nodeOffset;
123+
$this->currentStream = $stream;
124+
$this->pos = $offset;
125+
126+
return true;
92127
}
93128

94129
/**

apps/dav/tests/unit/Upload/AssemblyStreamTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ public function testGetContentsFread($expected, $nodes) {
5454
$this->assertEquals($expected, $content);
5555
}
5656

57+
/**
58+
* @dataProvider providesNodes()
59+
*/
60+
public function testSeek($expected, $nodes) {
61+
$stream = \OCA\DAV\Upload\AssemblyStream::wrap($nodes);
62+
63+
$offset = floor(strlen($expected) * 0.6);
64+
if(fseek($stream, $offset) === -1) {
65+
$this->fail('fseek failed');
66+
}
67+
68+
$content = stream_get_contents($stream);
69+
$this->assertEquals(substr($expected, $offset), $content);
70+
}
71+
5772
function providesNodes() {
5873
$data8k = $this->makeData(8192);
5974
$dataLess8k = $this->makeData(8191);

0 commit comments

Comments
 (0)