MediaView now forces download of unknown file types. Closes #140

This commit is contained in:
ADmad 2011-02-27 06:31:15 +05:30
parent 2414ad8c8f
commit d999650d5e
2 changed files with 70 additions and 19 deletions

View file

@ -89,13 +89,9 @@ class MediaView extends View {
* @return mixed
*/
function render() {
$name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = $compress = null;
$name = $download = $extension = $id = $modified = $path = $cache = $mimeType = $compress = null;
extract($this->viewVars, EXTR_OVERWRITE);
if ($size) {
$id = $id . '_' . $size;
}
if (is_dir($path)) {
$path = $path . $id;
} else {
@ -109,15 +105,12 @@ class MediaView extends View {
throw new NotFoundException('The requested file was not found');
}
if (is_null($name)) {
$name = $id;
}
if (is_array($mimeType)) {
$this->response->type($mimeType);
}
if (isset($extension) && $this->response->type($extension) && $this->_isActive()) {
if (isset($extension) && $this->_isActive()) {
$extension = strtolower($extension);
$chunkSize = 8192;
$buffer = '';
$fileSize = @filesize($path);
@ -131,6 +124,9 @@ class MediaView extends View {
} else {
$modified = time();
}
if ($this->response->type($extension) === false) {
$download = true;
}
if ($cache) {
$this->response->cache($modified, $cache);
@ -155,7 +151,10 @@ class MediaView extends View {
if (!empty($contentType)) {
$this->response->type($contentType);
}
$this->response->download($name . '.' . $extension);
if (is_null($name)) {
$name = $id;
}
$this->response->download($name);
$this->response->header(array('Accept-Ranges' => 'bytes'));
$httpRange = env('HTTP_RANGE');
@ -176,7 +175,6 @@ class MediaView extends View {
$this->response->header('Content-Length', $fileSize);
}
} else {
$this->response->type($extension);
$this->response->header(array(
'Content-Length' => $fileSize
));
@ -237,7 +235,7 @@ class MediaView extends View {
/**
* Flushes the contents of the output buffer
*
*
* @return void
*/
protected function _flushBuffer() {

View file

@ -77,7 +77,7 @@ class MediaViewTest extends CakeTestCase {
->method('_isActive')
->will($this->returnValue(true));
$this->MediaView->response->expects($this->exactly(2))
$this->MediaView->response->expects($this->exactly(1))
->method('type')
->with('css')
->will($this->returnArgument(0));
@ -91,7 +91,7 @@ class MediaViewTest extends CakeTestCase {
'Pragma' => 'no-cache'
));
$this->MediaView->response->expects($this->at(3))
$this->MediaView->response->expects($this->at(2))
->method('header')
->with(array(
'Content-Length' => 31
@ -107,6 +107,61 @@ class MediaViewTest extends CakeTestCase {
$this->assertTrue($result !== false);
}
/**
* testRenderWithUnknownFileType method
*
* @access public
* @return void
*/
function testRenderWithUnknownFileType() {
$this->MediaView->viewVars = array(
'path' => TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'config' . DS,
'id' => 'no_section.ini',
'extension' => 'ini',
);
$this->MediaView->expects($this->exactly(2))
->method('_isActive')
->will($this->returnValue(true));
$this->MediaView->response->expects($this->exactly(1))
->method('type')
->with('ini')
->will($this->returnValue(false));
$this->MediaView->response->expects($this->at(1))
->method('header')
->with(array(
'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
'Expires' => '0',
'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'no-cache'
));
$this->MediaView->response->expects($this->once())
->method('download')
->with('no_section.ini');
$this->MediaView->response->expects($this->at(3))
->method('header')
->with(array(
'Accept-Ranges' => 'bytes'
));
$this->MediaView->response->expects($this->at(4))
->method('header')
->with('Content-Length', 35);
$this->MediaView->response->expects($this->once())->method('send');
$this->MediaView->expects($this->once())->method('_clearBuffer');
$this->MediaView->expects($this->once())->method('_flushBuffer');
ob_start();
$result = $this->MediaView->render();
$output = ob_get_clean();
$this->assertEqual("some_key = some_value\nbool_key = 1\n", $output);
$this->assertTrue($result !== false);
}
/**
* testConnectionAborted method
*
@ -124,10 +179,8 @@ class MediaViewTest extends CakeTestCase {
->method('_isActive')
->will($this->returnValue(false));
$this->MediaView->response->expects($this->once())
->method('type')
->with('css')
->will($this->returnArgument(0));
$this->MediaView->response->expects($this->never())
->method('type');
$result = $this->MediaView->render();
$this->assertFalse($result);