|
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
|
9 |
|
public static function getHeaderParams($headers, $otherParams = array()) |
|
16
|
|
|
{ |
|
17
|
9 |
|
$params = []; |
|
18
|
9 |
|
$otherParams = array_map('strtolower', $otherParams); |
|
19
|
9 |
|
foreach ($headers as $header => $value) { |
|
20
|
9 |
|
$header = strtolower($header); |
|
21
|
9 |
|
if (strpos($header, 'x-upyun-') !== false) { |
|
22
|
5 |
|
$params[$header] = $value[0]; |
|
23
|
9 |
|
} else if (in_array($header, $otherParams)) { |
|
24
|
1 |
|
$params[$header] = $value[0]; |
|
25
|
1 |
|
} |
|
26
|
9 |
|
} |
|
27
|
9 |
|
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
|
14 |
|
public static function encodeURI($url) |
|
76
|
|
|
{ |
|
77
|
|
|
$unescaped = array( |
|
78
|
14 |
|
'%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~', |
|
79
|
14 |
|
'%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')' |
|
80
|
14 |
|
); |
|
81
|
|
|
$reserved = array( |
|
82
|
14 |
|
'%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':', |
|
83
|
14 |
|
'%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$' |
|
84
|
14 |
|
); |
|
85
|
|
|
$score = array( |
|
86
|
|
|
'%23'=>'#' |
|
87
|
14 |
|
); |
|
88
|
14 |
|
return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|