Completed
Pull Request — master (#5514)
by Robin
21:54
created
apps/dav/lib/Upload/AssemblyStream.php 3 patches
Doc Comments   +5 added lines, -2 removed lines patch added patch discarded remove patch
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
 
147 147
 	/**
148 148
 	 * @param string $data
149
-	 * @return int
149
+	 * @return boolean
150 150
 	 */
151 151
 	public function stream_write($data) {
152 152
 		return false;
@@ -252,7 +252,7 @@  discard block
 block discarded – undo
252 252
 	}
253 253
 
254 254
 	/**
255
-	 * @param $pos
255
+	 * @param integer $pos
256 256
 	 * @return IFile | null
257 257
 	 */
258 258
 	private function getNodeForPosition($pos) {
@@ -280,6 +280,9 @@  discard block
 block discarded – undo
280 280
 		}
281 281
 	}
282 282
 
283
+	/**
284
+	 * @param resource $handle
285
+	 */
283 286
 	private function seekStream($handle, $position) {
284 287
 		if (stream_get_meta_data($handle)['seekable']) {
285 288
 			fseek($this->currentStream, $position);
Please login to merge, or discard this patch.
Indentation   +255 added lines, -255 removed lines patch added patch discarded remove patch
@@ -36,260 +36,260 @@
 block discarded – undo
36 36
  */
37 37
 class AssemblyStream implements \Icewind\Streams\File {
38 38
 
39
-	/** @var resource */
40
-	private $context;
41
-
42
-	/** @var IFile[] */
43
-	private $nodes;
44
-
45
-	/** @var int */
46
-	private $pos = 0;
47
-
48
-	/** @var array */
49
-	private $sortedNodes;
50
-
51
-	/** @var int */
52
-	private $size;
53
-
54
-	/** @var resource */
55
-	private $currentStream = null;
56
-
57
-	/**
58
-	 * @param string $path
59
-	 * @param string $mode
60
-	 * @param int $options
61
-	 * @param string &$opened_path
62
-	 * @return bool
63
-	 */
64
-	public function stream_open($path, $mode, $options, &$opened_path) {
65
-		$this->loadContext('assembly');
66
-
67
-		// sort the nodes
68
-		$nodes = $this->nodes;
69
-		// http://stackoverflow.com/a/10985500
70
-		@usort($nodes, function (IFile $a, IFile $b) {
71
-			return strnatcmp($a->getName(), $b->getName());
72
-		});
73
-		$this->nodes = $nodes;
74
-
75
-		// build additional information
76
-		$this->sortedNodes = [];
77
-		$start = 0;
78
-		foreach ($this->nodes as $node) {
79
-			$size = $node->getSize();
80
-			$name = $node->getName();
81
-			$this->sortedNodes[$name] = ['node' => $node, 'start' => $start, 'end' => $start + $size];
82
-			$start += $size;
83
-			$this->size = $start;
84
-		}
85
-		return true;
86
-	}
87
-
88
-	/**
89
-	 * @param string $offset
90
-	 * @param int $whence
91
-	 * @return bool
92
-	 */
93
-	public function stream_seek($offset, $whence = SEEK_SET) {
94
-		return false;
95
-	}
96
-
97
-	/**
98
-	 * @return int
99
-	 */
100
-	public function stream_tell() {
101
-		return $this->pos;
102
-	}
103
-
104
-	/**
105
-	 * @param int $count
106
-	 * @return string
107
-	 */
108
-	public function stream_read($count) {
109
-		do {
110
-			if ($this->currentStream === null) {
111
-				list($node, $posInNode) = $this->getNodeForPosition($this->pos);
112
-				if (is_null($node)) {
113
-					// reached last node, no more data
114
-					return '';
115
-				}
116
-				$this->currentStream = $this->getStream($node);
117
-				if ($posInNode > 0) {
118
-					$this->seekStream($this->currentStream, $posInNode);
119
-				}
120
-			}
121
-
122
-			$data = fread($this->currentStream, $count);
123
-			// isset is faster than strlen
124
-			if (isset($data[$count - 1])) {
125
-				// we read the full count
126
-				$read = $count;
127
-			} else {
128
-				// reaching end of stream, which happens less often so strlen is ok
129
-				$read = strlen($data);
130
-			}
131
-
132
-			if (feof($this->currentStream)) {
133
-				fclose($this->currentStream);
134
-				$this->currentNode = null;
135
-				$this->currentStream = null;
136
-			}
137
-			// if no data read, try again with the next node because
138
-			// returning empty data can make the caller think there is no more
139
-			// data left to read
140
-		} while ($read === 0);
141
-
142
-		// update position
143
-		$this->pos += $read;
144
-		return $data;
145
-	}
146
-
147
-	/**
148
-	 * @param string $data
149
-	 * @return int
150
-	 */
151
-	public function stream_write($data) {
152
-		return false;
153
-	}
154
-
155
-	/**
156
-	 * @param int $option
157
-	 * @param int $arg1
158
-	 * @param int $arg2
159
-	 * @return bool
160
-	 */
161
-	public function stream_set_option($option, $arg1, $arg2) {
162
-		return false;
163
-	}
164
-
165
-	/**
166
-	 * @param int $size
167
-	 * @return bool
168
-	 */
169
-	public function stream_truncate($size) {
170
-		return false;
171
-	}
172
-
173
-	/**
174
-	 * @return array
175
-	 */
176
-	public function stream_stat() {
177
-		return [];
178
-	}
179
-
180
-	/**
181
-	 * @param int $operation
182
-	 * @return bool
183
-	 */
184
-	public function stream_lock($operation) {
185
-		return false;
186
-	}
187
-
188
-	/**
189
-	 * @return bool
190
-	 */
191
-	public function stream_flush() {
192
-		return false;
193
-	}
194
-
195
-	/**
196
-	 * @return bool
197
-	 */
198
-	public function stream_eof() {
199
-		return $this->pos >= $this->size;
200
-	}
201
-
202
-	/**
203
-	 * @return bool
204
-	 */
205
-	public function stream_close() {
206
-		return true;
207
-	}
208
-
209
-
210
-	/**
211
-	 * Load the source from the stream context and return the context options
212
-	 *
213
-	 * @param string $name
214
-	 * @return array
215
-	 * @throws \Exception
216
-	 */
217
-	protected function loadContext($name) {
218
-		$context = stream_context_get_options($this->context);
219
-		if (isset($context[$name])) {
220
-			$context = $context[$name];
221
-		} else {
222
-			throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
223
-		}
224
-		if (isset($context['nodes']) and is_array($context['nodes'])) {
225
-			$this->nodes = $context['nodes'];
226
-		} else {
227
-			throw new \BadMethodCallException('Invalid context, nodes not set');
228
-		}
229
-		return $context;
230
-	}
231
-
232
-	/**
233
-	 * @param IFile[] $nodes
234
-	 * @return resource
235
-	 *
236
-	 * @throws \BadMethodCallException
237
-	 */
238
-	public static function wrap(array $nodes) {
239
-		$context = stream_context_create([
240
-			'assembly' => [
241
-				'nodes' => $nodes]
242
-		]);
243
-		stream_wrapper_register('assembly', '\OCA\DAV\Upload\AssemblyStream');
244
-		try {
245
-			$wrapped = fopen('assembly://', 'r', null, $context);
246
-		} catch (\BadMethodCallException $e) {
247
-			stream_wrapper_unregister('assembly');
248
-			throw $e;
249
-		}
250
-		stream_wrapper_unregister('assembly');
251
-		return $wrapped;
252
-	}
253
-
254
-	/**
255
-	 * @param $pos
256
-	 * @return IFile | null
257
-	 */
258
-	private function getNodeForPosition($pos) {
259
-		foreach ($this->sortedNodes as $node) {
260
-			if ($pos >= $node['start'] && $pos < $node['end']) {
261
-				return [$node['node'], $pos - $node['start']];
262
-			}
263
-		}
264
-		return null;
265
-	}
266
-
267
-	/**
268
-	 * @param IFile $node
269
-	 * @return resource
270
-	 */
271
-	private function getStream(IFile $node) {
272
-		$data = $node->get();
273
-		if (is_resource($data)) {
274
-			return $data;
275
-		} else {
276
-			$tmp = fopen('php://temp', 'w+');
277
-			fwrite($tmp, $data);
278
-			rewind($tmp);
279
-			return $tmp;
280
-		}
281
-	}
282
-
283
-	private function seekStream($handle, $position) {
284
-		if (stream_get_meta_data($handle)['seekable']) {
285
-			fseek($this->currentStream, $position);
286
-		} else {
287
-			$offset = $position - $this->stream_tell($handle);
288
-			if ($offset < 0) {
289
-				throw new \InvalidArgumentException('Can\'t rewind stream');
290
-			}
291
-			fread($handle, $offset);
292
-		}
293
-	}
39
+    /** @var resource */
40
+    private $context;
41
+
42
+    /** @var IFile[] */
43
+    private $nodes;
44
+
45
+    /** @var int */
46
+    private $pos = 0;
47
+
48
+    /** @var array */
49
+    private $sortedNodes;
50
+
51
+    /** @var int */
52
+    private $size;
53
+
54
+    /** @var resource */
55
+    private $currentStream = null;
56
+
57
+    /**
58
+     * @param string $path
59
+     * @param string $mode
60
+     * @param int $options
61
+     * @param string &$opened_path
62
+     * @return bool
63
+     */
64
+    public function stream_open($path, $mode, $options, &$opened_path) {
65
+        $this->loadContext('assembly');
66
+
67
+        // sort the nodes
68
+        $nodes = $this->nodes;
69
+        // http://stackoverflow.com/a/10985500
70
+        @usort($nodes, function (IFile $a, IFile $b) {
71
+            return strnatcmp($a->getName(), $b->getName());
72
+        });
73
+        $this->nodes = $nodes;
74
+
75
+        // build additional information
76
+        $this->sortedNodes = [];
77
+        $start = 0;
78
+        foreach ($this->nodes as $node) {
79
+            $size = $node->getSize();
80
+            $name = $node->getName();
81
+            $this->sortedNodes[$name] = ['node' => $node, 'start' => $start, 'end' => $start + $size];
82
+            $start += $size;
83
+            $this->size = $start;
84
+        }
85
+        return true;
86
+    }
87
+
88
+    /**
89
+     * @param string $offset
90
+     * @param int $whence
91
+     * @return bool
92
+     */
93
+    public function stream_seek($offset, $whence = SEEK_SET) {
94
+        return false;
95
+    }
96
+
97
+    /**
98
+     * @return int
99
+     */
100
+    public function stream_tell() {
101
+        return $this->pos;
102
+    }
103
+
104
+    /**
105
+     * @param int $count
106
+     * @return string
107
+     */
108
+    public function stream_read($count) {
109
+        do {
110
+            if ($this->currentStream === null) {
111
+                list($node, $posInNode) = $this->getNodeForPosition($this->pos);
112
+                if (is_null($node)) {
113
+                    // reached last node, no more data
114
+                    return '';
115
+                }
116
+                $this->currentStream = $this->getStream($node);
117
+                if ($posInNode > 0) {
118
+                    $this->seekStream($this->currentStream, $posInNode);
119
+                }
120
+            }
121
+
122
+            $data = fread($this->currentStream, $count);
123
+            // isset is faster than strlen
124
+            if (isset($data[$count - 1])) {
125
+                // we read the full count
126
+                $read = $count;
127
+            } else {
128
+                // reaching end of stream, which happens less often so strlen is ok
129
+                $read = strlen($data);
130
+            }
131
+
132
+            if (feof($this->currentStream)) {
133
+                fclose($this->currentStream);
134
+                $this->currentNode = null;
135
+                $this->currentStream = null;
136
+            }
137
+            // if no data read, try again with the next node because
138
+            // returning empty data can make the caller think there is no more
139
+            // data left to read
140
+        } while ($read === 0);
141
+
142
+        // update position
143
+        $this->pos += $read;
144
+        return $data;
145
+    }
146
+
147
+    /**
148
+     * @param string $data
149
+     * @return int
150
+     */
151
+    public function stream_write($data) {
152
+        return false;
153
+    }
154
+
155
+    /**
156
+     * @param int $option
157
+     * @param int $arg1
158
+     * @param int $arg2
159
+     * @return bool
160
+     */
161
+    public function stream_set_option($option, $arg1, $arg2) {
162
+        return false;
163
+    }
164
+
165
+    /**
166
+     * @param int $size
167
+     * @return bool
168
+     */
169
+    public function stream_truncate($size) {
170
+        return false;
171
+    }
172
+
173
+    /**
174
+     * @return array
175
+     */
176
+    public function stream_stat() {
177
+        return [];
178
+    }
179
+
180
+    /**
181
+     * @param int $operation
182
+     * @return bool
183
+     */
184
+    public function stream_lock($operation) {
185
+        return false;
186
+    }
187
+
188
+    /**
189
+     * @return bool
190
+     */
191
+    public function stream_flush() {
192
+        return false;
193
+    }
194
+
195
+    /**
196
+     * @return bool
197
+     */
198
+    public function stream_eof() {
199
+        return $this->pos >= $this->size;
200
+    }
201
+
202
+    /**
203
+     * @return bool
204
+     */
205
+    public function stream_close() {
206
+        return true;
207
+    }
208
+
209
+
210
+    /**
211
+     * Load the source from the stream context and return the context options
212
+     *
213
+     * @param string $name
214
+     * @return array
215
+     * @throws \Exception
216
+     */
217
+    protected function loadContext($name) {
218
+        $context = stream_context_get_options($this->context);
219
+        if (isset($context[$name])) {
220
+            $context = $context[$name];
221
+        } else {
222
+            throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
223
+        }
224
+        if (isset($context['nodes']) and is_array($context['nodes'])) {
225
+            $this->nodes = $context['nodes'];
226
+        } else {
227
+            throw new \BadMethodCallException('Invalid context, nodes not set');
228
+        }
229
+        return $context;
230
+    }
231
+
232
+    /**
233
+     * @param IFile[] $nodes
234
+     * @return resource
235
+     *
236
+     * @throws \BadMethodCallException
237
+     */
238
+    public static function wrap(array $nodes) {
239
+        $context = stream_context_create([
240
+            'assembly' => [
241
+                'nodes' => $nodes]
242
+        ]);
243
+        stream_wrapper_register('assembly', '\OCA\DAV\Upload\AssemblyStream');
244
+        try {
245
+            $wrapped = fopen('assembly://', 'r', null, $context);
246
+        } catch (\BadMethodCallException $e) {
247
+            stream_wrapper_unregister('assembly');
248
+            throw $e;
249
+        }
250
+        stream_wrapper_unregister('assembly');
251
+        return $wrapped;
252
+    }
253
+
254
+    /**
255
+     * @param $pos
256
+     * @return IFile | null
257
+     */
258
+    private function getNodeForPosition($pos) {
259
+        foreach ($this->sortedNodes as $node) {
260
+            if ($pos >= $node['start'] && $pos < $node['end']) {
261
+                return [$node['node'], $pos - $node['start']];
262
+            }
263
+        }
264
+        return null;
265
+    }
266
+
267
+    /**
268
+     * @param IFile $node
269
+     * @return resource
270
+     */
271
+    private function getStream(IFile $node) {
272
+        $data = $node->get();
273
+        if (is_resource($data)) {
274
+            return $data;
275
+        } else {
276
+            $tmp = fopen('php://temp', 'w+');
277
+            fwrite($tmp, $data);
278
+            rewind($tmp);
279
+            return $tmp;
280
+        }
281
+    }
282
+
283
+    private function seekStream($handle, $position) {
284
+        if (stream_get_meta_data($handle)['seekable']) {
285
+            fseek($this->currentStream, $position);
286
+        } else {
287
+            $offset = $position - $this->stream_tell($handle);
288
+            if ($offset < 0) {
289
+                throw new \InvalidArgumentException('Can\'t rewind stream');
290
+            }
291
+            fread($handle, $offset);
292
+        }
293
+    }
294 294
 
295 295
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
 		// sort the nodes
68 68
 		$nodes = $this->nodes;
69 69
 		// http://stackoverflow.com/a/10985500
70
-		@usort($nodes, function (IFile $a, IFile $b) {
70
+		@usort($nodes, function(IFile $a, IFile $b) {
71 71
 			return strnatcmp($a->getName(), $b->getName());
72 72
 		});
73 73
 		$this->nodes = $nodes;
@@ -219,7 +219,7 @@  discard block
 block discarded – undo
219 219
 		if (isset($context[$name])) {
220 220
 			$context = $context[$name];
221 221
 		} else {
222
-			throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
222
+			throw new \BadMethodCallException('Invalid context, "'.$name.'" options not set');
223 223
 		}
224 224
 		if (isset($context['nodes']) and is_array($context['nodes'])) {
225 225
 			$this->nodes = $context['nodes'];
Please login to merge, or discard this patch.