1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Freyo\Flysystem\QcloudCOSv4; |
4
|
|
|
|
5
|
|
|
use Freyo\Flysystem\QcloudCOSv4\Client\Conf; |
6
|
|
|
use Freyo\Flysystem\QcloudCOSv4\Client\Cosapi; |
7
|
|
|
use Freyo\Flysystem\QcloudCOSv4\Exceptions\RuntimeException; |
8
|
|
|
use League\Flysystem\Adapter\AbstractAdapter; |
9
|
|
|
use League\Flysystem\AdapterInterface; |
10
|
|
|
use League\Flysystem\Config; |
11
|
|
|
use League\Flysystem\Util; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Adapter. |
15
|
|
|
*/ |
16
|
|
|
class Adapter extends AbstractAdapter |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var |
20
|
|
|
*/ |
21
|
|
|
protected $bucket; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
protected $debug; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Adapter constructor. |
30
|
|
|
* |
31
|
|
|
* @param $config |
32
|
|
|
*/ |
33
|
|
|
public function __construct($config) |
34
|
|
|
{ |
35
|
|
|
Conf::setAppId($config['app_id']); |
36
|
|
|
Conf::setSecretId($config['secret_id']); |
37
|
|
|
Conf::setSecretKey($config['secret_key']); |
38
|
|
|
|
39
|
|
|
$this->bucket = $config['bucket']; |
40
|
|
|
$this->debug = $config['debug']; |
41
|
|
|
|
42
|
|
|
$this->setPathPrefix($config['protocol'].'://'.$config['domain'].'/'); |
43
|
|
|
|
44
|
|
|
Cosapi::setTimeout($config['timeout']); |
45
|
|
|
Cosapi::setRegion($config['region']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
15 |
|
public function getBucket() |
52
|
|
|
{ |
53
|
15 |
|
return $this->bucket; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $path |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
public function getUrl($path) |
62
|
|
|
{ |
63
|
1 |
|
return $this->applyPathPrefix($path); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $path |
68
|
|
|
* @param string $contents |
69
|
|
|
* @param Config $config |
70
|
|
|
* |
71
|
|
|
* @throws RuntimeException |
72
|
|
|
* |
73
|
|
|
* @return array|bool |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function write($path, $contents, Config $config) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$temporaryPath = $this->createTemporaryFile($contents); |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$response = Cosapi::upload($this->getBucket(), $temporaryPath, $path, |
|
|
|
|
81
|
|
|
null, null, $config->get('insertOnly', 1)); |
82
|
|
|
|
83
|
|
|
$response = $this->normalizeResponse($response); |
84
|
|
|
|
85
|
|
|
if (false === $response) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->setContentType($path, $contents); |
90
|
|
|
} catch (RuntimeException $exception) { |
91
|
|
|
throw $exception; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $response; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $path |
99
|
|
|
* @param resource $resource |
100
|
|
|
* @param Config $config |
101
|
|
|
* |
102
|
|
|
* @throws RuntimeException |
103
|
|
|
* |
104
|
|
|
* @return array|bool |
105
|
|
|
*/ |
106
|
1 |
View Code Duplication |
public function writeStream($path, $resource, Config $config) |
|
|
|
|
107
|
|
|
{ |
108
|
1 |
|
$uri = stream_get_meta_data($resource)['uri']; |
109
|
|
|
|
110
|
1 |
|
$response = Cosapi::upload($this->getBucket(), $uri, $path, |
111
|
1 |
|
null, null, $config->get('insertOnly', 1)); |
112
|
|
|
|
113
|
1 |
|
$response = $this->normalizeResponse($response); |
114
|
|
|
|
115
|
|
|
if (false === $response) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->setContentType($path, stream_get_contents($resource)); |
120
|
|
|
|
121
|
|
|
return $response; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $path |
126
|
|
|
* @param string $contents |
127
|
|
|
* @param Config $config |
128
|
|
|
* |
129
|
|
|
* @throws RuntimeException |
130
|
|
|
* |
131
|
|
|
* @return array|bool |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function update($path, $contents, Config $config) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$temporaryPath = $this->createTemporaryFile($contents); |
136
|
|
|
|
137
|
|
|
try { |
138
|
|
|
$response = Cosapi::upload($this->getBucket(), $temporaryPath, $path, |
|
|
|
|
139
|
|
|
null, null, $config->get('insertOnly', 0)); |
140
|
|
|
|
141
|
|
|
$response = $this->normalizeResponse($response); |
142
|
|
|
|
143
|
|
|
if (false === $response) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->setContentType($path, $contents); |
148
|
|
|
} catch (RuntimeException $exception) { |
149
|
|
|
throw $exception; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $response; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $path |
157
|
|
|
* @param resource $resource |
158
|
|
|
* @param Config $config |
159
|
|
|
* |
160
|
|
|
* @throws RuntimeException |
161
|
|
|
* |
162
|
|
|
* @return array|bool |
163
|
|
|
*/ |
164
|
1 |
View Code Duplication |
public function updateStream($path, $resource, Config $config) |
|
|
|
|
165
|
|
|
{ |
166
|
1 |
|
$uri = stream_get_meta_data($resource)['uri']; |
167
|
|
|
|
168
|
1 |
|
$response = Cosapi::upload($this->getBucket(), $uri, $path, |
169
|
1 |
|
null, null, $config->get('insertOnly', 0)); |
170
|
|
|
|
171
|
1 |
|
$response = $this->normalizeResponse($response); |
172
|
|
|
|
173
|
|
|
if (false === $response) { |
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->setContentType($path, stream_get_contents($resource)); |
178
|
|
|
|
179
|
|
|
return $response; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $path |
184
|
|
|
* @param string $newpath |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
1 |
|
public function rename($path, $newpath) |
189
|
|
|
{ |
190
|
1 |
|
return (bool) $this->normalizeResponse( |
191
|
1 |
|
Cosapi::moveFile($this->getBucket(), $path, $newpath, 1) |
|
|
|
|
192
|
1 |
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $path |
197
|
|
|
* @param string $newpath |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
1 |
|
public function copy($path, $newpath) |
202
|
|
|
{ |
203
|
1 |
|
return (bool) $this->normalizeResponse( |
204
|
1 |
|
Cosapi::copyFile($this->getBucket(), $path, $newpath, 1) |
|
|
|
|
205
|
1 |
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param string $path |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
1 |
|
public function delete($path) |
214
|
|
|
{ |
215
|
1 |
|
return (bool) $this->normalizeResponse( |
216
|
1 |
|
Cosapi::delFile($this->getBucket(), $path) |
217
|
1 |
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $dirname |
222
|
|
|
* |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
1 |
|
public function deleteDir($dirname) |
226
|
|
|
{ |
227
|
1 |
|
return (bool) $this->normalizeResponse( |
228
|
1 |
|
Cosapi::delFolder($this->getBucket(), $dirname) |
229
|
1 |
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $dirname |
234
|
|
|
* @param Config $config |
235
|
|
|
* |
236
|
|
|
* @return array|bool |
237
|
|
|
*/ |
238
|
1 |
|
public function createDir($dirname, Config $config) |
239
|
|
|
{ |
240
|
1 |
|
return $this->normalizeResponse( |
241
|
1 |
|
Cosapi::createFolder($this->getBucket(), $dirname) |
242
|
1 |
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param string $path |
247
|
|
|
* @param string $visibility |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
1 |
|
public function setVisibility($path, $visibility) |
252
|
|
|
{ |
253
|
1 |
|
$visibility = $visibility === AdapterInterface::VISIBILITY_PUBLIC ? 'eWPrivateRPublic' : 'eWRPrivate'; |
254
|
|
|
|
255
|
1 |
|
return (bool) $this->normalizeResponse( |
|
|
|
|
256
|
1 |
|
Cosapi::update($this->getBucket(), $path, null, $visibility) |
257
|
1 |
|
); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param string $path |
262
|
|
|
* |
263
|
|
|
* @return bool |
264
|
|
|
*/ |
265
|
1 |
|
public function has($path) |
266
|
|
|
{ |
267
|
|
|
try { |
268
|
1 |
|
return (bool) $this->getMetadata($path); |
269
|
1 |
|
} catch (RuntimeException $exception) { |
270
|
1 |
|
return false; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param string $path |
276
|
|
|
* |
277
|
|
|
* @return array |
278
|
|
|
*/ |
279
|
|
|
public function read($path) |
280
|
|
|
{ |
281
|
|
|
return ['contents' => file_get_contents($this->getUrl($path))]; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param string $path |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function readStream($path) |
290
|
|
|
{ |
291
|
|
|
return ['stream' => fopen($this->getUrl($path), 'r')]; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param string $directory |
296
|
|
|
* @param bool $recursive |
297
|
|
|
* |
298
|
|
|
* @return array|bool |
299
|
|
|
*/ |
300
|
1 |
|
public function listContents($directory = '', $recursive = false) |
301
|
|
|
{ |
302
|
1 |
|
return $this->normalizeResponse( |
303
|
1 |
|
Cosapi::listFolder($this->getBucket(), $directory) |
304
|
1 |
|
); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param string $path |
309
|
|
|
* |
310
|
|
|
* @return array|bool |
311
|
|
|
*/ |
312
|
6 |
|
public function getMetadata($path) |
313
|
|
|
{ |
314
|
6 |
|
return $this->normalizeResponse( |
315
|
6 |
|
Cosapi::stat($this->getBucket(), $path) |
316
|
6 |
|
); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param string $path |
321
|
|
|
* |
322
|
|
|
* @return array|bool |
323
|
|
|
*/ |
324
|
1 |
|
public function getSize($path) |
325
|
|
|
{ |
326
|
1 |
|
$stat = $this->getMetadata($path); |
327
|
|
|
|
328
|
|
|
if (isset($stat['filesize'])) { |
329
|
|
|
return ['size' => $stat['filesize']]; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return false; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param string $path |
337
|
|
|
* |
338
|
|
|
* @return array|bool |
339
|
|
|
*/ |
340
|
1 |
|
public function getMimetype($path) |
341
|
|
|
{ |
342
|
1 |
|
$stat = $this->getMetadata($path); |
343
|
|
|
|
344
|
|
|
if (isset($stat['custom_headers']['Content-Type'])) { |
345
|
|
|
return ['mimetype' => $stat['custom_headers']['Content-Type']]; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
return false; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param string $path |
353
|
|
|
* |
354
|
|
|
* @return array|bool |
355
|
|
|
*/ |
356
|
1 |
|
public function getTimestamp($path) |
357
|
|
|
{ |
358
|
1 |
|
$stat = $this->getMetadata($path); |
359
|
|
|
|
360
|
|
|
if (isset($stat['ctime'])) { |
361
|
|
|
return ['timestamp' => $stat['ctime']]; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
return false; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param string $path |
369
|
|
|
* |
370
|
|
|
* @return array|bool |
371
|
|
|
*/ |
372
|
1 |
|
public function getVisibility($path) |
373
|
|
|
{ |
374
|
1 |
|
$stat = $this->getMetadata($path); |
375
|
|
|
|
376
|
|
View Code Duplication |
if (isset($stat['authority']) && $stat['authority'] === 'eWPrivateRPublic') { |
|
|
|
|
377
|
|
|
return ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
View Code Duplication |
if (isset($stat['authority']) && $stat['authority'] === 'eWRPrivate') { |
|
|
|
|
381
|
|
|
return ['visibility' => AdapterInterface::VISIBILITY_PRIVATE]; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return false; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Creates a temporary file |
389
|
|
|
* |
390
|
|
|
* @param string $content |
391
|
|
|
* |
392
|
|
|
* @return bool|string |
393
|
|
|
*/ |
394
|
|
|
protected function createTemporaryFile($content) |
395
|
|
|
{ |
396
|
|
|
$temporaryPath = $this->getTemporaryPath(); |
397
|
|
|
|
398
|
|
|
chmod($temporaryPath, 0777); |
399
|
|
|
|
400
|
|
|
file_put_contents($temporaryPath, $content); |
401
|
|
|
|
402
|
|
|
// The file is automatically removed when closed, or when the script ends. |
403
|
|
|
register_shutdown_function(function () use ($temporaryPath) { |
404
|
|
|
unlink($temporaryPath); |
405
|
|
|
}); |
406
|
|
|
|
407
|
|
|
return $temporaryPath; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Gets a temporary file path. |
412
|
|
|
* |
413
|
|
|
* @return string |
414
|
|
|
*/ |
415
|
|
|
protected function getTemporaryPath() |
416
|
|
|
{ |
417
|
|
|
return tempnam(sys_get_temp_dir(), uniqid('entwechat', true)); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @param string $path |
422
|
|
|
* @param string $content |
423
|
|
|
* |
424
|
|
|
* @return bool |
425
|
|
|
*/ |
426
|
|
|
protected function setContentType($path, $content) |
427
|
|
|
{ |
428
|
|
|
$custom_headers = [ |
429
|
|
|
'Content-Type' => Util::guessMimeType($path, $content), |
430
|
|
|
]; |
431
|
|
|
|
432
|
|
|
return $this->normalizeResponse( |
433
|
|
|
Cosapi::update($this->getBucket(), $path, null, null, $custom_headers) |
434
|
|
|
); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @param $response |
439
|
|
|
* |
440
|
|
|
* @throws RuntimeException |
441
|
|
|
* |
442
|
|
|
* @return mixed |
443
|
|
|
*/ |
444
|
15 |
|
protected function normalizeResponse($response) |
445
|
|
|
{ |
446
|
15 |
|
if ($response['code'] == 0) { |
447
|
|
|
return isset($response['data']) ? $response['data'] : true; |
448
|
|
|
} |
449
|
|
|
|
450
|
15 |
|
if ($this->debug) { |
451
|
15 |
|
throw new RuntimeException($response['message'], $response['code']); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
return false; |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
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.