2020-01-22 11:45:41 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020, Lukas Stabe (lukas@stabe.de)
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-03-19 16:32:25 +03:00
|
|
|
namespace OC\Files\Stream;
|
|
|
|
|
|
|
|
use Icewind\Streams\File;
|
2020-01-22 11:45:41 +03:00
|
|
|
|
|
|
|
/**
|
2020-03-19 16:32:25 +03:00
|
|
|
* A stream wrapper that uses http range requests to provide a seekable stream for http reading
|
2020-01-22 11:45:41 +03:00
|
|
|
*/
|
2020-03-19 16:32:25 +03:00
|
|
|
class SeekableHttpStream implements File {
|
|
|
|
private const PROTOCOL = 'httpseek';
|
|
|
|
|
2020-01-22 11:45:41 +03:00
|
|
|
private static $registered = false;
|
|
|
|
|
|
|
|
/**
|
2020-03-19 16:32:25 +03:00
|
|
|
* Registers the stream wrapper using the `httpseek://` url scheme
|
2020-01-22 11:45:41 +03:00
|
|
|
* $return void
|
|
|
|
*/
|
2020-03-19 16:32:25 +03:00
|
|
|
private static function registerIfNeeded() {
|
2020-01-22 11:45:41 +03:00
|
|
|
if (!self::$registered) {
|
|
|
|
stream_wrapper_register(
|
2020-03-19 16:32:25 +03:00
|
|
|
self::PROTOCOL,
|
|
|
|
self::class
|
2020-01-22 11:45:41 +03:00
|
|
|
);
|
|
|
|
self::$registered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:32:25 +03:00
|
|
|
/**
|
|
|
|
* Open a readonly-seekable http stream
|
|
|
|
*
|
|
|
|
* The provided callback will be called with byte range and should return an http stream for the requested range
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
|
|
|
* @return false|resource
|
|
|
|
*/
|
|
|
|
public static function open(callable $callback) {
|
|
|
|
$context = stream_context_create([
|
|
|
|
SeekableHttpStream::PROTOCOL => [
|
|
|
|
'callback' => $callback
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
SeekableHttpStream::registerIfNeeded();
|
|
|
|
return fopen(SeekableHttpStream::PROTOCOL . '://', 'r', false, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var resource */
|
|
|
|
public $context;
|
2020-01-22 11:45:41 +03:00
|
|
|
|
2020-03-19 16:32:25 +03:00
|
|
|
/** @var callable */
|
|
|
|
private $openCallback;
|
|
|
|
|
|
|
|
/** @var resource */
|
2020-01-22 11:45:41 +03:00
|
|
|
private $current;
|
2020-03-19 16:32:25 +03:00
|
|
|
/** @var int */
|
2020-01-22 11:45:41 +03:00
|
|
|
private $offset = 0;
|
|
|
|
|
2020-03-19 17:28:02 +03:00
|
|
|
private function reconnect(int $start) {
|
|
|
|
$range = $start . '-';
|
2020-01-22 11:45:41 +03:00
|
|
|
if ($this->current != null) {
|
|
|
|
fclose($this->current);
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:32:25 +03:00
|
|
|
$this->current = ($this->openCallback)($range);
|
2020-01-22 11:45:41 +03:00
|
|
|
|
2020-03-19 16:32:25 +03:00
|
|
|
if ($this->current === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-22 11:45:41 +03:00
|
|
|
|
|
|
|
$responseHead = stream_get_meta_data($this->current)['wrapper_data'];
|
2020-03-19 17:28:02 +03:00
|
|
|
$rangeHeaders = array_values(array_filter($responseHead, function ($v) {
|
2020-01-22 11:45:41 +03:00
|
|
|
return preg_match('#^content-range:#i', $v) === 1;
|
2020-03-19 17:28:02 +03:00
|
|
|
}));
|
|
|
|
if (!$rangeHeaders) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$contentRange = $rangeHeaders[0];
|
2020-01-22 11:45:41 +03:00
|
|
|
|
|
|
|
$content = trim(explode(':', $contentRange)[1]);
|
|
|
|
$range = trim(explode(' ', $content)[1]);
|
2020-03-19 17:28:02 +03:00
|
|
|
$begin = intval(explode('-', $range)[0]);
|
|
|
|
|
|
|
|
if ($begin !== $start) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->offset = $begin;
|
2020-01-22 11:45:41 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:48:40 +03:00
|
|
|
public function stream_open($path, $mode, $options, &$opened_path) {
|
2020-03-19 16:32:25 +03:00
|
|
|
$options = stream_context_get_options($this->context)[self::PROTOCOL];
|
|
|
|
$this->openCallback = $options['callback'];
|
2020-01-22 11:45:41 +03:00
|
|
|
|
2020-03-19 17:28:02 +03:00
|
|
|
return $this->reconnect(0);
|
2020-01-22 11:45:41 +03:00
|
|
|
}
|
|
|
|
|
2020-04-01 19:48:40 +03:00
|
|
|
public function stream_read($count) {
|
2020-03-19 16:32:25 +03:00
|
|
|
if (!$this->current) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-22 11:45:41 +03:00
|
|
|
$ret = fread($this->current, $count);
|
|
|
|
$this->offset += strlen($ret);
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:48:40 +03:00
|
|
|
public function stream_seek($offset, $whence = SEEK_SET) {
|
2020-01-22 11:45:41 +03:00
|
|
|
switch ($whence) {
|
2020-03-19 16:32:25 +03:00
|
|
|
case SEEK_SET:
|
|
|
|
if ($offset === $this->offset) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-19 17:28:02 +03:00
|
|
|
return $this->reconnect($offset);
|
2020-03-19 16:32:25 +03:00
|
|
|
case SEEK_CUR:
|
|
|
|
if ($offset === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-19 17:28:02 +03:00
|
|
|
return $this->reconnect($this->offset + $offset);
|
2020-03-19 16:32:25 +03:00
|
|
|
case SEEK_END:
|
|
|
|
return false;
|
2020-01-22 11:45:41 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:48:40 +03:00
|
|
|
public function stream_tell() {
|
2020-01-22 11:45:41 +03:00
|
|
|
return $this->offset;
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:48:40 +03:00
|
|
|
public function stream_stat() {
|
2020-01-22 11:45:41 +03:00
|
|
|
return fstat($this->current);
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:48:40 +03:00
|
|
|
public function stream_eof() {
|
2020-01-22 11:45:41 +03:00
|
|
|
return feof($this->current);
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:48:40 +03:00
|
|
|
public function stream_close() {
|
2020-01-22 11:45:41 +03:00
|
|
|
fclose($this->current);
|
|
|
|
}
|
2020-03-19 16:32:25 +03:00
|
|
|
|
|
|
|
public function stream_write($data) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_set_option($option, $arg1, $arg2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_truncate($size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_lock($operation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_flush() {
|
|
|
|
return; //noop because readonly stream
|
|
|
|
}
|
2020-01-22 11:45:41 +03:00
|
|
|
}
|