|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: liu21st <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think\response; |
|
14
|
|
|
|
|
15
|
|
|
use think\Exception; |
|
16
|
|
|
use think\Response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* File Response |
|
20
|
|
|
*/ |
|
21
|
|
|
class File extends Response |
|
22
|
|
|
{ |
|
23
|
|
|
protected $expire = 360; |
|
24
|
|
|
protected $name; |
|
25
|
|
|
protected $mimeType; |
|
26
|
|
|
protected $isContent = false; |
|
27
|
|
|
protected $force = true; |
|
28
|
|
|
protected $isSwoole = false; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($data = '', int $code = 200) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->init($data, $code); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 处理数据 |
|
37
|
|
|
* @access protected |
|
38
|
|
|
* @param mixed $data 要处理的数据 |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function output($data) |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$this->isContent && !is_file($data)) { |
|
45
|
|
|
throw new Exception('file not exists:' . $data); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
while (ob_get_level() > 0) { |
|
49
|
|
|
ob_end_clean(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!empty($this->name)) { |
|
53
|
|
|
$name = $this->name; |
|
54
|
|
|
} else { |
|
55
|
|
|
$name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : ''; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($this->isContent) { |
|
59
|
|
|
$mimeType = $this->mimeType; |
|
60
|
|
|
$size = strlen($data); |
|
61
|
|
|
} else { |
|
62
|
|
|
$mimeType = $this->getMimeType($data); |
|
63
|
|
|
$size = filesize($data); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->header['Pragma'] = 'public'; |
|
67
|
|
|
$this->header['Content-Type'] = $mimeType ?: 'application/octet-stream'; |
|
68
|
|
|
$this->header['Cache-control'] = 'max-age=' . $this->expire; |
|
69
|
|
|
$this->header['Content-Disposition'] = ($this->force ? 'attachment; ' : '') . 'filename="' . $name . '"'; |
|
70
|
|
|
if (!$this->getIsSwoole()) { |
|
71
|
|
|
$this->header['Content-Length'] = $size; |
|
72
|
|
|
} |
|
73
|
|
|
$this->header['Content-Transfer-Encoding'] = 'binary'; |
|
74
|
|
|
$this->header['Expires'] = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT'; |
|
75
|
|
|
|
|
76
|
|
|
$this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT'); |
|
77
|
|
|
|
|
78
|
|
|
return $this->isContent ? $data : file_get_contents($data); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* 设置是否为内容 必须配合mimeType方法使用 |
|
83
|
|
|
* @access public |
|
84
|
|
|
* @param bool $content |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
|
|
public function isContent(bool $content = true) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->isContent = $content; |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* 设置有效期 |
|
95
|
|
|
* @access public |
|
96
|
|
|
* @param integer $expire 有效期 |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
public function expire(int $expire) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->expire = $expire; |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* 设置文件类型 |
|
107
|
|
|
* @access public |
|
108
|
|
|
* @param string $filename 文件名 |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function mimeType(string $mimeType) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->mimeType = $mimeType; |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* 设置文件强制下载 |
|
119
|
|
|
* @access public |
|
120
|
|
|
* @param bool $force 强制浏览器下载 |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
|
|
public function force(bool $force) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->force = $force; |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* 设置是否为 Swoole 环境下使用 |
|
131
|
|
|
* @access public |
|
132
|
|
|
* @param bool $isSwoole |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
|
|
public function isSwoole(bool $isSwoole) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->isSwoole = $isSwoole; |
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* 获取是否为 Swoole 环境下使用 |
|
143
|
|
|
* @access protected |
|
144
|
|
|
* @return bool |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getIsSwoole(): bool |
|
147
|
|
|
{ |
|
148
|
|
|
if ($this->isSwoole === true && defined('SWOOLE_VERSION_ID') && SWOOLE_VERSION_ID >= 40603) { |
|
|
|
|
|
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* 获取文件类型信息 |
|
157
|
|
|
* @access protected |
|
158
|
|
|
* @param string $filename 文件名 |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function getMimeType(string $filename): string |
|
162
|
|
|
{ |
|
163
|
|
|
if (!empty($this->mimeType)) { |
|
164
|
|
|
return $this->mimeType; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
168
|
|
|
|
|
169
|
|
|
return finfo_file($finfo, $filename); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* 设置下载文件的显示名称 |
|
174
|
|
|
* @access public |
|
175
|
|
|
* @param string $filename 文件名 |
|
176
|
|
|
* @param bool $extension 后缀自动识别 |
|
177
|
|
|
* @return $this |
|
178
|
|
|
*/ |
|
179
|
|
|
public function name(string $filename, bool $extension = true) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->name = $filename; |
|
182
|
|
|
|
|
183
|
|
|
if ($extension && false === strpos($filename, '.')) { |
|
184
|
|
|
$this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION); |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|