1
|
|
|
<?php |
2
|
|
|
namespace Upyun; |
3
|
|
|
|
4
|
|
|
class Util |
5
|
|
|
{ |
6
|
|
|
public static function trim($str) |
7
|
|
|
{ |
8
|
|
|
if (is_array($str)) { |
9
|
|
|
return array_map(array('Util', 'trim'), $str); |
10
|
|
|
} else { |
11
|
|
|
return trim($str); |
12
|
|
|
} |
13
|
|
|
} |
14
|
|
|
|
15
|
14 |
|
public static function getHeaderParams($headers, $otherParams = array()) |
16
|
|
|
{ |
17
|
14 |
|
$params = []; |
18
|
14 |
|
$otherParams = array_map('strtolower', $otherParams); |
19
|
14 |
|
foreach ($headers as $header => $value) { |
20
|
14 |
|
$header = strtolower($header); |
21
|
14 |
|
if (strpos($header, 'x-upyun-') !== false) { |
22
|
14 |
|
$params[$header] = $value[0]; |
23
|
14 |
|
} else if (in_array($header, $otherParams)) { |
24
|
1 |
|
$params[$header] = $value[0]; |
25
|
1 |
|
} |
26
|
14 |
|
} |
27
|
14 |
|
return $params; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public static function parseDir($body) |
31
|
|
|
{ |
32
|
1 |
|
$files = array(); |
33
|
1 |
|
if (!$body) { |
34
|
1 |
|
return array(); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
$lines = explode("\n", $body); |
38
|
1 |
|
foreach ($lines as $line) { |
39
|
1 |
|
$file = []; |
40
|
1 |
|
list($file['name'], $file['type'], $file['size'], $file['time']) = explode("\t", $line, 4); |
41
|
1 |
|
$files[] = $file; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
1 |
|
return $files; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
public static function base64Json($params) |
48
|
|
|
{ |
49
|
2 |
|
return base64_encode(json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function stringifyHeaders($headers) |
53
|
|
|
{ |
54
|
|
|
$return = array(); |
55
|
|
|
foreach ($headers as $key => $value) { |
56
|
|
|
$return[] = "$key: $value"; |
57
|
|
|
} |
58
|
|
|
return $return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function md5Hash($resource) |
62
|
|
|
{ |
63
|
|
|
rewind($resource); |
64
|
|
|
$ctx = hash_init('md5'); |
65
|
|
|
hash_update_stream($ctx, $resource); |
66
|
|
|
$md5 = hash_final($ctx); |
67
|
|
|
return $md5; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* GuzzleHttp\Psr\Uri use `parse_url`,not good for utf-8, |
72
|
|
|
* So should `encodeURI` first, before `new Psr7\Request` |
73
|
|
|
* @see http://stackoverflow.com/questions/4929584/encodeuri-in-php |
74
|
|
|
*/ |
75
|
19 |
|
public static function encodeURI($url) |
76
|
|
|
{ |
77
|
|
|
$unescaped = array( |
78
|
19 |
|
'%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~', |
79
|
19 |
|
'%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')' |
80
|
19 |
|
); |
81
|
|
|
$reserved = array( |
82
|
19 |
|
'%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':', |
83
|
19 |
|
'%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$' |
84
|
19 |
|
); |
85
|
|
|
$score = array( |
86
|
|
|
'%23'=>'#' |
87
|
19 |
|
); |
88
|
19 |
|
return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score)); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
public static function isSuccess($code) |
92
|
|
|
{ |
93
|
2 |
|
return $code >= 200 && $code < 300; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|