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