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