1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlphaSnow\AliyunOss; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\AdapterInterface; |
6
|
|
|
use League\Flysystem\Config; |
7
|
|
|
use League\Flysystem\Util; |
8
|
|
|
use OSS\Core\OssException; |
9
|
|
|
use OSS\OssClient; |
10
|
|
|
|
11
|
|
|
trait AliyunOssAdapterTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
2 |
View Code Duplication |
public function write($path, $contents, Config $config) |
|
|
|
|
17
|
|
|
{ |
18
|
2 |
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
19
|
2 |
|
$options = $this->getOptions([], $config); |
|
|
|
|
20
|
|
|
|
21
|
2 |
|
if (!isset($options[OssClient::OSS_LENGTH])) { |
22
|
2 |
|
$options[OssClient::OSS_LENGTH] = Util::contentSize($contents); |
23
|
|
|
} |
24
|
2 |
|
if (!isset($options[OssClient::OSS_CONTENT_TYPE])) { |
25
|
2 |
|
$options[OssClient::OSS_CONTENT_TYPE] = Util::guessMimeType($path, $contents); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
try { |
29
|
|
|
// https://help.aliyun.com/document_detail/31978.html |
30
|
2 |
|
$this->client->putObject($this->bucket, $object, $contents, $options); |
|
|
|
|
31
|
|
|
} catch (OssException $e) { |
32
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
return $this->normalizeResponse($options, $path); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
2 |
|
public function writeStream($path, $resource, Config $config) |
43
|
|
|
{ |
44
|
2 |
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
45
|
2 |
|
$options = $this->getOptions([], $config); |
|
|
|
|
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
// https://help.aliyun.com/document_detail/31959.html |
49
|
2 |
|
$this->client->uploadStream($this->bucket, $object, $resource, $options); |
50
|
|
|
} catch (OssException $e) { |
51
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
return $this->normalizeResponse($options, $path); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
1 |
View Code Duplication |
public function writeFile($path, $filePath, Config $config) |
|
|
|
|
62
|
|
|
{ |
63
|
1 |
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
64
|
1 |
|
$options = $this->getOptions([], $config); |
|
|
|
|
65
|
|
|
|
66
|
1 |
|
if (!isset($options[OssClient::OSS_CONTENT_TYPE])) { |
67
|
1 |
|
$options[OssClient::OSS_CHECK_MD5] = true; |
68
|
|
|
} |
69
|
1 |
|
if (!isset($options[OssClient::OSS_CONTENT_TYPE])) { |
70
|
1 |
|
$options[OssClient::OSS_CONTENT_TYPE] = Util::guessMimeType($path, ''); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
try { |
74
|
1 |
|
$this->client->uploadFile($this->bucket, $object, $filePath, $options); |
75
|
|
|
} catch (OssException $e) { |
76
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return $this->normalizeResponse($options, $path); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function update($path, $contents, Config $config) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
if (!$config->has('visibility') && !$config->has('ACL')) { |
89
|
|
|
// 新文件未配置权限情况下,继承旧文件权限 |
90
|
|
|
$config->set(AliyunOssUtil::$metaMap['ACL'], $this->getObjectACL($path)); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
// 允许覆盖同名文件 |
93
|
|
|
$config->set('x-oss-forbid-overwrite', 'false'); |
94
|
|
|
|
95
|
|
|
return $this->write($path, $contents, $config); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function updateStream($path, $resource, Config $config) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if (!$config->has('visibility') && !$config->has('ACL')) { |
104
|
|
|
// 新文件未配置权限情况下,继承旧文件权限 |
105
|
|
|
$config->set(AliyunOssUtil::$metaMap['ACL'], $this->getObjectACL($path)); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
// 允许覆盖同名文件 |
108
|
|
|
$config->set('x-oss-forbid-overwrite', 'false'); |
109
|
|
|
|
110
|
|
|
return $this->writeStream($path, $resource, $config); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function rename($path, $newpath) |
117
|
|
|
{ |
118
|
|
|
if (!$this->copy($path, $newpath)) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->delete($path); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function copy($path, $newpath) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
131
|
|
|
$newObject = $this->applyPathPrefix($newpath); |
|
|
|
|
132
|
|
|
|
133
|
|
|
try { |
134
|
|
|
$this->client->copyObject($this->bucket, $object, $this->bucket, $newObject); |
135
|
|
|
} catch (OssException $e) { |
136
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function delete($path) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$bucket = $this->bucket; |
149
|
|
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
150
|
|
|
|
151
|
|
|
try { |
152
|
|
|
$this->client->deleteObject($bucket, $object); |
153
|
|
|
} catch (OssException $e) { |
154
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return !$this->has($path); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function deleteDir($dirname) |
165
|
|
|
{ |
166
|
|
|
$dirname = rtrim($this->applyPathPrefix($dirname), '/') . '/'; |
|
|
|
|
167
|
|
|
$dirObjects = $this->listDirObjects($dirname, true); |
|
|
|
|
168
|
|
|
|
169
|
|
|
if (count($dirObjects['objects']) > 0) { |
170
|
|
|
$objects = []; |
171
|
|
|
foreach ($dirObjects['objects'] as $object) { |
172
|
|
|
$objects[] = $object['Key']; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
try { |
176
|
|
|
$this->client->deleteObjects($this->bucket, $objects); |
177
|
|
|
} catch (OssException $e) { |
178
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
try { |
184
|
|
|
$this->client->deleteObject($this->bucket, $dirname); |
185
|
|
|
} catch (OssException $e) { |
186
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function createDir($dirname, Config $config) |
197
|
|
|
{ |
198
|
|
|
$object = $this->applyPathPrefix($dirname); |
|
|
|
|
199
|
|
|
$options = $this->getOptionsFromConfig($config); |
|
|
|
|
200
|
|
|
|
201
|
|
|
try { |
202
|
|
|
$this->client->createObjectDir($this->bucket, $object, $options); |
203
|
|
|
} catch (OssException $e) { |
204
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return ['path' => $dirname, 'type' => 'dir']; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
|
|
public function setVisibility($path, $visibility) |
215
|
|
|
{ |
216
|
|
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
217
|
|
|
$acl = ($visibility === AdapterInterface::VISIBILITY_PUBLIC) ? OssClient::OSS_ACL_TYPE_PUBLIC_READ : OssClient::OSS_ACL_TYPE_PRIVATE; |
218
|
|
|
|
219
|
|
|
$this->client->putObjectAcl($this->bucket, $object, $acl); |
220
|
|
|
|
221
|
|
|
return compact('visibility'); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.