1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sulao\Flysystem\BaiduBos; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Config; |
6
|
|
|
|
7
|
|
|
trait UtilTrait |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param string $directory |
11
|
|
|
* @param false $recursive |
12
|
|
|
* |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
3 |
|
public function buildListDirOptions( |
16
|
|
|
string $directory = '', |
17
|
|
|
bool $recursive = false |
18
|
|
|
): array { |
19
|
3 |
|
$options = []; |
20
|
|
|
|
21
|
3 |
|
if (!$recursive) { |
22
|
3 |
|
$options['query']['delimiter'] = '/'; |
23
|
|
|
} |
24
|
|
|
|
25
|
3 |
|
$directory = trim($directory, '/'); |
26
|
3 |
|
if ($directory !== '') { |
27
|
3 |
|
$directory .= '/'; |
28
|
3 |
|
$options['query']['prefix'] = $directory; |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
return $options; |
32
|
|
|
} |
33
|
|
|
/** |
34
|
|
|
* Extract options from config |
35
|
|
|
* |
36
|
|
|
* @param Config $config |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
3 |
|
protected function extractOptions(Config $config): array |
41
|
|
|
{ |
42
|
3 |
|
$options = []; |
43
|
|
|
|
44
|
3 |
|
foreach (['headers', 'query', 'body', 'request', 'authorize'] as $key) { |
45
|
3 |
|
if ($config->get($key)) { |
46
|
1 |
|
$options[$key] = $config->get($key); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
return $options; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Normalize the object meta array. |
55
|
|
|
* |
56
|
|
|
* @param array $meta |
57
|
|
|
* @param string $path |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
1 |
|
protected function normalizeMeta(array $meta, string $path): array |
62
|
|
|
{ |
63
|
1 |
|
$result = pathinfo($path); |
64
|
|
|
|
65
|
1 |
|
if (isset($meta['Last-Modified'])) { |
66
|
1 |
|
$result['lastModified'] = strtotime($meta['Last-Modified']); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
if (isset($meta['Content-Length'])) { |
70
|
1 |
|
$result['fileSize'] = $meta['Content-Length']; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
if (isset($meta['Content-Type'])) { |
74
|
1 |
|
$result['mimeType'] = $meta['Content-Type']; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return array_merge($result, ['type' => 'file']); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Normalize the content from list contents of dir. |
82
|
|
|
* |
83
|
|
|
* @param array $content |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
2 |
|
protected function normalizeContent(array $content): array |
88
|
|
|
{ |
89
|
2 |
|
$return = []; |
90
|
|
|
|
91
|
2 |
|
if (str_ends_with($content['key'], '/')) { |
92
|
2 |
|
$return['type'] = 'dir'; |
93
|
2 |
|
$return['path'] = $content['key']; |
94
|
|
|
} else { |
95
|
1 |
|
$return['type'] = 'file'; |
96
|
1 |
|
$return['path'] = $content['key']; |
97
|
1 |
|
$return['size'] = $content['size']; |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
if (isset($content['lastModified'])) { |
101
|
2 |
|
$return['timestamp'] = strtotime($content['lastModified']); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
return $return + pathinfo($content['key']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Extract permissions from acl |
109
|
|
|
* |
110
|
|
|
* @param array $acl |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
1 |
|
protected function extractPermissions(array $acl): array |
115
|
|
|
{ |
116
|
1 |
|
$permissions = []; |
117
|
1 |
|
foreach ($acl as $row) { |
118
|
1 |
|
$ids = array_column($row['grantee'], 'id'); |
119
|
1 |
|
if (in_array('*', $ids)) { |
120
|
1 |
|
$permissions = $row['permission']; |
121
|
1 |
|
break; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return $permissions; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|