2013-07-02 11:45:34 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2013-07-02 11:45:34 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-07-02 11:45:34 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\Stream;
|
|
|
|
|
|
2017-01-04 09:13:02 -05:00
|
|
|
use Icewind\Streams\Wrapper;
|
|
|
|
|
|
2013-07-02 11:45:34 -04:00
|
|
|
/**
|
|
|
|
|
* stream wrapper limits the amount of data that can be written to a stream
|
|
|
|
|
*
|
2017-01-04 09:13:02 -05:00
|
|
|
* usage: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
|
2013-07-02 11:45:34 -04:00
|
|
|
*/
|
2017-01-04 09:13:02 -05:00
|
|
|
class Quota extends Wrapper {
|
2013-07-02 11:45:34 -04:00
|
|
|
/**
|
|
|
|
|
* @var int $limit
|
|
|
|
|
*/
|
|
|
|
|
private $limit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param resource $stream
|
|
|
|
|
* @param int $limit
|
2024-09-19 12:19:34 -04:00
|
|
|
* @return resource|false
|
2013-07-02 11:45:34 -04:00
|
|
|
*/
|
2020-04-10 10:51:06 -04:00
|
|
|
public static function wrap($stream, $limit) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$context = stream_context_create([
|
|
|
|
|
'quota' => [
|
2017-01-04 09:13:02 -05:00
|
|
|
'source' => $stream,
|
|
|
|
|
'limit' => $limit
|
2020-03-26 04:30:18 -04:00
|
|
|
]
|
|
|
|
|
]);
|
2017-01-04 09:13:02 -05:00
|
|
|
return Wrapper::wrapSource($stream, $context, 'quota', self::class);
|
2013-07-02 11:45:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function stream_open($path, $mode, $options, &$opened_path) {
|
2017-01-04 09:13:02 -05: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 11:45:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function stream_seek($offset, $whence = SEEK_SET) {
|
2013-10-25 06:09:46 -04:00
|
|
|
if ($whence === SEEK_END) {
|
|
|
|
|
// go to the end to find out last position's offset
|
|
|
|
|
$oldOffset = $this->stream_tell();
|
|
|
|
|
if (fseek($this->source, 0, $whence) !== 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$whence = SEEK_SET;
|
|
|
|
|
$offset = $this->stream_tell() + $offset;
|
|
|
|
|
$this->limit += $oldOffset - $offset;
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($whence === SEEK_SET) {
|
2013-07-02 11:45:34 -04:00
|
|
|
$this->limit += $this->stream_tell() - $offset;
|
|
|
|
|
} else {
|
|
|
|
|
$this->limit -= $offset;
|
|
|
|
|
}
|
2013-10-25 10:15:01 -04:00
|
|
|
// this wrapper needs to return "true" for success.
|
|
|
|
|
// the fseek call itself returns 0 on succeess
|
2014-09-22 05:35:42 -04:00
|
|
|
return fseek($this->source, $offset, $whence) === 0;
|
2013-07-02 11: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;
|
|
|
|
|
}
|
2025-10-13 18:26:48 -04:00
|
|
|
$written = fwrite($this->source, $data);
|
|
|
|
|
// Decrement quota by the actual number of bytes written ($written),
|
|
|
|
|
// not the intended size
|
|
|
|
|
$this->limit -= $written;
|
|
|
|
|
return $written;
|
2013-07-02 11:45:34 -04:00
|
|
|
}
|
|
|
|
|
}
|