summaryrefslogtreecommitdiffstats
path: root/lib/private/Files/Stream/CountReadStream.php
diff options
context:
space:
mode:
authorTomasz Grobelny <tomasz@grobelny.net>2018-11-03 23:55:06 +0000
committerTomasz Grobelny <tomasz@grobelny.net>2018-11-04 09:39:19 +0000
commit1fa6e0be23f0684ce76de3311c52e01495a4de7e (patch)
treeef50b564c793d77ef5ade04349f6c5f97a22c7f5 /lib/private/Files/Stream/CountReadStream.php
parent41687ef00fa5667467564770a4e29403e32d167f (diff)
parentb9783993da7c689b0b1e55dc5696b0c3f90a4c10 (diff)
downloadnextcloud-server-1fa6e0be23f0684ce76de3311c52e01495a4de7e.tar.gz
nextcloud-server-1fa6e0be23f0684ce76de3311c52e01495a4de7e.zip
Merge remote-tracking branch 'upstream/master' into fix_file_move
Signed-off-by: Tomasz Grobelny <tomasz@grobelny.net>
Diffstat (limited to 'lib/private/Files/Stream/CountReadStream.php')
-rw-r--r--lib/private/Files/Stream/CountReadStream.php65
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/private/Files/Stream/CountReadStream.php b/lib/private/Files/Stream/CountReadStream.php
new file mode 100644
index 00000000000..93cadf8f214
--- /dev/null
+++ b/lib/private/Files/Stream/CountReadStream.php
@@ -0,0 +1,65 @@
+<?php declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\Files\Stream;
+
+use Icewind\Streams\Wrapper;
+
+class CountReadStream extends Wrapper {
+ /** @var int */
+ private $count;
+
+ /** @var callback */
+ private $callback;
+
+ public static function wrap($source, $callback) {
+ $context = stream_context_create(array(
+ 'count' => array(
+ 'source' => $source,
+ 'callback' => $callback,
+ )
+ ));
+ return Wrapper::wrapSource($source, $context, 'count', self::class);
+ }
+
+ public function dir_opendir($path, $options) {
+ return false;
+ }
+
+ public function stream_open($path, $mode, $options, &$opened_path) {
+ $context = $this->loadContext('count');
+
+ $this->callback = $context['callback'];
+ return true;
+ }
+
+ public function stream_read($count) {
+ $result = parent::stream_read($count);
+ $this->count += strlen($result);
+ return $result;
+ }
+
+ public function stream_close() {
+ $result = parent::stream_close();
+ call_user_func($this->callback, $this->count);
+ return $result;
+ }
+}