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\MimeType; |
10
|
|
|
use Srmklive\Dropbox\ParseResponse; |
11
|
|
|
|
12
|
|
|
class DropboxAdapter extends AbstractAdapter |
13
|
|
|
{ |
14
|
|
|
use NotSupportingVisibilityTrait, ParseResponse; |
15
|
|
|
|
16
|
|
|
/** @var \Srmklive\Dropbox\Client\DropboxClient */ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
19 |
|
public function __construct(DropboxClient $client, $prefix = '') |
20
|
|
|
{ |
21
|
19 |
|
$this->client = $client; |
22
|
|
|
|
23
|
19 |
|
$this->setPathPrefix($prefix); |
24
|
19 |
|
} |
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
|
1 |
|
public function writeStream($path, $resource, Config $config) |
38
|
|
|
{ |
39
|
1 |
|
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 $e) { |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return false; |
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
|
|
|
|
87
|
1 |
|
return true; |
88
|
1 |
|
} catch (\Exception $e) { |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
1 |
|
public function delete($path) |
98
|
|
|
{ |
99
|
1 |
|
$location = $this->applyPathPrefix($path); |
100
|
|
|
|
101
|
|
|
try { |
102
|
1 |
|
$this->client->delete($location); |
103
|
|
|
|
104
|
1 |
|
return true; |
105
|
|
|
} catch (\Exception $e) { |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
1 |
|
public function deleteDir($dirname) |
115
|
|
|
{ |
116
|
1 |
|
return $this->delete($dirname); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
1 |
View Code Duplication |
public function createDir($dirname, Config $config) |
|
|
|
|
123
|
|
|
{ |
124
|
1 |
|
$path = $this->applyPathPrefix($dirname); |
125
|
1 |
|
$response = false; |
126
|
|
|
|
127
|
|
|
try { |
128
|
1 |
|
$object = $this->client->createFolder($path); |
129
|
|
|
|
130
|
1 |
|
$response = $this->normalizeResponse($object); |
131
|
1 |
|
} catch (\Exception $e) { |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
return $response; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
2 |
|
public function has($path) |
141
|
|
|
{ |
142
|
2 |
|
return $this->getMetadata($path); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
1 |
|
public function read($path) |
149
|
|
|
{ |
150
|
|
|
try { |
151
|
1 |
|
$object = $this->readStream($path); |
152
|
|
|
|
153
|
1 |
|
$object['contents'] = stream_get_contents($object['stream']); |
154
|
1 |
|
fclose($object['stream']); |
155
|
1 |
|
unset($object['stream']); |
156
|
|
|
|
157
|
1 |
|
return $object; |
158
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
2 |
|
public function readStream($path) |
168
|
|
|
{ |
169
|
2 |
|
$path = $this->applyPathPrefix($path); |
170
|
2 |
|
$response = false; |
171
|
|
|
|
172
|
|
|
try { |
173
|
2 |
|
$stream = $this->client->download($path); |
174
|
|
|
|
175
|
2 |
|
$response = compact('stream'); |
176
|
2 |
|
} catch (\Exception $e) { |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
2 |
|
return $response; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
1 |
|
public function listContents($directory = '', $recursive = false) |
186
|
|
|
{ |
187
|
1 |
|
$response = []; |
188
|
|
|
|
189
|
|
|
try { |
190
|
1 |
|
$location = $this->applyPathPrefix($directory); |
191
|
|
|
|
192
|
1 |
|
$result = $this->client->listFolder($location, $recursive); |
193
|
|
|
|
194
|
1 |
|
$response = array_map(function ($entry) { |
195
|
1 |
|
return $this->normalizeResponse($entry); |
196
|
1 |
|
}, $result['entries']); |
197
|
1 |
|
} catch (\Exception $exception) { |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
return $response; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
5 |
View Code Duplication |
public function getMetadata($path) |
|
|
|
|
207
|
|
|
{ |
208
|
5 |
|
$path = $this->applyPathPrefix($path); |
209
|
|
|
|
210
|
|
|
try { |
211
|
5 |
|
$object = $this->client->getMetadata($path); |
212
|
|
|
|
213
|
4 |
|
return $this->normalizeResponse($object); |
214
|
1 |
|
} catch (\Exception $e) { |
215
|
1 |
|
return false; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
1 |
|
public function getSize($path) |
223
|
|
|
{ |
224
|
1 |
|
return $this->getMetadata($path); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function getMimetype($path) |
231
|
|
|
{ |
232
|
|
|
return ['mimetype' => MimeType::detectByFilename($path)]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
1 |
|
public function getTimestamp($path) |
239
|
|
|
{ |
240
|
1 |
|
return $this->getMetadata($path); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
|
|
public function getTemporaryLink($path) |
247
|
|
|
{ |
248
|
|
|
return $this->client->getTemporaryLink($path); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritdoc} |
253
|
|
|
*/ |
254
|
|
|
public function getThumbnail($path, $format = 'jpeg', $size = 'w64h64') |
255
|
|
|
{ |
256
|
|
|
return $this->client->getThumbnail($path, $format, $size); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* {@inheritdoc} |
261
|
|
|
*/ |
262
|
18 |
|
public function applyPathPrefix($path) |
263
|
|
|
{ |
264
|
18 |
|
$path = parent::applyPathPrefix($path); |
265
|
|
|
|
266
|
18 |
|
return '/'.trim($path, '/'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return DropboxClient |
271
|
|
|
*/ |
272
|
1 |
|
public function getClient() |
273
|
|
|
{ |
274
|
1 |
|
return $this->client; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param string $path |
279
|
|
|
* @param resource|string $contents |
280
|
|
|
* @param string $mode |
281
|
|
|
* |
282
|
|
|
* @throws \Exception |
283
|
|
|
* |
284
|
|
|
* @return array|false file metadata |
285
|
|
|
*/ |
286
|
4 |
View Code Duplication |
protected function upload($path, $contents, $mode) |
|
|
|
|
287
|
|
|
{ |
288
|
4 |
|
$path = $this->applyPathPrefix($path); |
289
|
|
|
|
290
|
|
|
try { |
291
|
4 |
|
$object = $this->client->upload($path, $contents, $mode); |
292
|
|
|
|
293
|
4 |
|
return $this->normalizeResponse($object); |
294
|
|
|
} catch (\Exception $e) { |
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return false; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|