Merge pull request #2934 from nextcloud/quota-stream-non-global
remove the need to register the quota streamwrapper globally
This commit is contained in:
commit
73fcb69cad
|
@ -671,7 +671,6 @@ class OC {
|
||||||
|
|
||||||
// register the stream wrappers
|
// register the stream wrappers
|
||||||
stream_wrapper_register('close', 'OC\Files\Stream\Close');
|
stream_wrapper_register('close', 'OC\Files\Stream\Close');
|
||||||
stream_wrapper_register('quota', 'OC\Files\Stream\Quota');
|
|
||||||
|
|
||||||
\OC::$server->getEventLogger()->start('init_session', 'Initialize session');
|
\OC::$server->getEventLogger()->start('init_session', 'Initialize session');
|
||||||
OC_App::loadApps(array('session'));
|
OC_App::loadApps(array('session'));
|
||||||
|
|
|
@ -25,61 +25,44 @@
|
||||||
|
|
||||||
namespace OC\Files\Stream;
|
namespace OC\Files\Stream;
|
||||||
|
|
||||||
|
use Icewind\Streams\Wrapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stream wrapper limits the amount of data that can be written to a stream
|
* stream wrapper limits the amount of data that can be written to a stream
|
||||||
*
|
*
|
||||||
* usage: void \OC\Files\Stream\Quota::register($id, $stream, $limit)
|
* usage: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
|
||||||
* or: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
|
|
||||||
*/
|
*/
|
||||||
class Quota {
|
class Quota extends Wrapper {
|
||||||
private static $streams = array();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var resource $source
|
|
||||||
*/
|
|
||||||
private $source;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int $limit
|
* @var int $limit
|
||||||
*/
|
*/
|
||||||
private $limit;
|
private $limit;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $id
|
|
||||||
* @param resource $stream
|
|
||||||
* @param int $limit
|
|
||||||
*/
|
|
||||||
public static function register($id, $stream, $limit) {
|
|
||||||
self::$streams[$id] = array($stream, $limit);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* remove all registered streams
|
|
||||||
*/
|
|
||||||
public static function clear() {
|
|
||||||
self::$streams = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param resource $stream
|
* @param resource $stream
|
||||||
* @param int $limit
|
* @param int $limit
|
||||||
* @return resource
|
* @return resource
|
||||||
*/
|
*/
|
||||||
static public function wrap($stream, $limit) {
|
static public function wrap($stream, $limit) {
|
||||||
$id = uniqid();
|
$context = stream_context_create(array(
|
||||||
self::register($id, $stream, $limit);
|
'quota' => array(
|
||||||
$meta = stream_get_meta_data($stream);
|
'source' => $stream,
|
||||||
return fopen('quota://' . $id, $meta['mode']);
|
'limit' => $limit
|
||||||
|
)
|
||||||
|
));
|
||||||
|
return Wrapper::wrapSource($stream, $context, 'quota', self::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stream_open($path, $mode, $options, &$opened_path) {
|
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||||
$id = substr($path, strlen('quota://'));
|
$context = $this->loadContext('quota');
|
||||||
if (isset(self::$streams[$id])) {
|
$this->source = $context['source'];
|
||||||
list($this->source, $this->limit) = self::$streams[$id];
|
$this->limit = $context['limit'];
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dir_opendir($path, $options) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stream_seek($offset, $whence = SEEK_SET) {
|
public function stream_seek($offset, $whence = SEEK_SET) {
|
||||||
|
@ -103,10 +86,6 @@ class Quota {
|
||||||
return fseek($this->source, $offset, $whence) === 0;
|
return fseek($this->source, $offset, $whence) === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stream_tell() {
|
|
||||||
return ftell($this->source);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stream_read($count) {
|
public function stream_read($count) {
|
||||||
$this->limit -= $count;
|
$this->limit -= $count;
|
||||||
return fread($this->source, $count);
|
return fread($this->source, $count);
|
||||||
|
@ -121,37 +100,4 @@ class Quota {
|
||||||
$this->limit -= $size;
|
$this->limit -= $size;
|
||||||
return fwrite($this->source, $data);
|
return fwrite($this->source, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stream_set_option($option, $arg1, $arg2) {
|
|
||||||
switch ($option) {
|
|
||||||
case STREAM_OPTION_BLOCKING:
|
|
||||||
stream_set_blocking($this->source, $arg1);
|
|
||||||
break;
|
|
||||||
case STREAM_OPTION_READ_TIMEOUT:
|
|
||||||
stream_set_timeout($this->source, $arg1, $arg2);
|
|
||||||
break;
|
|
||||||
case STREAM_OPTION_WRITE_BUFFER:
|
|
||||||
stream_set_write_buffer($this->source, $arg1, $arg2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stream_stat() {
|
|
||||||
return fstat($this->source);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stream_lock($mode) {
|
|
||||||
return flock($this->source, $mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stream_flush() {
|
|
||||||
return fflush($this->source);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stream_eof() {
|
|
||||||
return feof($this->source);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function stream_close() {
|
|
||||||
fclose($this->source);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,14 +9,11 @@
|
||||||
namespace Test\Files\Stream;
|
namespace Test\Files\Stream;
|
||||||
|
|
||||||
class QuotaTest extends \Test\TestCase {
|
class QuotaTest extends \Test\TestCase {
|
||||||
protected function tearDown() {
|
|
||||||
\OC\Files\Stream\Quota::clear();
|
|
||||||
parent::tearDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $mode
|
* @param string $mode
|
||||||
* @param integer $limit
|
* @param integer $limit
|
||||||
|
* @return resource
|
||||||
*/
|
*/
|
||||||
protected function getStream($mode, $limit) {
|
protected function getStream($mode, $limit) {
|
||||||
$source = fopen('php://temp', $mode);
|
$source = fopen('php://temp', $mode);
|
||||||
|
|
Loading…
Reference in New Issue