|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Flysystem\Copy; |
|
4
|
|
|
|
|
5
|
|
|
use Barracuda\Copy\API; |
|
6
|
|
|
use League\Flysystem\Adapter\AbstractAdapter; |
|
7
|
|
|
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; |
|
8
|
|
|
use League\Flysystem\Config; |
|
9
|
|
|
use League\Flysystem\Util; |
|
10
|
|
|
use stdClass; |
|
11
|
|
|
|
|
12
|
|
|
class CopyAdapter extends AbstractAdapter |
|
13
|
|
|
{ |
|
14
|
|
|
use NotSupportingVisibilityTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Result key map. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $resultMap = [ |
|
22
|
|
|
'size' => 'size', |
|
23
|
|
|
'mime_type' => 'mimetype', |
|
24
|
|
|
'type' => 'type', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Copy API. |
|
29
|
|
|
* |
|
30
|
|
|
* @var API |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $client; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param API $client |
|
38
|
|
|
* @param string $prefix |
|
39
|
|
|
*/ |
|
40
|
18 |
|
public function __construct(API $client, $prefix = null) |
|
41
|
|
|
{ |
|
42
|
18 |
|
$this->client = $client; |
|
43
|
18 |
|
$this->setPathPrefix($prefix); |
|
44
|
18 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the Copy API instance. |
|
48
|
|
|
* |
|
49
|
|
|
* @return API |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function getClient() |
|
52
|
|
|
{ |
|
53
|
3 |
|
return $this->client; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Check weather a file exists. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $path |
|
60
|
|
|
* |
|
61
|
|
|
* @return array|false false or file metadata |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function has($path) |
|
64
|
|
|
{ |
|
65
|
3 |
|
return $this->getMetadata($path); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function write($path, $contents, Config $config) |
|
72
|
|
|
{ |
|
73
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
74
|
3 |
|
$result = $this->client->uploadFromString($location, $contents); |
|
75
|
|
|
|
|
76
|
3 |
|
return $this->normalizeObject($result, $location); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function writeStream($path, $resource, Config $config) |
|
83
|
|
|
{ |
|
84
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
85
|
3 |
|
$result = $this->client->uploadFromStream($location, $resource); |
|
86
|
|
|
|
|
87
|
3 |
|
return $this->normalizeObject($result, $location); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
3 |
|
public function update($path, $contents, Config $config) |
|
94
|
|
|
{ |
|
95
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
96
|
3 |
|
$result = $this->client->uploadFromString($location, $contents); |
|
97
|
|
|
|
|
98
|
3 |
|
return $this->normalizeObject($result, $location); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function updateStream($path, $resource, Config $config) |
|
105
|
|
|
{ |
|
106
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
107
|
3 |
|
$result = $this->client->uploadFromStream($location, $resource); |
|
108
|
|
|
|
|
109
|
3 |
|
return $this->normalizeObject($result, $location); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
3 |
|
public function read($path) |
|
116
|
|
|
{ |
|
117
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
118
|
|
|
|
|
119
|
3 |
|
return $this->client->readToString($location); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
3 |
|
public function readStream($path) |
|
126
|
|
|
{ |
|
127
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
128
|
|
|
|
|
129
|
3 |
|
return $this->client->readToStream($location); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
3 |
|
public function rename($path, $newpath) |
|
136
|
|
|
{ |
|
137
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
138
|
3 |
|
$destination = $this->applyPathPrefix($newpath); |
|
139
|
|
|
|
|
140
|
3 |
|
if (! $this->client->rename($location, $destination)) { |
|
141
|
3 |
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
3 |
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
6 |
|
public function copy($path, $newpath) |
|
151
|
|
|
{ |
|
152
|
6 |
|
$origin = $this->applyPathPrefix($path); |
|
153
|
6 |
|
$destination = $this->applyPathPrefix($newpath); |
|
154
|
|
|
|
|
155
|
|
|
try { |
|
156
|
6 |
|
$this->client->copy($origin, $destination); |
|
157
|
6 |
|
} catch (\Exception $e) { |
|
158
|
3 |
|
return false; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
3 |
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc} |
|
166
|
|
|
*/ |
|
167
|
3 |
|
public function delete($path) |
|
168
|
|
|
{ |
|
169
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
170
|
|
|
|
|
171
|
3 |
|
return $this->client->removeFile($location); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
*/ |
|
177
|
3 |
|
public function deleteDir($path) |
|
178
|
|
|
{ |
|
179
|
3 |
|
$location = $this->applyPathPrefix($path); |
|
180
|
|
|
|
|
181
|
3 |
|
return $this->client->removeDir($location); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* {@inheritdoc} |
|
186
|
|
|
*/ |
|
187
|
6 |
|
public function createDir($path, Config $config) |
|
188
|
|
|
{ |
|
189
|
6 |
|
$location = $this->applyPathPrefix($path); |
|
190
|
|
|
|
|
191
|
|
|
try { |
|
192
|
6 |
|
$this->client->createDir($location); |
|
193
|
6 |
|
} catch (\Exception $e) { |
|
194
|
3 |
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
3 |
|
return compact('path') + ['type' => 'dir']; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
*/ |
|
203
|
15 |
|
public function getMetadata($path) |
|
204
|
|
|
{ |
|
205
|
15 |
|
$location = $this->applyPathPrefix($path); |
|
206
|
15 |
|
$objects = $this->client->listPath($location); |
|
207
|
|
|
|
|
208
|
15 |
|
if ($objects === false || isset($objects[0]) === false || empty($objects[0])) { |
|
209
|
15 |
|
return false; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
15 |
|
return $this->normalizeObject($objects[0], $location); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* {@inheritdoc} |
|
217
|
|
|
*/ |
|
218
|
3 |
|
public function getMimetype($path) |
|
219
|
|
|
{ |
|
220
|
3 |
|
return $this->getMetadata($path); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* {@inheritdoc} |
|
225
|
|
|
*/ |
|
226
|
3 |
|
public function getSize($path) |
|
227
|
|
|
{ |
|
228
|
3 |
|
return $this->getMetadata($path); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* {@inheritdoc} |
|
233
|
|
|
*/ |
|
234
|
3 |
|
public function getTimestamp($path) |
|
235
|
|
|
{ |
|
236
|
3 |
|
return $this->getMetadata($path); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* {@inheritdoc} |
|
241
|
|
|
*/ |
|
242
|
3 |
|
public function listContents($dirname = '', $recursive = false) |
|
243
|
|
|
{ |
|
244
|
3 |
|
$listing = []; |
|
245
|
3 |
|
$location = $this->applyPathPrefix($dirname); |
|
246
|
|
|
|
|
247
|
3 |
|
if (! $result = $this->client->listPath($location)) { |
|
248
|
3 |
|
return []; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
3 |
|
foreach ($result as $object) { |
|
252
|
3 |
|
$listing[] = $this->normalizeObject($object, $object->path); |
|
253
|
|
|
|
|
254
|
3 |
|
if ($recursive && $object->type == 'dir') { |
|
255
|
3 |
|
$listing = array_merge($listing, $this->listContents($object->path, $recursive)); |
|
256
|
3 |
|
} |
|
257
|
3 |
|
} |
|
258
|
|
|
|
|
259
|
3 |
|
return $listing; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Normalize a result from Copy. |
|
264
|
|
|
* |
|
265
|
|
|
* @param stdClass $object |
|
266
|
|
|
* @param string $path |
|
267
|
|
|
* |
|
268
|
|
|
* @return array|false file metadata |
|
269
|
|
|
*/ |
|
270
|
30 |
|
protected function normalizeObject($object, $path) |
|
271
|
|
|
{ |
|
272
|
30 |
|
if (!$object instanceof stdClass) { |
|
273
|
6 |
|
return false; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
30 |
|
if (isset($object->modified_time)) { |
|
277
|
30 |
|
$timestamp = $object->modified_time; |
|
278
|
30 |
|
} |
|
279
|
|
|
|
|
280
|
30 |
|
$path = trim($this->removePathPrefix($path), '/'); |
|
281
|
30 |
|
$result = Util::map((array) $object, static::$resultMap); |
|
282
|
|
|
|
|
283
|
30 |
|
return compact('timestamp', 'path') + $result; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Apply the path prefix. |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $path |
|
290
|
|
|
* |
|
291
|
|
|
* @return string prefixed path |
|
292
|
|
|
*/ |
|
293
|
54 |
|
public function applyPathPrefix($path) |
|
294
|
|
|
{ |
|
295
|
54 |
|
$prefixed = parent::applyPathPrefix($path); |
|
296
|
|
|
|
|
297
|
54 |
|
return '/'.ltrim($prefixed, '/'); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|