Skip to content

Commit bf700c8

Browse files
committed
Made specifying 'extension' optional. Fixed bug where downloaded file did not have extension when 'name' was specified. Fixes #2554
1 parent 4949a87 commit bf700c8

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/Cake/Test/Case/View/MediaViewTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public function testRenderWithUnknownFileTypeIE() {
244244
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
245245
'id' => 'no_section.ini',
246246
'extension' => 'ini',
247+
'name' => 'config'
247248
);
248249
$this->MediaView->expects($this->exactly(2))
249250
->method('_isActive')
@@ -270,7 +271,7 @@ public function testRenderWithUnknownFileTypeIE() {
270271

271272
$this->MediaView->response->expects($this->once())
272273
->method('download')
273-
->with('no_section.ini');
274+
->with('config.ini');
274275

275276
$this->MediaView->response->expects($this->at(4))
276277
->method('header')
@@ -357,7 +358,7 @@ public function testConnectionAbortedOnBuffering() {
357358
*
358359
* @return void
359360
*/
360-
function testRenderUpperExtesnion() {
361+
public function testRenderUpperExtension() {
361362
$this->MediaView->viewVars = array(
362363
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS .'img' . DS,
363364
'id' => 'test_2.JPG',
@@ -376,4 +377,27 @@ function testRenderUpperExtesnion() {
376377
$this->MediaView->render();
377378
}
378379

380+
/**
381+
* Test downloading files with extension not explicitly set.
382+
*
383+
* @return void
384+
*/
385+
public function testRenderExtensionNotSet() {
386+
$this->MediaView->viewVars = array(
387+
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS .'img' . DS,
388+
'id' => 'test_2.JPG',
389+
);
390+
391+
$this->MediaView->response->expects($this->any())
392+
->method('type')
393+
->with('jpg')
394+
->will($this->returnArgument(0));
395+
396+
$this->MediaView->expects($this->at(0))
397+
->method('_isActive')
398+
->will($this->returnValue(true));
399+
400+
$this->MediaView->render();
401+
}
402+
379403
}

lib/Cake/View/MediaView.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
* - `id` The filename on the server's filesystem, including extension.
3232
* - `name` The filename that will be sent to the user, specified without the extension.
3333
* - `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
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`.
3536
* - `path` The absolute path, including the trailing / on the server's filesystem to `id`.
3637
* - `mimeType` The mime type of the file if CakeResponse doesn't know about it.
38+
* Must be an associative array with extension as key and mime type as value eg. array('ini' => 'text/plain')
3739
*
3840
* ### Usage
3941
*
@@ -113,7 +115,11 @@ public function render($view = null, $layout = null) {
113115
$this->response->type($mimeType);
114116
}
115117

116-
if (isset($extension) && $this->_isActive()) {
118+
if (!isset($extension)) {
119+
$extension = pathinfo($id, PATHINFO_EXTENSION);
120+
}
121+
122+
if ($this->_isActive()) {
117123
$extension = strtolower($extension);
118124
$chunkSize = 8192;
119125
$buffer = '';
@@ -128,7 +134,7 @@ public function render($view = null, $layout = null) {
128134
} else {
129135
$modified = time();
130136
}
131-
if ($this->response->type($extension) === false) {
137+
if (!$extension || $this->response->type($extension) === false) {
132138
$download = true;
133139
}
134140

@@ -157,6 +163,8 @@ public function render($view = null, $layout = null) {
157163
}
158164
if (is_null($name)) {
159165
$name = $id;
166+
} elseif ($extension) {
167+
$name .= '.' . $extension;
160168
}
161169
$this->response->download($name);
162170
$this->response->header(array('Accept-Ranges' => 'bytes'));

0 commit comments

Comments
 (0)