2013-07-02 19:45:34 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2013-07-02 19:45:34 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-07-02 19:45:34 +04:00
|
|
|
namespace OC\Files\Stream;
|
|
|
|
|
2017-01-04 17:13:02 +03:00
|
|
|
use Icewind\Streams\Wrapper;
|
|
|
|
|
2013-07-02 19:45:34 +04:00
|
|
|
/**
|
|
|
|
* stream wrapper limits the amount of data that can be written to a stream
|
|
|
|
*
|
2017-01-04 17:13:02 +03:00
|
|
|
* usage: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
|
2013-07-02 19:45:34 +04:00
|
|
|
*/
|
2017-01-04 17:13:02 +03:00
|
|
|
class Quota extends Wrapper {
|
2013-07-02 19:45:34 +04:00
|
|
|
/**
|
|
|
|
* @var int $limit
|
|
|
|
*/
|
|
|
|
private $limit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param resource $stream
|
|
|
|
* @param int $limit
|
2020-12-03 13:03:17 +03:00
|
|
|
* @return bool|resource
|
2013-07-02 19:45:34 +04:00
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public static function wrap($stream, $limit) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$context = stream_context_create([
|
|
|
|
'quota' => [
|
2017-01-04 17:13:02 +03:00
|
|
|
'source' => $stream,
|
|
|
|
'limit' => $limit
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
]);
|
2017-01-04 17:13:02 +03:00
|
|
|
return Wrapper::wrapSource($stream, $context, 'quota', self::class);
|
2013-07-02 19:45:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_open($path, $mode, $options, &$opened_path) {
|
2017-01-04 17:13:02 +03:00
|
|
|
$context = $this->loadContext('quota');
|
|
|
|
$this->source = $context['source'];
|
|
|
|
$this->limit = $context['limit'];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dir_opendir($path, $options) {
|
|
|
|
return false;
|
2013-07-02 19:45:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_seek($offset, $whence = SEEK_SET) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($whence === SEEK_END) {
|
2013-10-25 14:09:46 +04:00
|
|
|
// go to the end to find out last position's offset
|
|
|
|
$oldOffset = $this->stream_tell();
|
2020-04-10 15:19:56 +03:00
|
|
|
if (fseek($this->source, 0, $whence) !== 0) {
|
2013-10-25 14:09:46 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$whence = SEEK_SET;
|
|
|
|
$offset = $this->stream_tell() + $offset;
|
|
|
|
$this->limit += $oldOffset - $offset;
|
2020-04-10 15:19:56 +03:00
|
|
|
} elseif ($whence === SEEK_SET) {
|
2013-07-02 19:45:34 +04:00
|
|
|
$this->limit += $this->stream_tell() - $offset;
|
|
|
|
} else {
|
|
|
|
$this->limit -= $offset;
|
|
|
|
}
|
2013-10-25 18:15:01 +04:00
|
|
|
// this wrapper needs to return "true" for success.
|
|
|
|
// the fseek call itself returns 0 on succeess
|
2014-09-22 13:35:42 +04:00
|
|
|
return fseek($this->source, $offset, $whence) === 0;
|
2013-07-02 19:45:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_read($count) {
|
|
|
|
$this->limit -= $count;
|
|
|
|
return fread($this->source, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stream_write($data) {
|
|
|
|
$size = strlen($data);
|
|
|
|
if ($size > $this->limit) {
|
|
|
|
$data = substr($data, 0, $this->limit);
|
|
|
|
$size = $this->limit;
|
|
|
|
}
|
|
|
|
$this->limit -= $size;
|
|
|
|
return fwrite($this->source, $data);
|
|
|
|
}
|
|
|
|
}
|