|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\FlysystemDropbox; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use League\Flysystem\Adapter\AbstractAdapter; |
|
7
|
|
|
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; |
|
8
|
|
|
use League\Flysystem\Config; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
|
|
11
|
|
|
class DropboxAdapter extends AbstractAdapter |
|
12
|
|
|
{ |
|
13
|
|
|
use NotSupportingVisibilityTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \Spatie\FlysystemDropbox\DropboxClient */ |
|
16
|
|
|
protected $client; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(DropboxClient $client, string $prefix = null) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->client = $client; |
|
21
|
|
|
$this->setPathPrefix($prefix); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $path |
|
26
|
|
|
* @param string $contents |
|
27
|
|
|
* @param Config $config Config object |
|
28
|
|
|
* |
|
29
|
|
|
* @return array|false false on failure file meta data on success |
|
30
|
|
|
*/ |
|
31
|
|
|
public function write($path, $contents, Config $config) |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->upload($path, $contents, 'add'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $path |
|
38
|
|
|
* @param resource $resource |
|
39
|
|
|
* @param Config $config Config object |
|
40
|
|
|
* |
|
41
|
|
|
* @return array|false false on failure file meta data on success |
|
42
|
|
|
*/ |
|
43
|
|
|
public function writeStream($path, $resource, Config $config) |
|
44
|
|
|
{ |
|
45
|
|
|
// TODO: Implement writeStream() method. |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* @param string $contents |
|
51
|
|
|
* @param Config $config Config object |
|
52
|
|
|
* |
|
53
|
|
|
* @return array|false false on failure file meta data on success |
|
54
|
|
|
*/ |
|
55
|
|
|
public function update($path, $contents, Config $config) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->upload($path, $contents, 'overwrite'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $path |
|
62
|
|
|
* @param resource $resource |
|
63
|
|
|
* @param Config $config Config object |
|
64
|
|
|
* |
|
65
|
|
|
* @return array|false false on failure file meta data on success |
|
66
|
|
|
*/ |
|
67
|
|
|
public function updateStream($path, $resource, Config $config) |
|
68
|
|
|
{ |
|
69
|
|
|
// TODO: Implement updateStream() method. |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
* @param string $newPath |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function rename($path, $newPath): bool |
|
79
|
|
|
{ |
|
80
|
|
|
$path = $this->applyPathPrefix($path); |
|
81
|
|
|
$newPath = $this->applyPathPrefix($newPath); |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
$this->client->move($path, $newPath); |
|
85
|
|
|
} catch (Exception $e) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $path |
|
94
|
|
|
* @param string $newpath |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function copy($path, $newpath): bool |
|
99
|
|
|
{ |
|
100
|
|
|
$path = $this->applyPathPrefix($path); |
|
101
|
|
|
$newpath = $this->applyPathPrefix($newpath); |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
|
|
$this->client->copy($path, $newpath); |
|
105
|
|
|
} catch (Exception $e) { |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $path |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
public function delete($path): bool |
|
118
|
|
|
{ |
|
119
|
|
|
$location = $this->applyPathPrefix($path); |
|
120
|
|
|
|
|
121
|
|
|
try { |
|
122
|
|
|
$this->client->delete($location); |
|
123
|
|
|
} catch (Exception $e) { |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Delete a directory. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $dirname |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function deleteDir($dirname): bool |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->delete($dirname); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $dirname directory name |
|
144
|
|
|
* @param Config $config |
|
145
|
|
|
* |
|
146
|
|
|
* @return array|false |
|
147
|
|
|
*/ |
|
148
|
|
|
public function createDir($dirname, Config $config) |
|
149
|
|
|
{ |
|
150
|
|
|
$path = $this->applyPathPrefix($dirname); |
|
151
|
|
|
|
|
152
|
|
|
$result = $this->client->createFolder($path); |
|
153
|
|
|
|
|
154
|
|
|
if ($result === null) { |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $this->normalizeResponse($result); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Check whether a file exists. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $path |
|
165
|
|
|
* |
|
166
|
|
|
* @return array|bool|null |
|
167
|
|
|
*/ |
|
168
|
|
|
public function has($path) |
|
169
|
|
|
{ |
|
170
|
|
|
// TODO: Implement has() method. |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Read a file. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $path |
|
177
|
|
|
* |
|
178
|
|
|
* @return array|false |
|
179
|
|
|
*/ |
|
180
|
|
|
public function read($path) |
|
181
|
|
|
{ |
|
182
|
|
|
// TODO: Implement read() method. |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Read a file as a stream. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $path |
|
189
|
|
|
* |
|
190
|
|
|
* @return array|false |
|
191
|
|
|
*/ |
|
192
|
|
|
public function readStream($path) |
|
193
|
|
|
{ |
|
194
|
|
|
// TODO: Implement readStream() method. |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* List contents of a directory. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $directory |
|
201
|
|
|
* @param bool $recursive |
|
202
|
|
|
* |
|
203
|
|
|
* @return array |
|
204
|
|
|
*/ |
|
205
|
|
|
public function listContents($directory = '', $recursive = false) |
|
206
|
|
|
{ |
|
207
|
|
|
$listing = []; |
|
208
|
|
|
|
|
209
|
|
|
$location = $this->applyPathPrefix($directory); |
|
210
|
|
|
|
|
211
|
|
|
$result = $this->client->listContents($location, $recursive); |
|
212
|
|
|
|
|
213
|
|
|
if (!count($result['entries'])) { |
|
214
|
|
|
return []; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
foreach ($result['entries'] as $object) { |
|
218
|
|
|
$path = $this->removePathPrefix($object['path_display']); |
|
219
|
|
|
$listing[] = $this->normalizeResponse($object, $path); |
|
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $listing; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Get all the meta data of a file or directory. |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $path |
|
229
|
|
|
* |
|
230
|
|
|
* @return array|false |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getMetadata($path) |
|
233
|
|
|
{ |
|
234
|
|
|
// TODO: Implement getMetadata() method. |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get the size of a file. |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $path |
|
241
|
|
|
* |
|
242
|
|
|
* @return array|false |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getSize($path) |
|
245
|
|
|
{ |
|
246
|
|
|
// TODO: Implement getSize() method. |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Get the mimetype of a file. |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $path |
|
253
|
|
|
* |
|
254
|
|
|
* @return array|false |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getMimetype($path) |
|
257
|
|
|
{ |
|
258
|
|
|
throw new LogicException('The Dropbox API v2 does not support mimetypes. Path: ' . $path); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Get the timestamp of a file. |
|
263
|
|
|
* |
|
264
|
|
|
* @param string $path |
|
265
|
|
|
* |
|
266
|
|
|
* @return array|false |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getTimestamp($path) |
|
269
|
|
|
{ |
|
270
|
|
|
// TODO: Implement getTimestamp() method. |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
public function getTemporaryLink(string $path): string |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->client->getTemporaryLink($path); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64') |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->client->getThumbnail($path, $format, $size); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
public function applyPathPrefix($path): string |
|
284
|
|
|
{ |
|
285
|
|
|
$path = parent::applyPathPrefix($path); |
|
286
|
|
|
|
|
287
|
|
|
return '/' . ltrim(rtrim($path, '/'), '/'); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
protected function upload($path, $contents, $mode) |
|
291
|
|
|
{ |
|
292
|
|
|
$location = $this->applyPathPrefix($path); |
|
293
|
|
|
|
|
294
|
|
|
$result = $this->client->uploadFromString($location, $mode, $contents); |
|
295
|
|
|
|
|
296
|
|
|
return $this->normalizeResponse($result); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
protected function normalizeResponse(array $response): array |
|
300
|
|
|
{ |
|
301
|
|
|
$result = ['path' => ltrim($this->removePathPrefix($response['path_display']), '/')]; |
|
302
|
|
|
|
|
303
|
|
|
if (isset($response['server_modified'])) { |
|
304
|
|
|
$result['timestamp'] = strtotime($response['server_modified']); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
if (isset($response['size'])) { |
|
308
|
|
|
$result['bytes'] = $response['size']; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
$result['type'] = $response['.tag'] === 'folder' ? 'dir' : 'file'; |
|
312
|
|
|
|
|
313
|
|
|
return $result; |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.