Skip to content

Commit

Permalink
Made specifying 'extension' optional. Fixed bug where downloaded file…
Browse files Browse the repository at this point in the history
… did not have extension when 'name' was specified. Fixes  #2554
  • Loading branch information
ADmad committed Feb 7, 2012
1 parent 4949a87 commit bf700c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
28 changes: 26 additions & 2 deletions lib/Cake/Test/Case/View/MediaViewTest.php
Expand Up @@ -244,6 +244,7 @@ public function testRenderWithUnknownFileTypeIE() {
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
'id' => 'no_section.ini',
'extension' => 'ini',
'name' => 'config'
);
$this->MediaView->expects($this->exactly(2))
->method('_isActive')
Expand All @@ -270,7 +271,7 @@ public function testRenderWithUnknownFileTypeIE() {

$this->MediaView->response->expects($this->once())
->method('download')
->with('no_section.ini');
->with('config.ini');

$this->MediaView->response->expects($this->at(4))
->method('header')
Expand Down Expand Up @@ -357,7 +358,7 @@ public function testConnectionAbortedOnBuffering() {
*
* @return void
*/
function testRenderUpperExtesnion() {
public function testRenderUpperExtension() {
$this->MediaView->viewVars = array(
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS .'img' . DS,
'id' => 'test_2.JPG',
Expand All @@ -376,4 +377,27 @@ function testRenderUpperExtesnion() {
$this->MediaView->render();
}

/**
* Test downloading files with extension not explicitly set.
*
* @return void
*/
public function testRenderExtensionNotSet() {
$this->MediaView->viewVars = array(
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS .'img' . DS,
'id' => 'test_2.JPG',
);

$this->MediaView->response->expects($this->any())
->method('type')
->with('jpg')
->will($this->returnArgument(0));

$this->MediaView->expects($this->at(0))
->method('_isActive')
->will($this->returnValue(true));

$this->MediaView->render();
}

}
14 changes: 11 additions & 3 deletions lib/Cake/View/MediaView.php
Expand Up @@ -31,9 +31,11 @@
* - `id` The filename on the server's filesystem, including extension.
* - `name` The filename that will be sent to the user, specified without the extension.
* - `download` Set to true to set a `Content-Disposition` header. This is ideal for file downloads.
* - `extension` The extension of the file being served. This is used to set the mimetype
* - `extension` The extension of the file being served. This is used to set the mimetype.
* If not provided its extracted from filename provided as `id`.
* - `path` The absolute path, including the trailing / on the server's filesystem to `id`.
* - `mimeType` The mime type of the file if CakeResponse doesn't know about it.
* Must be an associative array with extension as key and mime type as value eg. array('ini' => 'text/plain')
*
* ### Usage
*
Expand Down Expand Up @@ -113,7 +115,11 @@ public function render($view = null, $layout = null) {
$this->response->type($mimeType);
}

if (isset($extension) && $this->_isActive()) {
if (!isset($extension)) {
$extension = pathinfo($id, PATHINFO_EXTENSION);
}

if ($this->_isActive()) {
$extension = strtolower($extension);
$chunkSize = 8192;
$buffer = '';
Expand All @@ -128,7 +134,7 @@ public function render($view = null, $layout = null) {
} else {
$modified = time();
}
if ($this->response->type($extension) === false) {
if (!$extension || $this->response->type($extension) === false) {
$download = true;
}

Expand Down Expand Up @@ -157,6 +163,8 @@ public function render($view = null, $layout = null) {
}
if (is_null($name)) {
$name = $id;
} elseif ($extension) {
$name .= '.' . $extension;
}
$this->response->download($name);
$this->response->header(array('Accept-Ranges' => 'bytes'));
Expand Down

0 comments on commit bf700c8

Please sign in to comment.