1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/flysystem-qiniu. |
5
|
|
|
* (c) overtrue <[email protected]> |
6
|
|
|
* This source file is subject to the MIT license that is bundled |
7
|
|
|
* with this source code in the file LICENSE. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Overtrue\Flysystem\Qiniu; |
11
|
|
|
|
12
|
|
|
use League\Flysystem\Adapter\AbstractAdapter; |
13
|
|
|
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; |
14
|
|
|
use League\Flysystem\Config; |
15
|
|
|
use Qiniu\Auth; |
16
|
|
|
use Qiniu\Storage\BucketManager; |
17
|
|
|
use Qiniu\Storage\UploadManager; |
18
|
|
|
|
19
|
|
|
class QiniuAdapter extends AbstractAdapter |
20
|
|
|
{ |
21
|
|
|
use NotSupportingVisibilityTrait; |
22
|
|
|
|
23
|
|
|
protected $accessKey; |
24
|
|
|
protected $secretKey; |
25
|
|
|
protected $bucket; |
26
|
|
|
protected $domain; |
27
|
|
|
|
28
|
|
|
protected $authManager; |
29
|
|
|
protected $uploadManager; |
30
|
|
|
protected $bucketManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* QiniuAdapter constructor. |
34
|
|
|
* |
35
|
|
|
* @param string $accessKey |
36
|
|
|
* @param string $secretKey |
37
|
|
|
* @param string $bucket |
38
|
|
|
* @param string $domain |
39
|
|
|
*/ |
40
|
1 |
|
public function __construct($accessKey, $secretKey, $bucket, $domain) |
41
|
|
|
{ |
42
|
1 |
|
$this->accessKey = $accessKey; |
43
|
1 |
|
$this->secretKey = $secretKey; |
44
|
1 |
|
$this->bucket = $bucket; |
45
|
1 |
|
$this->domain = $domain; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Write a new file. |
50
|
|
|
* |
51
|
|
|
* @param string $path |
52
|
|
|
* @param string $contents |
53
|
|
|
* @param Config $config Config object |
54
|
|
|
* |
55
|
|
|
* @return array|false false on failure file meta data on success |
56
|
|
|
*/ |
57
|
2 |
|
public function write($path, $contents, Config $config) |
58
|
|
|
{ |
59
|
2 |
|
$mime = 'application/octet-stream'; |
60
|
|
|
|
61
|
2 |
|
if ($config->has('mime')) { |
62
|
1 |
|
$mime = $config->get('mime'); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
2 |
|
list($response, $error) = $this->getUploadManager()->put( |
66
|
2 |
|
$this->getAuthManager()->uploadToken($this->bucket), |
67
|
2 |
|
$path, |
68
|
2 |
|
$contents, |
69
|
2 |
|
null, |
70
|
|
|
$mime |
71
|
2 |
|
); |
72
|
|
|
|
73
|
2 |
|
if ($error) { |
74
|
2 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return $response; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Write a new file using a stream. |
82
|
|
|
* |
83
|
|
|
* @param string $path |
84
|
|
|
* @param resource $resource |
85
|
|
|
* @param Config $config Config object |
86
|
|
|
* |
87
|
|
|
* @return array|false false on failure file meta data on success |
88
|
|
|
*/ |
89
|
1 |
|
public function writeStream($path, $resource, Config $config) |
90
|
|
|
{ |
91
|
1 |
|
$contents = ''; |
92
|
|
|
|
93
|
1 |
|
while (!feof($resource)) { |
94
|
1 |
|
$contents .= fread($resource, 1024); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
1 |
|
$response = $this->write($path, $contents, $config); |
98
|
|
|
|
99
|
1 |
|
if ($response === false) { |
100
|
1 |
|
return $response; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return compact('path'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Update a file. |
108
|
|
|
* |
109
|
|
|
* @param string $path |
110
|
|
|
* @param string $contents |
111
|
|
|
* @param Config $config Config object |
112
|
|
|
* |
113
|
|
|
* @return array|false false on failure file meta data on success |
114
|
|
|
*/ |
115
|
1 |
|
public function update($path, $contents, Config $config) |
116
|
|
|
{ |
117
|
1 |
|
$this->delete($path); |
118
|
|
|
|
119
|
1 |
|
return $this->write($path, $contents, $config); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Update a file using a stream. |
124
|
|
|
* |
125
|
|
|
* @param string $path |
126
|
|
|
* @param resource $resource |
127
|
|
|
* @param Config $config Config object |
128
|
|
|
* |
129
|
|
|
* @return array|false false on failure file meta data on success |
130
|
|
|
*/ |
131
|
1 |
|
public function updateStream($path, $resource, Config $config) |
132
|
|
|
{ |
133
|
1 |
|
$this->delete($path); |
134
|
|
|
|
135
|
1 |
|
return $this->writeStream($path, $resource, $config); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Rename a file. |
140
|
|
|
* |
141
|
|
|
* @param string $path |
142
|
|
|
* @param string $newpath |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
1 |
|
public function rename($path, $newpath) |
147
|
|
|
{ |
148
|
1 |
|
$response = $this->getBucketManager()->rename($this->bucket, $path, $newpath); |
149
|
|
|
|
150
|
1 |
|
return is_null($response); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Copy a file. |
155
|
|
|
* |
156
|
|
|
* @param string $path |
157
|
|
|
* @param string $newpath |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
1 |
|
public function copy($path, $newpath) |
162
|
|
|
{ |
163
|
1 |
|
$response = $this->getBucketManager()->copy($this->bucket, $path, $this->bucket, $newpath); |
164
|
|
|
|
165
|
1 |
|
return is_null($response); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Delete a file. |
170
|
|
|
* |
171
|
|
|
* @param string $path |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
1 |
|
public function delete($path) |
176
|
|
|
{ |
177
|
1 |
|
$response = $this->getBucketManager()->delete($this->bucket, $path); |
178
|
|
|
|
179
|
1 |
|
return is_null($response); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Delete a directory. |
184
|
|
|
* |
185
|
|
|
* @param string $dirname |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
1 |
|
public function deleteDir($dirname) |
190
|
|
|
{ |
191
|
1 |
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Create a directory. |
196
|
|
|
* |
197
|
|
|
* @param string $dirname directory name |
198
|
|
|
* @param Config $config |
199
|
|
|
* |
200
|
|
|
* @return array|false |
201
|
|
|
*/ |
202
|
1 |
|
public function createDir($dirname, Config $config) |
203
|
|
|
{ |
204
|
1 |
|
return ['path' => $dirname, 'type' => 'dir']; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Check whether a file exists. |
209
|
|
|
* |
210
|
|
|
* @param string $path |
211
|
|
|
* |
212
|
|
|
* @return array|bool|null |
213
|
|
|
*/ |
214
|
1 |
|
public function has($path) |
215
|
|
|
{ |
216
|
1 |
|
list($response, $error) = $this->getBucketManager()->stat($this->bucket, $path); |
217
|
|
|
|
218
|
1 |
|
return !$error || is_array($response); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get resource url. |
223
|
|
|
* |
224
|
|
|
* @param string $path |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
1 |
|
public function getUrl($path) |
229
|
|
|
{ |
230
|
1 |
|
return $this->normalizeHost($this->domain).$path; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Read a file. |
235
|
|
|
* |
236
|
|
|
* @param string $path |
237
|
|
|
* |
238
|
|
|
* @return array|false |
239
|
|
|
*/ |
240
|
1 |
|
public function read($path) |
241
|
|
|
{ |
242
|
1 |
|
$contents = file_get_contents($this->getUrl($path)); |
243
|
|
|
|
244
|
1 |
|
return compact('contents', 'path'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Read a file as a stream. |
249
|
|
|
* |
250
|
|
|
* @param string $path |
251
|
|
|
* |
252
|
|
|
* @return array|false |
253
|
|
|
*/ |
254
|
1 |
|
public function readStream($path) |
255
|
|
|
{ |
256
|
1 |
|
if (ini_get('allow_url_fopen')) { |
257
|
1 |
|
$stream = fopen($this->normalizeHost($this->domain).$path, 'r'); |
258
|
|
|
|
259
|
1 |
|
return compact('stream', 'path'); |
260
|
|
|
} |
261
|
|
|
|
262
|
1 |
|
return false; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* List contents of a directory. |
267
|
|
|
* |
268
|
|
|
* @param string $directory |
269
|
|
|
* @param bool $recursive |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
1 |
|
public function listContents($directory = '', $recursive = false) |
274
|
|
|
{ |
275
|
1 |
|
$list = []; |
276
|
|
|
|
277
|
1 |
|
$result = $this->getBucketManager()->listFiles($this->bucket, $directory); |
278
|
|
|
|
279
|
1 |
|
foreach ($result[0] as $files) { |
280
|
1 |
|
$list[] = $this->normalizeFileInfo($files); |
281
|
1 |
|
} |
282
|
|
|
|
283
|
1 |
|
return $list; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get all the meta data of a file or directory. |
288
|
|
|
* |
289
|
|
|
* @param string $path |
290
|
|
|
* |
291
|
|
|
* @return array|false |
292
|
|
|
*/ |
293
|
1 |
|
public function getMetadata($path) |
294
|
|
|
{ |
295
|
1 |
|
$result = $this->getBucketManager()->stat($this->bucket, $path); |
296
|
1 |
|
$result[0]['key'] = $path; |
297
|
|
|
|
298
|
1 |
|
return $this->normalizeFileInfo($result[0]); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Get the size of a file. |
303
|
|
|
* |
304
|
|
|
* @param string $path |
305
|
|
|
* |
306
|
|
|
* @return array|false |
307
|
|
|
*/ |
308
|
1 |
|
public function getSize($path) |
309
|
|
|
{ |
310
|
1 |
|
return $this->getMetadata($path); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Fetch url to bucket. |
315
|
|
|
* |
316
|
|
|
* @param string $path |
317
|
|
|
* @param string $url |
318
|
|
|
* |
319
|
|
|
* @return array|false |
320
|
|
|
*/ |
321
|
|
|
public function fetch($path, $url) |
322
|
|
|
{ |
323
|
|
|
list($response, $error) = $this->getBucketManager()->fetch($url, $this->bucket, $path); |
324
|
|
|
|
325
|
|
|
if ($error) { |
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $response; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Get the mimetype of a file. |
334
|
|
|
* |
335
|
|
|
* @param string $path |
336
|
|
|
* |
337
|
|
|
* @return array|false |
338
|
|
|
*/ |
339
|
1 |
|
public function getMimetype($path) |
340
|
|
|
{ |
341
|
1 |
|
$response = $this->getBucketManager()->stat($this->bucket, $path); |
342
|
|
|
|
343
|
1 |
|
if (empty($response[0]['mimeType'])) { |
344
|
1 |
|
return false; |
345
|
|
|
} |
346
|
|
|
|
347
|
1 |
|
return ['mimetype' => $response[0]['mimeType']]; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get the timestamp of a file. |
352
|
|
|
* |
353
|
|
|
* @param string $path |
354
|
|
|
* |
355
|
|
|
* @return array|false |
356
|
|
|
*/ |
357
|
1 |
|
public function getTimestamp($path) |
358
|
|
|
{ |
359
|
1 |
|
return $this->getMetadata($path); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param \Qiniu\Storage\BucketManager $manager |
364
|
|
|
* |
365
|
|
|
* @return $this |
366
|
|
|
*/ |
367
|
1 |
|
public function setBucketManager(BucketManager $manager) |
368
|
|
|
{ |
369
|
1 |
|
$this->bucketManager = $manager; |
370
|
|
|
|
371
|
1 |
|
return $this; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param \Qiniu\Storage\UploadManager $manager |
376
|
|
|
* |
377
|
|
|
* @return $this |
378
|
|
|
*/ |
379
|
1 |
|
public function setUploadManager(UploadManager $manager) |
380
|
|
|
{ |
381
|
1 |
|
$this->uploadManager = $manager; |
382
|
|
|
|
383
|
1 |
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @param \Qiniu\Auth $manager |
388
|
|
|
* |
389
|
|
|
* @return $this |
390
|
|
|
*/ |
391
|
1 |
|
public function setAuthManager(Auth $manager) |
392
|
|
|
{ |
393
|
1 |
|
$this->authManager = $manager; |
394
|
|
|
|
395
|
1 |
|
return $this; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return \Qiniu\Storage\BucketManager |
400
|
|
|
*/ |
401
|
1 |
|
public function getBucketManager() |
402
|
|
|
{ |
403
|
1 |
|
return $this->bucketManager ?: $this->bucketManager = new BucketManager($this->getAuthManager()); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @return \Qiniu\Auth |
408
|
|
|
*/ |
409
|
1 |
|
public function getAuthManager() |
410
|
|
|
{ |
411
|
1 |
|
return $this->authManager ?: $this->authManager = new Auth($this->accessKey, $this->secretKey); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @return \Qiniu\Storage\UploadManager |
416
|
|
|
*/ |
417
|
1 |
|
public function getUploadManager() |
418
|
|
|
{ |
419
|
1 |
|
return $this->uploadManager ?: $this->uploadManager = new UploadManager(); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @param array $stats |
424
|
|
|
* |
425
|
|
|
* @return array |
426
|
|
|
*/ |
427
|
2 |
|
protected function normalizeFileInfo(array $stats) |
428
|
|
|
{ |
429
|
|
|
return [ |
430
|
2 |
|
'type' => 'file', |
431
|
2 |
|
'path' => $stats['key'], |
432
|
2 |
|
'timestamp' => floor($stats['putTime'] / 10000000), |
433
|
2 |
|
'size' => $stats['fsize'], |
434
|
2 |
|
]; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @param string $domain |
439
|
|
|
* |
440
|
|
|
* @return string |
441
|
|
|
*/ |
442
|
2 |
|
protected function normalizeHost($domain) |
443
|
|
|
{ |
444
|
2 |
|
if (0 !== stripos($domain, 'https://') && 0 !== stripos($domain, 'http://')) { |
445
|
2 |
|
$domain = "http://{$domain}"; |
446
|
2 |
|
} |
447
|
|
|
|
448
|
2 |
|
return rtrim($domain, '/').'/'; |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: