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