Skip to content

Commit 731208e

Browse files
committed
feat add getMimetype method
1 parent fe1dc22 commit 731208e

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/Upyun/Upyun.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,28 @@ public function has($path)
171171
* 获取云存储文件/目录的基本信息
172172
*
173173
* @param string $path 云存储的文件路径
174+
* @param array $otherHeaders 设置了后,方法将返回其他 http header 中的信息,默认为空
174175
*
175176
* @return array 返回一个数组,包含以下 key
176177
* - `x-upyun-file-type` 当 $path 是目录时,值为 *folder*,当 $path 是文件时,值为 *file*,
177178
* - `x-upyun-file-size` 文件大小
178179
* - `x-upyun-file-date` 文件的创建时间
179180
*/
180-
public function info($path)
181+
public function info($path, $otherHeaders = array())
181182
{
182183
$req = new Rest($this->config);
183184
$response = $req->request('HEAD', $path)
184185
->send();
185-
return Util::getHeaderParams($response->getHeaders());
186+
return Util::getHeaderParams($response->getHeaders(), $otherHeaders);
187+
}
188+
189+
public function getMimetype($path)
190+
{
191+
$params = $this->info($path, array('content-type'));
192+
if (isset($params['content-type'])) {
193+
return explode(';', $params['content-type'])[0];
194+
}
195+
return '';
186196
}
187197

188198
/**

src/Upyun/Util.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ public static function trim($str)
1212
}
1313
}
1414

15-
public static function getHeaderParams($headers)
15+
public static function getHeaderParams($headers, $otherParams = array())
1616
{
1717
$params = [];
18+
$otherParams = array_map('strtolower', $otherParams);
1819
foreach ($headers as $header => $value) {
1920
$header = strtolower($header);
2021
if (strpos($header, 'x-upyun-') !== false) {
2122
$params[$header] = $value[0];
23+
} else if (in_array($header, $otherParams)) {
24+
$params[$header] = $value[0];
2225
}
2326
}
2427
return $params;

tests/UpyunTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ public function testInfo()
152152
$this->assertEquals($info['x-upyun-file-size'], 19);
153153
}
154154

155+
/**
156+
* @depends testInfo
157+
*/
158+
public function testGetMimetype()
159+
{
160+
$type = self::$upyun->getMimetype('test-info.txt');
161+
$this->assertEquals($type, 'text/plain');
162+
}
163+
155164
/**
156165
*/
157166
public function testCreateDir()

0 commit comments

Comments
 (0)