1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sulao\Flysystem\BaiduBos; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Config; |
6
|
|
|
use League\Flysystem\FileAttributes; |
7
|
|
|
use League\Flysystem\FilesystemAdapter; |
8
|
|
|
use League\Flysystem\UnableToCreateDirectory; |
9
|
|
|
use League\Flysystem\UnableToCopyFile; |
10
|
|
|
use League\Flysystem\UnableToDeleteDirectory; |
11
|
|
|
use League\Flysystem\UnableToDeleteFile; |
12
|
|
|
use League\Flysystem\UnableToListContents; |
13
|
|
|
use League\Flysystem\UnableToMoveFile; |
14
|
|
|
use League\Flysystem\UnableToReadFile; |
15
|
|
|
use League\Flysystem\UnableToRetrieveMetadata; |
16
|
|
|
use League\Flysystem\UnableToSetVisibility; |
17
|
|
|
use League\Flysystem\UnableToWriteFile; |
18
|
|
|
use Sulao\BaiduBos\Client; |
19
|
|
|
use Sulao\BaiduBos\Exception; |
20
|
|
|
use Throwable; |
21
|
|
|
|
22
|
|
|
class BaiduBosAdapter implements FilesystemAdapter |
23
|
|
|
{ |
24
|
|
|
use UtilTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Client |
28
|
|
|
*/ |
29
|
|
|
protected Client $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* BaiduBosAdapter constructor. |
33
|
|
|
* |
34
|
|
|
* @param Client $client |
35
|
|
|
*/ |
36
|
2 |
|
public function __construct(Client $client) |
37
|
|
|
{ |
38
|
2 |
|
$this->client = $client; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Write a new file. |
43
|
|
|
* |
44
|
|
|
* @param string $path |
45
|
|
|
* @param string $contents |
46
|
|
|
* @param Config $config |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
* @throws UnableToWriteFile |
50
|
|
|
*/ |
51
|
2 |
|
public function write(string $path, string $contents, Config $config): void |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
2 |
|
$this->client |
55
|
2 |
|
->putObject($path, $contents, $this->extractOptions($config)); |
56
|
1 |
|
} catch (Throwable $e) { |
57
|
1 |
|
throw UnableToWriteFile::atLocation($path, $e->getMessage(), $e); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Write a new file using a stream. |
63
|
|
|
* |
64
|
|
|
* @param string $path |
65
|
|
|
* @param resource $contents |
66
|
|
|
* @param Config $config |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
* @throws UnableToWriteFile |
70
|
|
|
*/ |
71
|
2 |
|
public function writeStream(string $path, $contents, Config $config): void |
72
|
|
|
{ |
73
|
2 |
|
$this->write($path, stream_get_contents($contents), $config); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Rename/Move a file. |
78
|
|
|
* |
79
|
|
|
* @param string $source |
80
|
|
|
* @param string $destination |
81
|
|
|
* @param Config $config |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
* @throws UnableToMoveFile |
85
|
|
|
*/ |
86
|
2 |
|
public function move( |
87
|
|
|
string $source, |
88
|
|
|
string $destination, |
89
|
|
|
Config $config |
90
|
|
|
): void { |
91
|
|
|
try { |
92
|
2 |
|
$this->client->copyObject($source, $destination); |
93
|
1 |
|
$this->client->deleteObject($source); |
94
|
1 |
|
} catch (Throwable $e) { |
95
|
1 |
|
throw UnableToMoveFile::fromLocationTo($source, $destination, $e); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Copy a file. |
102
|
|
|
* |
103
|
|
|
* @param string $source |
104
|
|
|
* @param string $destination |
105
|
|
|
* @param Config $config |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
* @throws UnableToCopyFile |
109
|
|
|
*/ |
110
|
2 |
|
public function copy( |
111
|
|
|
string $source, |
112
|
|
|
string $destination, |
113
|
|
|
Config $config |
114
|
|
|
): void { |
115
|
|
|
try { |
116
|
2 |
|
$this->client->copyObject($source, $destination); |
117
|
1 |
|
} catch (Throwable $e) { |
118
|
1 |
|
throw UnableToCopyFile::fromLocationTo( |
119
|
1 |
|
$source, |
120
|
1 |
|
$e->getMessage(), |
121
|
1 |
|
$e |
122
|
1 |
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Delete a file. |
128
|
|
|
* |
129
|
|
|
* @param string $path |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
* @throws UnableToDeleteFile |
133
|
|
|
*/ |
134
|
2 |
|
public function delete(string $path): void |
135
|
|
|
{ |
136
|
|
|
try { |
137
|
2 |
|
$this->client->deleteObject($path); |
138
|
1 |
|
} catch (Throwable $e) { |
139
|
1 |
|
throw UnableToDeleteFile::atLocation($path, $e->getMessage(), $e); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Check whether a file exists. |
145
|
|
|
* |
146
|
|
|
* @param string $path |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
1 |
|
public function fileExists(string $path): bool |
151
|
|
|
{ |
152
|
|
|
try { |
153
|
1 |
|
$this->client->getObjectMeta($path); |
154
|
1 |
|
} catch (Throwable $e) { |
155
|
1 |
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Check whether a dir exists. |
163
|
|
|
* |
164
|
|
|
* @param string $path |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
2 |
|
public function directoryExists(string $path): bool |
169
|
|
|
{ |
170
|
|
|
try { |
171
|
2 |
|
$lists = $this->listContents($path, false); |
172
|
1 |
|
return !empty($lists); |
173
|
1 |
|
} catch (Throwable $e) { |
174
|
1 |
|
return false; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Create a directory. |
181
|
|
|
* |
182
|
|
|
* @param string $path |
183
|
|
|
* @param Config $config |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
* @throws UnableToCreateDirectory |
187
|
|
|
*/ |
188
|
2 |
|
public function createDirectory(string $path, Config $config): void |
189
|
|
|
{ |
190
|
|
|
try { |
191
|
2 |
|
$this->client->putObject( |
192
|
2 |
|
rtrim($path, '/') . '/', |
193
|
2 |
|
'', |
194
|
2 |
|
$this->extractOptions($config) |
195
|
2 |
|
); |
196
|
1 |
|
} catch (Throwable $e) { |
197
|
1 |
|
throw UnableToCreateDirectory::atLocation( |
198
|
1 |
|
$path, |
199
|
1 |
|
$e->getMessage(), |
200
|
1 |
|
$e |
201
|
1 |
|
); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the mime-type of a file. |
207
|
|
|
* |
208
|
|
|
* @param string $path |
209
|
|
|
* |
210
|
|
|
* @return FileAttributes |
211
|
|
|
* @throws UnableToRetrieveMetadata |
212
|
|
|
*/ |
213
|
2 |
|
public function mimeType(string $path): FileAttributes |
214
|
|
|
{ |
215
|
|
|
try { |
216
|
2 |
|
$meta = $this->getMetadata($path); |
217
|
1 |
|
$mimeType = $meta['mimeType']; |
218
|
1 |
|
} catch (Throwable $e) { |
219
|
1 |
|
throw UnableToRetrieveMetadata::mimeType( |
220
|
1 |
|
$path, |
221
|
1 |
|
$e->getMessage(), |
222
|
1 |
|
$e |
223
|
1 |
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
return new FileAttributes($path, null, null, null, $mimeType); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get the lastModified of a file. |
232
|
|
|
* |
233
|
|
|
* @param string $path |
234
|
|
|
* |
235
|
|
|
* @return FileAttributes |
236
|
|
|
* @throws UnableToRetrieveMetadata |
237
|
|
|
*/ |
238
|
2 |
|
public function lastModified(string $path): FileAttributes |
239
|
|
|
{ |
240
|
|
|
try { |
241
|
2 |
|
$meta = $this->getMetadata($path); |
242
|
1 |
|
$lastModified = $meta['lastModified']; |
243
|
1 |
|
} catch (Throwable $e) { |
244
|
1 |
|
throw UnableToRetrieveMetadata::lastModified( |
245
|
1 |
|
$path, |
246
|
1 |
|
$e->getMessage(), |
247
|
1 |
|
$e |
248
|
1 |
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
1 |
|
return new FileAttributes($path, null, null, $lastModified); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get the size of a file. |
256
|
|
|
* |
257
|
|
|
* @param string $path |
258
|
|
|
* |
259
|
|
|
* @return FileAttributes |
260
|
|
|
* @throws UnableToRetrieveMetadata |
261
|
|
|
*/ |
262
|
2 |
|
public function fileSize(string $path): FileAttributes |
263
|
|
|
{ |
264
|
|
|
try { |
265
|
2 |
|
$meta = $this->getMetadata($path); |
266
|
1 |
|
$fileSize = $meta['fileSize']; |
267
|
1 |
|
} catch (Throwable $e) { |
268
|
1 |
|
throw UnableToRetrieveMetadata::fileSize( |
269
|
1 |
|
$path, |
270
|
1 |
|
$e->getMessage(), |
271
|
1 |
|
$e |
272
|
1 |
|
); |
273
|
|
|
} |
274
|
|
|
|
275
|
1 |
|
return new FileAttributes($path, $fileSize); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Delete a directory. |
281
|
|
|
* |
282
|
|
|
* @param string $path |
283
|
|
|
* |
284
|
|
|
* @return void |
285
|
|
|
* @throws UnableToDeleteDirectory |
286
|
|
|
*/ |
287
|
2 |
|
public function deleteDirectory(string $path): void |
288
|
|
|
{ |
289
|
|
|
try { |
290
|
2 |
|
$this->client->deleteObject(rtrim($path, '/') . '/'); |
291
|
1 |
|
} catch (Throwable $e) { |
292
|
1 |
|
throw UnableToDeleteDirectory::atLocation( |
293
|
1 |
|
$path, |
294
|
1 |
|
$e->getMessage(), |
295
|
1 |
|
$e |
296
|
1 |
|
); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Read a file. |
303
|
|
|
* |
304
|
|
|
* @param string $path |
305
|
|
|
* |
306
|
|
|
* @return string |
307
|
|
|
* @throws UnableToReadFile |
308
|
|
|
*/ |
309
|
2 |
|
public function read(string $path): string |
310
|
|
|
{ |
311
|
|
|
try { |
312
|
2 |
|
$contents = $this->client->getObject($path); |
313
|
1 |
|
} catch (Throwable $e) { |
314
|
1 |
|
throw UnableToReadFile::fromLocation($path, $e->getMessage(), $e); |
315
|
|
|
} |
316
|
|
|
|
317
|
1 |
|
return $contents; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Read a file as a stream. |
322
|
|
|
* |
323
|
|
|
* @param string $path |
324
|
|
|
* |
325
|
|
|
* @return resource |
326
|
|
|
* @throws UnableToReadFile |
327
|
|
|
*/ |
328
|
2 |
|
public function readStream(string $path) |
329
|
|
|
{ |
330
|
2 |
|
$contents = $this->read($path); |
331
|
1 |
|
$stream = fopen('php://temp', 'w+b'); |
332
|
1 |
|
fputs($stream, $contents); |
333
|
1 |
|
rewind($stream); |
334
|
|
|
|
335
|
1 |
|
return $stream; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* List contents of a directory. |
340
|
|
|
* |
341
|
|
|
* @param string $path |
342
|
|
|
* @param bool $deep |
343
|
|
|
* |
344
|
|
|
* @return iterable |
345
|
|
|
* @throws UnableToListContents |
346
|
|
|
*/ |
347
|
3 |
|
public function listContents(string $path, bool $deep): iterable |
348
|
|
|
{ |
349
|
3 |
|
$options = $this->buildListDirOptions($path, $deep); |
350
|
|
|
try { |
351
|
3 |
|
$result = $this->client->listObjects($options); |
352
|
1 |
|
} catch (Throwable $e) { |
353
|
1 |
|
throw UnableToListContents::atLocation($path, $e->getMessage(), $e); |
|
|
|
|
354
|
|
|
} |
355
|
|
|
|
356
|
2 |
|
$prefixes = isset($result['commonPrefixes']) |
357
|
2 |
|
? array_map(function ($item) { |
358
|
1 |
|
return ['key' => $item['prefix']]; |
359
|
2 |
|
}, $result['commonPrefixes']) |
360
|
1 |
|
: []; |
361
|
|
|
|
362
|
2 |
|
return array_map(function ($content) { |
363
|
2 |
|
return $this->normalizeContent($content); |
364
|
2 |
|
}, array_merge($result['contents'], $prefixes)); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Get the visibility of a file. |
369
|
|
|
* |
370
|
|
|
* @param string $path |
371
|
|
|
* |
372
|
|
|
* @return FileAttributes |
373
|
|
|
*/ |
374
|
|
|
|
375
|
2 |
|
public function visibility(string $path): FileAttributes |
376
|
|
|
{ |
377
|
|
|
try { |
378
|
2 |
|
$acl = $this->getObjectAcl($path); |
379
|
1 |
|
} catch (Throwable $e) { |
380
|
1 |
|
throw UnableToRetrieveMetadata::visibility( |
381
|
1 |
|
$path, |
382
|
1 |
|
$e->getMessage(), |
383
|
1 |
|
$e |
384
|
1 |
|
); |
385
|
|
|
} |
386
|
|
|
|
387
|
1 |
|
$permissions = $this->extractPermissions($acl); |
388
|
|
|
|
389
|
1 |
|
if (in_array('READ', $permissions)) { |
390
|
1 |
|
$visibility = 'public'; |
391
|
|
|
} else { |
392
|
1 |
|
$visibility = 'private'; |
393
|
|
|
} |
394
|
|
|
|
395
|
1 |
|
return new FileAttributes($path, null, $visibility); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Set the visibility for a file. |
401
|
|
|
* |
402
|
|
|
* @param string $path |
403
|
|
|
* @param string $visibility |
404
|
|
|
* |
405
|
|
|
* @return void |
406
|
|
|
*/ |
407
|
2 |
|
public function setVisibility(string $path, string $visibility): void |
408
|
|
|
{ |
409
|
2 |
|
if ($visibility === 'public') { |
410
|
1 |
|
$visibility = 'public-read'; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
try { |
414
|
2 |
|
$this->client->putObjectAcl($path, $visibility); |
415
|
1 |
|
} catch (Throwable $e) { |
416
|
1 |
|
throw UnableToSetVisibility::atLocation( |
417
|
1 |
|
$path, |
418
|
1 |
|
$e->getMessage(), |
419
|
1 |
|
$e |
420
|
1 |
|
); |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Get all the meta data of a file or directory. |
426
|
|
|
* |
427
|
|
|
* @param string $path |
428
|
|
|
* |
429
|
|
|
* @return array |
430
|
|
|
* @throws Exception |
431
|
|
|
*/ |
432
|
2 |
|
protected function getMetadata(string $path): array |
433
|
|
|
{ |
434
|
2 |
|
$meta = $this->client->getObjectMeta($path); |
435
|
1 |
|
return $this->normalizeMeta($meta, $path); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Get object acl, if not set, return bucket acl |
440
|
|
|
* |
441
|
|
|
* @param string $path |
442
|
|
|
* |
443
|
|
|
* @return array |
444
|
|
|
* @throws Throwable |
445
|
|
|
* @throws Exception |
446
|
|
|
*/ |
447
|
2 |
|
protected function getObjectAcl(string $path): array |
448
|
|
|
{ |
449
|
|
|
try { |
450
|
2 |
|
$result = $this->client->getObjectAcl($path); |
451
|
1 |
|
return $result['accessControlList']; |
452
|
2 |
|
} catch (Throwable $exception) { |
453
|
2 |
|
if ($exception->getCode() == 404) { |
454
|
1 |
|
return $this->getBucketAcl(); |
455
|
|
|
} |
456
|
|
|
|
457
|
1 |
|
throw $exception; |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Get bucket acl |
463
|
|
|
* |
464
|
|
|
* @return array |
465
|
|
|
* @throws Exception |
466
|
|
|
*/ |
467
|
1 |
|
protected function getBucketAcl(): array |
468
|
|
|
{ |
469
|
1 |
|
$result = $this->client->getBucketAcl(); |
470
|
1 |
|
return $result['accessControlList']; |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|