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
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function write($path, $contents, Config $config) |
28
|
|
|
{ |
29
|
|
|
return $this->upload($path, $contents, 'add'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function writeStream($path, $resource, Config $config) |
36
|
|
|
{ |
37
|
|
|
// TODO: Implement writeStream() method. |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function update($path, $contents, Config $config) |
44
|
|
|
{ |
45
|
|
|
return $this->upload($path, $contents, 'overwrite'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function updateStream($path, $resource, Config $config) |
52
|
|
|
{ |
53
|
|
|
// TODO: Implement updateStream() method. |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function rename($path, $newPath): bool |
60
|
|
|
{ |
61
|
|
|
$path = $this->applyPathPrefix($path); |
62
|
|
|
$newPath = $this->applyPathPrefix($newPath); |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
$this->client->move($path, $newPath); |
66
|
|
|
} catch (Exception $e) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function copy($path, $newpath): bool |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$path = $this->applyPathPrefix($path); |
79
|
|
|
$newpath = $this->applyPathPrefix($newpath); |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
$this->client->copy($path, $newpath); |
83
|
|
|
} catch (Exception $e) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function delete($path): bool |
94
|
|
|
{ |
95
|
|
|
$location = $this->applyPathPrefix($path); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$this->client->delete($location); |
99
|
|
|
} catch (Exception $e) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function deleteDir($dirname): bool |
110
|
|
|
{ |
111
|
|
|
return $this->delete($dirname); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function createDir($dirname, Config $config) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$path = $this->applyPathPrefix($dirname); |
120
|
|
|
|
121
|
|
|
$object = $this->client->createFolder($path); |
122
|
|
|
|
123
|
|
|
if ($object === null) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->normalizeResponse($object); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function has($path) |
134
|
|
|
{ |
135
|
|
|
return $this->getMetadata($path); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function read($path) |
142
|
|
|
{ |
143
|
|
|
// TODO: Implement read() method. |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function readStream($path) |
150
|
|
|
{ |
151
|
|
|
// TODO: Implement readStream() method. |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function listContents($directory = '', $recursive = false): array |
158
|
|
|
{ |
159
|
|
|
$listing = []; |
160
|
|
|
|
161
|
|
|
$location = $this->applyPathPrefix($directory); |
162
|
|
|
|
163
|
|
|
$result = $this->client->listContents($location, $recursive); |
164
|
|
|
|
165
|
|
|
if (!count($result['entries'])) { |
166
|
|
|
return []; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
foreach ($result['entries'] as $object) { |
170
|
|
|
$path = $this->removePathPrefix($object['path_display']); |
171
|
|
|
$listing[] = $this->normalizeResponse($object, $path); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $listing; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
public function getMetadata($path) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$path = $this->applyPathPrefix($path); |
183
|
|
|
|
184
|
|
|
try { |
185
|
|
|
$object = $this->client->getMetadata($path); |
186
|
|
|
} catch(Exception $e) { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this->normalizeResponse($object); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function getSize($path) |
197
|
|
|
{ |
198
|
|
|
return $this->getMetadata($path); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function getMimetype($path) |
205
|
|
|
{ |
206
|
|
|
throw new LogicException('The Dropbox API v2 does not support mimetypes. Path: ' . $path); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
|
|
public function getTimestamp($path) |
213
|
|
|
{ |
214
|
|
|
return $this->getMetadata($path); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getTemporaryLink(string $path): string |
218
|
|
|
{ |
219
|
|
|
return $this->client->getTemporaryLink($path); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64') |
223
|
|
|
{ |
224
|
|
|
return $this->client->getThumbnail($path, $format, $size); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function applyPathPrefix($path): string |
231
|
|
|
{ |
232
|
|
|
$path = parent::applyPathPrefix($path); |
233
|
|
|
|
234
|
|
|
return '/' . ltrim(rtrim($path, '/'), '/'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function getClient(): DropboxClient |
238
|
|
|
{ |
239
|
|
|
return $this->client; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
protected function upload($path, $contents, $mode) |
243
|
|
|
{ |
244
|
|
|
$location = $this->applyPathPrefix($path); |
245
|
|
|
|
246
|
|
|
$object = $this->client->uploadFromString($location, $mode, $contents); |
247
|
|
|
|
248
|
|
|
return $this->normalizeResponse($object); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
protected function normalizeResponse(array $response): array |
252
|
|
|
{ |
253
|
|
|
$result = ['path' => ltrim($this->removePathPrefix($response['path_display']), '/')]; |
254
|
|
|
|
255
|
|
|
if (isset($response['server_modified'])) { |
256
|
|
|
$result['timestamp'] = strtotime($response['server_modified']); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if (isset($response['size'])) { |
260
|
|
|
$result['bytes'] = $response['size']; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$result['type'] = $response['.tag'] === 'folder' ? 'dir' : 'file'; |
264
|
|
|
|
265
|
|
|
return $result; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.