@@ -15,73 +15,73 @@ |
||
| 15 | 15 | * usage: resource \OC\Files\Stream\Quota::wrap($stream, $limit) |
| 16 | 16 | */ |
| 17 | 17 | class Quota extends Wrapper { |
| 18 | - /** |
|
| 19 | - * @var int $limit |
|
| 20 | - */ |
|
| 21 | - private $limit; |
|
| 18 | + /** |
|
| 19 | + * @var int $limit |
|
| 20 | + */ |
|
| 21 | + private $limit; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @param resource $stream |
|
| 25 | - * @param int $limit |
|
| 26 | - * @return resource|false |
|
| 27 | - */ |
|
| 28 | - public static function wrap($stream, $limit) { |
|
| 29 | - $context = stream_context_create([ |
|
| 30 | - 'quota' => [ |
|
| 31 | - 'source' => $stream, |
|
| 32 | - 'limit' => $limit |
|
| 33 | - ] |
|
| 34 | - ]); |
|
| 35 | - return Wrapper::wrapSource($stream, $context, 'quota', self::class); |
|
| 36 | - } |
|
| 23 | + /** |
|
| 24 | + * @param resource $stream |
|
| 25 | + * @param int $limit |
|
| 26 | + * @return resource|false |
|
| 27 | + */ |
|
| 28 | + public static function wrap($stream, $limit) { |
|
| 29 | + $context = stream_context_create([ |
|
| 30 | + 'quota' => [ |
|
| 31 | + 'source' => $stream, |
|
| 32 | + 'limit' => $limit |
|
| 33 | + ] |
|
| 34 | + ]); |
|
| 35 | + return Wrapper::wrapSource($stream, $context, 'quota', self::class); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function stream_open($path, $mode, $options, &$opened_path) { |
|
| 39 | - $context = $this->loadContext('quota'); |
|
| 40 | - $this->source = $context['source']; |
|
| 41 | - $this->limit = $context['limit']; |
|
| 38 | + public function stream_open($path, $mode, $options, &$opened_path) { |
|
| 39 | + $context = $this->loadContext('quota'); |
|
| 40 | + $this->source = $context['source']; |
|
| 41 | + $this->limit = $context['limit']; |
|
| 42 | 42 | |
| 43 | - return true; |
|
| 44 | - } |
|
| 43 | + return true; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function dir_opendir($path, $options) { |
|
| 47 | - return false; |
|
| 48 | - } |
|
| 46 | + public function dir_opendir($path, $options) { |
|
| 47 | + return false; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function stream_seek($offset, $whence = SEEK_SET) { |
|
| 51 | - if ($whence === SEEK_END) { |
|
| 52 | - // go to the end to find out last position's offset |
|
| 53 | - $oldOffset = $this->stream_tell(); |
|
| 54 | - if (fseek($this->source, 0, $whence) !== 0) { |
|
| 55 | - return false; |
|
| 56 | - } |
|
| 57 | - $whence = SEEK_SET; |
|
| 58 | - $offset = $this->stream_tell() + $offset; |
|
| 59 | - $this->limit += $oldOffset - $offset; |
|
| 60 | - } elseif ($whence === SEEK_SET) { |
|
| 61 | - $this->limit += $this->stream_tell() - $offset; |
|
| 62 | - } else { |
|
| 63 | - $this->limit -= $offset; |
|
| 64 | - } |
|
| 65 | - // this wrapper needs to return "true" for success. |
|
| 66 | - // the fseek call itself returns 0 on succeess |
|
| 67 | - return fseek($this->source, $offset, $whence) === 0; |
|
| 68 | - } |
|
| 50 | + public function stream_seek($offset, $whence = SEEK_SET) { |
|
| 51 | + if ($whence === SEEK_END) { |
|
| 52 | + // go to the end to find out last position's offset |
|
| 53 | + $oldOffset = $this->stream_tell(); |
|
| 54 | + if (fseek($this->source, 0, $whence) !== 0) { |
|
| 55 | + return false; |
|
| 56 | + } |
|
| 57 | + $whence = SEEK_SET; |
|
| 58 | + $offset = $this->stream_tell() + $offset; |
|
| 59 | + $this->limit += $oldOffset - $offset; |
|
| 60 | + } elseif ($whence === SEEK_SET) { |
|
| 61 | + $this->limit += $this->stream_tell() - $offset; |
|
| 62 | + } else { |
|
| 63 | + $this->limit -= $offset; |
|
| 64 | + } |
|
| 65 | + // this wrapper needs to return "true" for success. |
|
| 66 | + // the fseek call itself returns 0 on succeess |
|
| 67 | + return fseek($this->source, $offset, $whence) === 0; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - public function stream_read($count) { |
|
| 71 | - $this->limit -= $count; |
|
| 72 | - return fread($this->source, $count); |
|
| 73 | - } |
|
| 70 | + public function stream_read($count) { |
|
| 71 | + $this->limit -= $count; |
|
| 72 | + return fread($this->source, $count); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - public function stream_write($data) { |
|
| 76 | - $size = strlen($data); |
|
| 77 | - if ($size > $this->limit) { |
|
| 78 | - $data = substr($data, 0, $this->limit); |
|
| 79 | - $size = $this->limit; |
|
| 80 | - } |
|
| 81 | - $written = fwrite($this->source, $data); |
|
| 82 | - // Decrement quota by the actual number of bytes written ($written), |
|
| 83 | - // not the intended size |
|
| 84 | - $this->limit -= $written; |
|
| 85 | - return $written; |
|
| 86 | - } |
|
| 75 | + public function stream_write($data) { |
|
| 76 | + $size = strlen($data); |
|
| 77 | + if ($size > $this->limit) { |
|
| 78 | + $data = substr($data, 0, $this->limit); |
|
| 79 | + $size = $this->limit; |
|
| 80 | + } |
|
| 81 | + $written = fwrite($this->source, $data); |
|
| 82 | + // Decrement quota by the actual number of bytes written ($written), |
|
| 83 | + // not the intended size |
|
| 84 | + $this->limit -= $written; |
|
| 85 | + return $written; |
|
| 86 | + } |
|
| 87 | 87 | } |