forked from cakephp/cakephp
/
MediaView.php
102 lines (94 loc) · 3.1 KB
/
MediaView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* Methods to display or download any type of file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View
* @since CakePHP(tm) v 1.2.0.5714
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @deprecated Deprecated since version 2.3, use Cake\Network\Response::file() instead
*/
namespace Cake\View;
use Cake\Core\Configure;
use Cake\Error;
use Cake\Network\Response;
/**
* Media View provides a custom view implementation for sending files to visitors. Its great
* for making the response of a controller action be a file that is saved somewhere on the filesystem.
*
* An example use comes from the CakePHP internals. MediaView is used to serve plugin and theme assets,
* as they are not normally accessible from an application's webroot. Unlike other views, MediaView
* uses several viewVars that have special meaning:
*
* - `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.
* - `path` The absolute path, including the trailing / on the server's filesystem to `id`.
* - `mimeType` The mime type of the file if Response 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
*
* {{{
* class ExampleController extends AppController {
* public function download() {
* $this->viewClass = 'Media';
* $params = array(
* 'id' => 'example.zip',
* 'name' => 'example',
* 'download' => true,
* 'extension' => 'zip',
* 'path' => APP . 'files/'
* );
* $this->set($params);
* }
* }
* }}}
*
* @package Cake.View
*/
class MediaView extends View {
/**
* Display or download the given file
*
* @param string $view Not used
* @param string $layout Not used
* @return boolean
*/
public function render($view = null, $layout = null) {
$name = $download = $id = $modified = $path = $cache = $mimeType = $compress = null;
extract($this->viewVars, EXTR_OVERWRITE);
$path = $path . $id;
if (is_array($mimeType)) {
$this->response->type($mimeType);
}
if ($cache) {
if (!empty($modified) && !is_numeric($modified)) {
$modified = strtotime($modified, time());
} else {
$modified = time();
}
$this->response->cache($modified, $cache);
} else {
$this->response->disableCache();
}
if ($name !== null) {
$name .= '.' . pathinfo($id, PATHINFO_EXTENSION);
}
$this->response->file($path, compact('name', 'download'));
if ($compress) {
$this->response->compress();
}
$this->response->send();
return true;
}
}