1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Srmklive\Dropbox\Adapter; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Adapter\AbstractAdapter; |
6
|
|
|
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; |
7
|
|
|
use League\Flysystem\Config; |
8
|
|
|
use LogicException; |
9
|
|
|
use Srmklive\Dropbox\Client\DropboxClient; |
10
|
|
|
use Srmklive\Dropbox\Exceptions\BadRequest; |
11
|
|
|
use Srmklive\Dropbox\ParseResponse; |
12
|
|
|
|
13
|
|
|
class DropboxAdapter extends AbstractAdapter |
14
|
|
|
{ |
15
|
|
|
use NotSupportingVisibilityTrait, ParseResponse; |
16
|
|
|
|
17
|
|
|
/** @var \Srmklive\Dropbox\Client\DropboxClient */ |
18
|
|
|
protected $client; |
19
|
|
|
|
20
|
19 |
|
public function __construct(DropboxClient $client, $prefix = '') |
21
|
|
|
{ |
22
|
19 |
|
$this->client = $client; |
23
|
|
|
|
24
|
19 |
|
$this->setPathPrefix($prefix); |
25
|
19 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
1 |
|
public function write($path, $contents, Config $config) |
31
|
|
|
{ |
32
|
1 |
|
return $this->upload($path, $contents, 'add'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
1 |
|
public function writeStream($path, $resource, Config $config) |
39
|
|
|
{ |
40
|
1 |
|
return $this->upload($path, $resource, 'add'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
1 |
|
public function update($path, $contents, Config $config) |
47
|
|
|
{ |
48
|
1 |
|
return $this->upload($path, $contents, 'overwrite'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
1 |
|
public function updateStream($path, $resource, Config $config) |
55
|
|
|
{ |
56
|
1 |
|
return $this->upload($path, $resource, 'overwrite'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
2 |
|
public function rename($path, $newPath) |
63
|
|
|
{ |
64
|
2 |
|
$path = $this->applyPathPrefix($path); |
65
|
2 |
|
$newPath = $this->applyPathPrefix($newPath); |
66
|
|
|
|
67
|
|
|
try { |
68
|
2 |
|
$this->client->move($path, $newPath); |
69
|
2 |
|
} catch (BadRequest $e) { |
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
2 |
View Code Duplication |
public function copy($path, $newpath) |
80
|
|
|
{ |
81
|
2 |
|
$path = $this->applyPathPrefix($path); |
82
|
2 |
|
$newpath = $this->applyPathPrefix($newpath); |
83
|
|
|
|
84
|
|
|
try { |
85
|
2 |
|
$this->client->copy($path, $newpath); |
86
|
2 |
|
} catch (BadRequest $e) { |
87
|
1 |
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
1 |
|
public function delete($path) |
97
|
|
|
{ |
98
|
1 |
|
$location = $this->applyPathPrefix($path); |
99
|
|
|
|
100
|
|
|
try { |
101
|
1 |
|
$this->client->delete($location); |
102
|
1 |
|
} catch (BadRequest $e) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function deleteDir($dirname) |
113
|
|
|
{ |
114
|
1 |
|
return $this->delete($dirname); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
1 |
View Code Duplication |
public function createDir($dirname, Config $config) |
121
|
|
|
{ |
122
|
1 |
|
$path = $this->applyPathPrefix($dirname); |
123
|
|
|
|
124
|
|
|
try { |
125
|
1 |
|
$object = $this->client->createFolder($path); |
126
|
1 |
|
} catch (BadRequest $e) { |
127
|
1 |
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $this->normalizeResponse($object); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
2 |
|
public function has($path) |
137
|
|
|
{ |
138
|
2 |
|
return $this->getMetadata($path); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function read($path) |
145
|
|
|
{ |
146
|
1 |
|
if (!$object = $this->readStream($path)) { |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
$object['contents'] = stream_get_contents($object['stream']); |
151
|
1 |
|
fclose($object['stream']); |
152
|
1 |
|
unset($object['stream']); |
153
|
|
|
|
154
|
1 |
|
return $object; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
2 |
View Code Duplication |
public function readStream($path) |
161
|
|
|
{ |
162
|
2 |
|
$path = $this->applyPathPrefix($path); |
163
|
|
|
|
164
|
|
|
try { |
165
|
2 |
|
$stream = $this->client->download($path); |
166
|
2 |
|
} catch (BadRequest $e) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
return compact('stream'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
1 |
|
public function listContents($directory = '', $recursive = false) |
177
|
|
|
{ |
178
|
1 |
|
$location = $this->applyPathPrefix($directory); |
179
|
|
|
|
180
|
1 |
|
$result = $this->client->listFolder($location, $recursive); |
181
|
|
|
|
182
|
1 |
|
if (!count($result['entries'])) { |
183
|
|
|
return []; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
return array_map(function ($entry) { |
187
|
1 |
|
$path = $this->removePathPrefix($entry['path_display']); |
188
|
|
|
|
189
|
1 |
|
return $this->normalizeResponse($entry, $path); |
|
|
|
|
190
|
1 |
|
}, $result['entries']); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
5 |
View Code Duplication |
public function getMetadata($path) |
197
|
|
|
{ |
198
|
5 |
|
$path = $this->applyPathPrefix($path); |
199
|
|
|
|
200
|
|
|
try { |
201
|
5 |
|
$object = $this->client->getMetadata($path); |
202
|
5 |
|
} catch (BadRequest $e) { |
203
|
1 |
|
return false; |
204
|
|
|
} |
205
|
|
|
|
206
|
4 |
|
return $this->normalizeResponse($object); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
1 |
|
public function getSize($path) |
213
|
|
|
{ |
214
|
1 |
|
return $this->getMetadata($path); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function getMimetype($path) |
221
|
|
|
{ |
222
|
|
|
throw new LogicException("The Dropbox API v2 does not support mimetypes. Given path: `{$path}`."); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
*/ |
228
|
1 |
|
public function getTimestamp($path) |
229
|
|
|
{ |
230
|
1 |
|
return $this->getMetadata($path); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getTemporaryLink($path) |
234
|
|
|
{ |
235
|
|
|
return $this->client->getTemporaryLink($path); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function getThumbnail($path, $format = 'jpeg', $size = 'w64h64') |
239
|
|
|
{ |
240
|
|
|
return $this->client->getThumbnail($path, $format, $size); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
18 |
|
public function applyPathPrefix($path) |
247
|
|
|
{ |
248
|
18 |
|
$path = parent::applyPathPrefix($path); |
249
|
|
|
|
250
|
18 |
|
return '/'.trim($path, '/'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Remove a path prefix. |
255
|
|
|
* |
256
|
|
|
* @param string $path |
257
|
|
|
* |
258
|
|
|
* @return string path without the prefix |
259
|
|
|
*/ |
260
|
10 |
|
public function removePathPrefix($path) |
261
|
|
|
{ |
262
|
10 |
|
return parent::removePathPrefix($path); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return DropboxClient |
267
|
|
|
*/ |
268
|
1 |
|
public function getClient() |
269
|
|
|
{ |
270
|
1 |
|
return $this->client; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $path |
275
|
|
|
* @param resource|string $contents |
276
|
|
|
* @param string $mode |
277
|
|
|
* |
278
|
|
|
* @return array|false file metadata |
279
|
|
|
*/ |
280
|
4 |
View Code Duplication |
protected function upload($path, $contents, $mode) |
281
|
|
|
{ |
282
|
4 |
|
$path = $this->applyPathPrefix($path); |
283
|
|
|
|
284
|
|
|
try { |
285
|
4 |
|
$object = $this->client->upload($path, $contents, $mode); |
286
|
4 |
|
} catch (BadRequest $e) { |
287
|
|
|
return false; |
288
|
|
|
} |
289
|
|
|
|
290
|
4 |
|
return $this->normalizeResponse($object); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
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
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.