summaryrefslogtreecommitdiffstats
path: root/apps/files_external/3rdparty/icewind/streams/src/SeekableWrapper.php
blob: d41fd73ec9c2e6e56c4397010d77c0375913b55f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
 * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Licensed under the MIT license:
 * http://opensource.org/licenses/MIT
 */

namespace Icewind\Streams;

/**
 * Wrapper that provides callbacks for write, read and close
 *
 * The following options should be passed in the context when opening the stream
 * [
 *     'callback' => [
 *        'source'  => resource
 *     ]
 * ]
 *
 * All callbacks are called after the operation is executed on the source stream
 */
class SeekableWrapper extends Wrapper {
	/**
	 * @var resource
	 */
	protected $cache;

	/**
	 * Wraps a stream to make it seekable
	 *
	 * @param resource $source
	 * @return resource
	 *
	 * @throws \BadMethodCallException
	 */
	public static function wrap($source) {
		$context = stream_context_create(array(
			'callback' => array(
				'source' => $source
			)
		));
		return Wrapper::wrapSource($source, $context, 'callback', '\Icewind\Streams\SeekableWrapper');
	}

	public function dir_opendir($path, $options) {
		return false;
	}

	public function stream_open($path, $mode, $options, &$opened_path) {
		$this->loadContext('callback');
		$this->cache = fopen('php://temp', 'w+');
		return true;
	}

	protected function readTill($position) {
		$current = ftell($this->source);
		if ($position > $current) {
			$data = parent::stream_read($position - $current);
			$cachePosition = ftell($this->cache);
			fseek($this->cache, $current);
			fwrite($this->cache, $data);
			fseek($this->cache, $cachePosition);
		}
	}

	public function stream_read($count) {
		$current = ftell($this->cache);
		$this->readTill($current + $count);
		return fread($this->cache, $count);
	}

	public function stream_seek($offset, $whence = SEEK_SET) {
		if ($whence === SEEK_SET) {
			$target = $offset;
		} else if ($whence === SEEK_CUR) {
			$current = ftell($this->cache);
			$target = $current + $offset;
		} else {
			return false;
		}
		$this->readTill($target);
		return fseek($this->cache, $target) === 0;
	}

	public function stream_tell() {
		return ftell($this->cache);
	}

	public function stream_eof() {
		return parent::stream_eof() and (ftell($this->source) === ftell($this->cache));
	}
}