Skip to content
This repository
Browse code

Deprecated MediaView and updated MediaView::render() to use CakeRespo…

…nse::file()
  • Loading branch information...
commit 69eba678f49e804846191d884d3e6ed761c4b447 1 parent c5d1260
ADmad authored July 11, 2012
204  lib/Cake/Test/Case/View/MediaViewTest.php
@@ -35,8 +35,15 @@ class MediaViewTest extends CakeTestCase {
35 35
  */
36 36
 	public function setUp() {
37 37
 		parent::setUp();
38  
-		$this->MediaView = $this->getMock('MediaView', array('_isActive', '_clearBuffer', '_flushBuffer'));
39  
-		$this->MediaView->response = $this->getMock('CakeResponse');
  38
+		$this->MediaView = new MediaView();
  39
+		$this->MediaView->response = $this->getMock('CakeResponse', array(
  40
+			'_isActive',
  41
+			'_clearBuffer',
  42
+			'_flushBuffer',
  43
+			'type',
  44
+			'header',
  45
+			'download'
  46
+		));
40 47
 	}
41 48
 
42 49
 /**
@@ -71,10 +78,10 @@ public function testRenderNotFound() {
71 78
 	public function testRender() {
72 79
 		$this->MediaView->viewVars = array(
73 80
 			'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
74  
-			'id' => 'test_asset.css',
75  
-			'extension' => 'css',
  81
+			'id' => 'test_asset.css'
76 82
 		);
77  
-		$this->MediaView->expects($this->exactly(2))
  83
+
  84
+		$this->MediaView->response->expects($this->exactly(1))
78 85
 			->method('_isActive')
79 86
 			->will($this->returnValue(true));
80 87
 
@@ -83,23 +90,23 @@ public function testRender() {
83 90
 			->with('css')
84 91
 			->will($this->returnArgument(0));
85 92
 
86  
-		$this->MediaView->response->expects($this->at(1))
  93
+		$this->MediaView->response->expects($this->at(0))
87 94
 			->method('header')
88 95
 			->with(array(
89  
-				'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
90  
-				'Expires' => '0',
91  
-				'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
92  
-				'Pragma' => 'no-cache'
  96
+				'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  97
+				'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  98
+				'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
93 99
 			));
94 100
 
95 101
 		$this->MediaView->response->expects($this->at(2))
96 102
 			->method('header')
97  
-			->with(array(
98  
-				'Content-Length' => 31
99  
-			));
100  
-		$this->MediaView->response->expects($this->once())->method('send');
101  
-		$this->MediaView->expects($this->once())->method('_clearBuffer');
102  
-		$this->MediaView->expects($this->once())->method('_flushBuffer');
  103
+			->with('Content-Length', 31);
  104
+
  105
+		$this->MediaView->response->expects($this->once())->method('_clearBuffer');
  106
+		$this->MediaView->response->expects($this->exactly(1))
  107
+			->method('_isActive')
  108
+			->will($this->returnValue(true));
  109
+		$this->MediaView->response->expects($this->once())->method('_flushBuffer');
103 110
 
104 111
 		ob_start();
105 112
 		$result = $this->MediaView->render();
@@ -118,25 +125,20 @@ public function testRenderWithUnknownFileTypeGeneric() {
118 125
 		$_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
119 126
 		$this->MediaView->viewVars = array(
120 127
 			'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
121  
-			'id' => 'no_section.ini',
122  
-			'extension' => 'ini',
  128
+			'id' => 'no_section.ini'
123 129
 		);
124  
-		$this->MediaView->expects($this->exactly(2))
125  
-			->method('_isActive')
126  
-			->will($this->returnValue(true));
127 130
 
128 131
 		$this->MediaView->response->expects($this->exactly(1))
129 132
 			->method('type')
130 133
 			->with('ini')
131 134
 			->will($this->returnValue(false));
132 135
 
133  
-		$this->MediaView->response->expects($this->at(1))
  136
+		$this->MediaView->response->expects($this->at(0))
134 137
 			->method('header')
135 138
 			->with(array(
136  
-				'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
137  
-				'Expires' => '0',
138  
-				'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
139  
-				'Pragma' => 'no-cache'
  139
+				'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  140
+				'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  141
+				'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
140 142
 			));
141 143
 
142 144
 		$this->MediaView->response->expects($this->once())
@@ -145,17 +147,17 @@ public function testRenderWithUnknownFileTypeGeneric() {
145 147
 
146 148
 		$this->MediaView->response->expects($this->at(3))
147 149
 			->method('header')
148  
-			->with(array(
149  
-				'Accept-Ranges' => 'bytes'
150  
-			));
  150
+			->with('Accept-Ranges', 'bytes');
151 151
 
152 152
 		$this->MediaView->response->expects($this->at(4))
153 153
 			->method('header')
154 154
 			->with('Content-Length', 35);
155 155
 
156  
-		$this->MediaView->response->expects($this->once())->method('send');
157  
-		$this->MediaView->expects($this->once())->method('_clearBuffer');
158  
-		$this->MediaView->expects($this->once())->method('_flushBuffer');
  156
+		$this->MediaView->response->expects($this->once())->method('_clearBuffer');
  157
+		$this->MediaView->response->expects($this->exactly(1))
  158
+			->method('_isActive')
  159
+			->will($this->returnValue(true));
  160
+		$this->MediaView->response->expects($this->once())->method('_flushBuffer');
159 161
 
160 162
 		ob_start();
161 163
 		$result = $this->MediaView->render();
@@ -178,48 +180,43 @@ public function testRenderWithUnknownFileTypeOpera() {
178 180
 		$this->MediaView->viewVars = array(
179 181
 			'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
180 182
 			'id' => 'no_section.ini',
181  
-			'extension' => 'ini',
182 183
 		);
183  
-		$this->MediaView->expects($this->exactly(2))
184  
-			->method('_isActive')
185  
-			->will($this->returnValue(true));
186 184
 
187 185
 		$this->MediaView->response->expects($this->at(0))
188  
-			->method('type')
189  
-			->with('ini')
190  
-			->will($this->returnValue(false));
191  
-
192  
-		$this->MediaView->response->expects($this->at(1))
193 186
 			->method('header')
194 187
 			->with(array(
195  
-				'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
196  
-				'Expires' => '0',
197  
-				'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
198  
-				'Pragma' => 'no-cache'
  188
+				'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  189
+				'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  190
+				'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
199 191
 			));
200 192
 
  193
+		$this->MediaView->response->expects($this->at(1))
  194
+			->method('type')
  195
+			->with('ini')
  196
+			->will($this->returnValue(false));
  197
+
201 198
 		$this->MediaView->response->expects($this->at(2))
202 199
 			->method('type')
203 200
 			->with('application/octetstream')
204 201
 			->will($this->returnValue(false));
205 202
 
206  
-		$this->MediaView->response->expects($this->once())
  203
+		$this->MediaView->response->expects($this->at(3))
207 204
 			->method('download')
208 205
 			->with('no_section.ini');
209 206
 
210 207
 		$this->MediaView->response->expects($this->at(4))
211 208
 			->method('header')
212  
-			->with(array(
213  
-				'Accept-Ranges' => 'bytes'
214  
-			));
  209
+			->with('Accept-Ranges', 'bytes');
215 210
 
216 211
 		$this->MediaView->response->expects($this->at(5))
217 212
 			->method('header')
218 213
 			->with('Content-Length', 35);
219 214
 
220  
-		$this->MediaView->response->expects($this->once())->method('send');
221  
-		$this->MediaView->expects($this->once())->method('_clearBuffer');
222  
-		$this->MediaView->expects($this->once())->method('_flushBuffer');
  215
+		$this->MediaView->response->expects($this->once())->method('_clearBuffer');
  216
+		$this->MediaView->response->expects($this->exactly(1))
  217
+			->method('_isActive')
  218
+			->will($this->returnValue(true));
  219
+		$this->MediaView->response->expects($this->once())->method('_flushBuffer');
223 220
 
224 221
 		ob_start();
225 222
 		$result = $this->MediaView->render();
@@ -242,49 +239,44 @@ public function testRenderWithUnknownFileTypeIE() {
242 239
 		$this->MediaView->viewVars = array(
243 240
 			'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
244 241
 			'id' => 'no_section.ini',
245  
-			'extension' => 'ini',
246 242
 			'name' => 'config'
247 243
 		);
248  
-		$this->MediaView->expects($this->exactly(2))
249  
-			->method('_isActive')
250  
-			->will($this->returnValue(true));
251 244
 
252 245
 		$this->MediaView->response->expects($this->at(0))
253  
-			->method('type')
254  
-			->with('ini')
255  
-			->will($this->returnValue(false));
256  
-
257  
-		$this->MediaView->response->expects($this->at(1))
258 246
 			->method('header')
259 247
 			->with(array(
260  
-				'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
261  
-				'Expires' => '0',
262  
-				'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
263  
-				'Pragma' => 'no-cache'
  248
+				'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  249
+				'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  250
+				'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
264 251
 			));
265 252
 
  253
+		$this->MediaView->response->expects($this->at(1))
  254
+			->method('type')
  255
+			->with('ini')
  256
+			->will($this->returnValue(false));
  257
+
266 258
 		$this->MediaView->response->expects($this->at(2))
267 259
 			->method('type')
268 260
 			->with('application/force-download')
269 261
 			->will($this->returnValue(false));
270 262
 
271  
-		$this->MediaView->response->expects($this->once())
  263
+		$this->MediaView->response->expects($this->at(3))
272 264
 			->method('download')
273 265
 			->with('config.ini');
274 266
 
275 267
 		$this->MediaView->response->expects($this->at(4))
276 268
 			->method('header')
277  
-			->with(array(
278  
-				'Accept-Ranges' => 'bytes'
279  
-			));
  269
+			->with('Accept-Ranges', 'bytes');
280 270
 
281 271
 		$this->MediaView->response->expects($this->at(5))
282 272
 			->method('header')
283 273
 			->with('Content-Length', 35);
284 274
 
285  
-		$this->MediaView->response->expects($this->once())->method('send');
286  
-		$this->MediaView->expects($this->once())->method('_clearBuffer');
287  
-		$this->MediaView->expects($this->once())->method('_flushBuffer');
  275
+		$this->MediaView->response->expects($this->once())->method('_clearBuffer');
  276
+		$this->MediaView->response->expects($this->exactly(1))
  277
+			->method('_isActive')
  278
+			->will($this->returnValue(true));
  279
+		$this->MediaView->response->expects($this->once())->method('_flushBuffer');
288 280
 
289 281
 		ob_start();
290 282
 		$result = $this->MediaView->render();
@@ -297,29 +289,6 @@ public function testRenderWithUnknownFileTypeIE() {
297 289
 	}
298 290
 
299 291
 /**
300  
- * testConnectionAborted method
301  
- *
302  
- * @return void
303  
- */
304  
-	public function testConnectionAborted() {
305  
-		$this->MediaView->viewVars = array(
306  
-			'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
307  
-			'id' => 'test_asset.css',
308  
-			'extension' => 'css',
309  
-		);
310  
-
311  
-		$this->MediaView->expects($this->once())
312  
-			->method('_isActive')
313  
-			->will($this->returnValue(false));
314  
-
315  
-		$this->MediaView->response->expects($this->never())
316  
-			->method('type');
317  
-
318  
-		$result = $this->MediaView->render();
319  
-		$this->assertFalse($result);
320  
-	}
321  
-
322  
-/**
323 292
  * testConnectionAbortedOnBuffering method
324 293
  *
325 294
  * @return void
@@ -327,29 +296,22 @@ public function testConnectionAborted() {
327 296
 	public function testConnectionAbortedOnBuffering() {
328 297
 		$this->MediaView->viewVars = array(
329 298
 			'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
330  
-			'id' => 'test_asset.css',
331  
-			'extension' => 'css',
  299
+			'id' => 'test_asset.css'
332 300
 		);
333 301
 
334  
-		$this->MediaView->expects($this->at(0))
335  
-			->method('_isActive')
336  
-			->will($this->returnValue(true));
337  
-
338 302
 		$this->MediaView->response->expects($this->any())
339 303
 			->method('type')
340 304
 			->with('css')
341 305
 			->will($this->returnArgument(0));
342 306
 
343  
-		$this->MediaView->expects($this->at(1))
  307
+		$this->MediaView->response->expects($this->at(1))
344 308
 			->method('_isActive')
345 309
 			->will($this->returnValue(false));
346 310
 
347  
-		$this->MediaView->response->expects($this->once())->method('send');
348  
-		$this->MediaView->expects($this->once())->method('_clearBuffer');
349  
-		$this->MediaView->expects($this->never())->method('_flushBuffer');
  311
+		$this->MediaView->response->expects($this->once())->method('_clearBuffer');
  312
+		$this->MediaView->response->expects($this->never())->method('_flushBuffer');
350 313
 
351  
-		$result = $this->MediaView->render();
352  
-		$this->assertFalse($result);
  314
+		$this->MediaView->render();
353 315
 	}
354 316
 
355 317
 /**
@@ -360,31 +322,7 @@ public function testConnectionAbortedOnBuffering() {
360 322
 	public function testRenderUpperExtension() {
361 323
 		$this->MediaView->viewVars = array(
362 324
 			'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS,
363  
-			'id' => 'test_2.JPG',
364  
-			'extension' => 'JPG',
365  
-		);
366  
-
367  
-		$this->MediaView->response->expects($this->any())
368  
-			->method('type')
369  
-			->with('jpg')
370  
-			->will($this->returnArgument(0));
371  
-
372  
-		$this->MediaView->expects($this->at(0))
373  
-			->method('_isActive')
374  
-			->will($this->returnValue(true));
375  
-
376  
-		$this->MediaView->render();
377  
-	}
378  
-
379  
-/**
380  
- * Test downloading files with extension not explicitly set.
381  
- *
382  
- * @return void
383  
- */
384  
-	public function testRenderExtensionNotSet() {
385  
-		$this->MediaView->viewVars = array(
386  
-			'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS,
387  
-			'id' => 'test_2.JPG',
  325
+			'id' => 'test_2.JPG'
388 326
 		);
389 327
 
390 328
 		$this->MediaView->response->expects($this->any())
@@ -392,7 +330,7 @@ public function testRenderExtensionNotSet() {
392 330
 			->with('jpg')
393 331
 			->will($this->returnArgument(0));
394 332
 
395  
-		$this->MediaView->expects($this->at(0))
  333
+		$this->MediaView->response->expects($this->at(0))
396 334
 			->method('_isActive')
397 335
 			->will($this->returnValue(true));
398 336
 
163  lib/Cake/View/MediaView.php
@@ -15,6 +15,7 @@
15 15
  * @package       Cake.View
16 16
  * @since         CakePHP(tm) v 1.2.0.5714
17 17
  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  18
+ * @deprecated    Deprecated since version 2.3, use CakeResponse::file() instead
18 19
  */
19 20
 
20 21
 App::uses('View', 'View');
@@ -31,8 +32,6 @@
31 32
  * - `id` The filename on the server's filesystem, including extension.
32 33
  * - `name` The filename that will be sent to the user, specified without the extension.
33 34
  * - `download` Set to true to set a `Content-Disposition` header.  This is ideal for file downloads.
34  
- * - `extension` The extension of the file being served. This is used to set the mimetype.
35  
- * 	If not provided its extracted from filename provided as `id`.
36 35
  * - `path` The absolute path, including the trailing / on the server's filesystem to `id`.
37 36
  * - `mimeType` The mime type of the file if CakeResponse doesn't know about it.
38 37
  * 	Must be an associative array with extension as key and mime type as value eg. array('ini' => 'text/plain')
@@ -60,22 +59,14 @@
60 59
 class MediaView extends View {
61 60
 
62 61
 /**
63  
- * Indicates whether response gzip compression was enabled for this class
64  
- *
65  
- * @var boolean
66  
- */
67  
-	protected  $_compressionEnabled = false;
68  
-
69  
-/**
70 62
  * Display or download the given file
71 63
  *
72 64
  * @param string $view Not used
73 65
  * @param string $layout Not used
74  
- * @return mixed
75  
- * @throws NotFoundException
  66
+ * @return boolean
76 67
  */
77 68
 	public function render($view = null, $layout = null) {
78  
-		$name = $download = $extension = $id = $modified = $path = $cache = $mimeType = $compress = null;
  69
+		$name = $download = $id = $modified = $path = $cache = $mimeType = $compress = null;
79 70
 		extract($this->viewVars, EXTR_OVERWRITE);
80 71
 
81 72
 		if (is_dir($path)) {
@@ -84,155 +75,31 @@ public function render($view = null, $layout = null) {
84 75
 			$path = APP . $path . $id;
85 76
 		}
86 77
 
87  
-		if (!is_file($path)) {
88  
-			if (Configure::read('debug')) {
89  
-				throw new NotFoundException(sprintf('The requested file %s was not found', $path));
90  
-			}
91  
-			throw new NotFoundException('The requested file was not found');
92  
-		}
93  
-
94 78
 		if (is_array($mimeType)) {
95 79
 			$this->response->type($mimeType);
96 80
 		}
97 81
 
98  
-		if (!isset($extension)) {
99  
-			$extension = pathinfo($id, PATHINFO_EXTENSION);
100  
-		}
101  
-
102  
-		if ($this->_isActive()) {
103  
-			$extension = strtolower($extension);
104  
-			$chunkSize = 8192;
105  
-			$buffer = '';
106  
-			$fileSize = @filesize($path);
107  
-			$handle = fopen($path, 'rb');
108  
-
109  
-			if ($handle === false) {
110  
-				return false;
111  
-			}
  82
+		if ($cache) {
112 83
 			if (!empty($modified) && !is_numeric($modified)) {
113 84
 				$modified = strtotime($modified, time());
114 85
 			} else {
115 86
 				$modified = time();
116 87
 			}
117  
-			if (!$extension || $this->response->type($extension) === false) {
118  
-				$download = true;
119  
-			}
120  
-
121  
-			if ($cache) {
122  
-				$this->response->cache($modified, $cache);
123  
-			} else {
124  
-				$this->response->header(array(
125  
-					'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
126  
-					'Expires' => '0',
127  
-					'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
128  
-					'Pragma' => 'no-cache'
129  
-				));
130  
-			}
131  
-
132  
-			if ($download) {
133  
-				$agent = env('HTTP_USER_AGENT');
134  
-
135  
-				if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
136  
-					$contentType = 'application/octetstream';
137  
-				} elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
138  
-					$contentType = 'application/force-download';
139  
-				}
140  
-
141  
-				if (!empty($contentType)) {
142  
-					$this->response->type($contentType);
143  
-				}
144  
-				if (is_null($name)) {
145  
-					$name = $id;
146  
-				} elseif ($extension) {
147  
-					$name .= '.' . $extension;
148  
-				}
149  
-				$this->response->download($name);
150  
-				$this->response->header(array('Accept-Ranges' => 'bytes'));
151  
-
152  
-				$httpRange = env('HTTP_RANGE');
153  
-				if (isset($httpRange)) {
154  
-					list($toss, $range) = explode('=', $httpRange);
155  
-
156  
-					$size = $fileSize - 1;
157  
-					$length = $fileSize - $range;
158  
-
159  
-					$this->response->header(array(
160  
-						'Content-Length' => $length,
161  
-						'Content-Range' => 'bytes ' . $range . $size . '/' . $fileSize
162  
-					));
163  
-
164  
-					$this->response->statusCode(206);
165  
-					fseek($handle, $range);
166  
-				} else {
167  
-					$this->response->header('Content-Length', $fileSize);
168  
-				}
169  
-			} else {
170  
-				$this->response->header(array(
171  
-					'Content-Length' => $fileSize
172  
-				));
173  
-			}
174  
-			$this->_clearBuffer();
175  
-			if ($compress) {
176  
-				$this->_compressionEnabled = $this->response->compress();
177  
-			}
178  
-
179  
-			$this->response->send();
180  
-			return $this->_sendFile($handle);
  88
+			$this->response->cache($modified, $cache);
  89
+		} else {
  90
+			$this->response->disableCache();
181 91
 		}
182 92
 
183  
-		return false;
184  
-	}
185  
-
186  
-/**
187  
- * Reads out a file handle, and echos the content to the client.
188  
- *
189  
- * @param resource $handle A file handle or stream
190  
- * @return void
191  
- */
192  
-	protected function _sendFile($handle) {
193  
-		$chunkSize = 8192;
194  
-		$buffer = '';
195  
-		while (!feof($handle)) {
196  
-			if (!$this->_isActive()) {
197  
-				fclose($handle);
198  
-				return false;
199  
-			}
200  
-			set_time_limit(0);
201  
-			$buffer = fread($handle, $chunkSize);
202  
-			echo $buffer;
203  
-			if (!$this->_compressionEnabled) {
204  
-				$this->_flushBuffer();
205  
-			}
  93
+		if ($name !== null) {
  94
+			$name .= '.' . pathinfo($id, PATHINFO_EXTENSION);
206 95
 		}
207  
-		fclose($handle);
208  
-	}
209  
-
210  
-/**
211  
- * Returns true if connection is still active
212  
- *
213  
- * @return boolean
214  
- */
215  
-	protected function _isActive() {
216  
-		return connection_status() == 0 && !connection_aborted();
217  
-	}
  96
+		$this->response->file($path, compact('name', 'download'));
218 97
 
219  
-/**
220  
- * Clears the contents of the topmost output buffer and discards them
221  
- *
222  
- * @return boolean
223  
- */
224  
-	protected function _clearBuffer() {
225  
-		return @ob_end_clean();
226  
-	}
227  
-
228  
-/**
229  
- * Flushes the contents of the output buffer
230  
- *
231  
- * @return void
232  
- */
233  
-	protected function _flushBuffer() {
234  
-		@flush();
235  
-		@ob_flush();
  98
+		if ($compress) {
  99
+			$this->response->compress();
  100
+		}
  101
+		$this->response->send();
  102
+		return true;
236 103
 	}
237 104
 
238 105
 }

0 notes on commit 69eba67

Please sign in to comment.
Something went wrong with that request. Please try again.