/** @var int */
private $offset = 0;
- private function reconnect($range) {
+ private function reconnect(int $start) {
+ $range = $start . '-';
if ($this->current != null) {
fclose($this->current);
}
}
$responseHead = stream_get_meta_data($this->current)['wrapper_data'];
- $contentRange = array_values(array_filter($responseHead, function ($v) {
+ $rangeHeaders = array_values(array_filter($responseHead, function ($v) {
return preg_match('#^content-range:#i', $v) === 1;
- }))[0];
+ }));
+ if (!$rangeHeaders) {
+ return false;
+ }
+ $contentRange = $rangeHeaders[0];
$content = trim(explode(':', $contentRange)[1]);
$range = trim(explode(' ', $content)[1]);
- $begin = explode('-', $range)[0];
- $this->offset = intval($begin);
+ $begin = intval(explode('-', $range)[0]);
+
+ if ($begin !== $start) {
+ return false;
+ }
+
+ $this->offset = $begin;
return true;
}
$options = stream_context_get_options($this->context)[self::PROTOCOL];
$this->openCallback = $options['callback'];
- return $this->reconnect('0-');
+ return $this->reconnect(0);
}
function stream_read($count) {
if ($offset === $this->offset) {
return true;
}
- return $this->reconnect($offset . '-');
+ return $this->reconnect($offset);
case SEEK_CUR:
if ($offset === 0) {
return true;
}
- return $this->reconnect(($this->offset + $offset) . '-');
+ return $this->reconnect($this->offset + $offset);
case SEEK_END:
return false;
}
class MultiPartUploadS3 extends S3 {
function writeObject($urn, $stream) {
$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
- 'mup_threshold' => 1
+ 'mup_threshold' => 1,
]);
}
}
$this->assertEquals(file_get_contents(__FILE__), stream_get_contents($result));
}
+
+ public function testSeek() {
+ $data = file_get_contents(__FILE__);
+
+ $instance = $this->getInstance();
+ $instance->writeObject('seek', $this->stringToStream($data));
+
+ $read = $instance->readObject('seek');
+ $this->assertEquals(substr($data, 0, 100), fread($read, 100));
+
+ fseek($read, 10);
+ $this->assertEquals(substr($data, 10, 100), fread($read, 100));
+
+ fseek($read, 100, SEEK_CUR);
+ $this->assertEquals(substr($data, 210, 100), fread($read, 100));
+ }
}