remove the need to register the quota streamwrapper globally

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2017-01-04 15:13:02 +01:00
parent 9dbcc1a177
commit 968de70bc5
No known key found for this signature in database
GPG Key ID: CBCA68FBAEBF98C9
3 changed files with 21 additions and 79 deletions

View File

@ -669,7 +669,6 @@ class OC {
// register the stream wrappers
stream_wrapper_register('static', 'OC\Files\Stream\StaticStream');
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_App::loadApps(array('session'));

View File

@ -25,61 +25,44 @@
namespace OC\Files\Stream;
use Icewind\Streams\Wrapper;
/**
* stream wrapper limits the amount of data that can be written to a stream
*
* usage: void \OC\Files\Stream\Quota::register($id, $stream, $limit)
* or: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
* usage: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
*/
class Quota {
private static $streams = array();
/**
* @var resource $source
*/
private $source;
class Quota extends Wrapper {
/**
* @var int $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 int $limit
* @return resource
*/
static public function wrap($stream, $limit) {
$id = uniqid();
self::register($id, $stream, $limit);
$meta = stream_get_meta_data($stream);
return fopen('quota://' . $id, $meta['mode']);
$context = stream_context_create(array(
'quota' => array(
'source' => $stream,
'limit' => $limit
)
));
return Wrapper::wrapSource($stream, $context, 'quota', self::class);
}
public function stream_open($path, $mode, $options, &$opened_path) {
$id = substr($path, strlen('quota://'));
if (isset(self::$streams[$id])) {
list($this->source, $this->limit) = self::$streams[$id];
return true;
} else {
return false;
}
$context = $this->loadContext('quota');
$this->source = $context['source'];
$this->limit = $context['limit'];
return true;
}
public function dir_opendir($path, $options) {
return false;
}
public function stream_seek($offset, $whence = SEEK_SET) {
@ -103,10 +86,6 @@ class Quota {
return fseek($this->source, $offset, $whence) === 0;
}
public function stream_tell() {
return ftell($this->source);
}
public function stream_read($count) {
$this->limit -= $count;
return fread($this->source, $count);
@ -121,37 +100,4 @@ class Quota {
$this->limit -= $size;
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);
}
}

View File

@ -9,14 +9,11 @@
namespace Test\Files\Stream;
class QuotaTest extends \Test\TestCase {
protected function tearDown() {
\OC\Files\Stream\Quota::clear();
parent::tearDown();
}
/**
* @param string $mode
* @param integer $limit
* @return resource
*/
protected function getStream($mode, $limit) {
$source = fopen('php://temp', $mode);