Skip to content

Commit

Permalink
Support seeking also from the end of file on S3 storage
Browse files Browse the repository at this point in the history
The PR nextcloud#20033 added support
for `fseek` for  the S3 storage backend. However, the seek mode SEEK_END
was left out that time. This PR fills this gap.

Signed-off-by: Pauli Järvinen <pauli.jarvinen@gmail.com>
  • Loading branch information
paulijar committed Sep 11, 2021
1 parent bc53501 commit a335592
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/private/Files/Stream/SeekableHttpStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public static function open(callable $callback) {
private $current;
/** @var int */
private $offset = 0;
/** @var int */
private $length = 0;

private function reconnect(int $start) {
$range = $start . '-';
Expand All @@ -101,12 +103,14 @@ private function reconnect(int $start) {
$content = trim(explode(':', $contentRange)[1]);
$range = trim(explode(' ', $content)[1]);
$begin = intval(explode('-', $range)[0]);
$length = intval(explode('/', $range)[1]);

if ($begin !== $start) {
return false;
}

$this->offset = $begin;
$this->length = $length;

return true;
}
Expand Down Expand Up @@ -140,7 +144,12 @@ public function stream_seek($offset, $whence = SEEK_SET) {
}
return $this->reconnect($this->offset + $offset);
case SEEK_END:
return false;
if ($this->length === 0) {
return false;
} elseif ($this->length + $offset === $this->offset) {
return true;
}
return $this->reconnect($this->length + $offset);
}
return false;
}
Expand Down

0 comments on commit a335592

Please sign in to comment.