1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Bart Visscher <[email protected]> |
4
|
|
|
* @author Benjamin Liles <[email protected]> |
5
|
|
|
* @author Christian Berendt <[email protected]> |
6
|
|
|
* @author Daniel Tosello <[email protected]> |
7
|
|
|
* @author Felix Moeller <[email protected]> |
8
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
9
|
|
|
* @author Martin Mattel <[email protected]> |
10
|
|
|
* @author Morris Jobke <[email protected]> |
11
|
|
|
* @author Philipp Kapfer <[email protected]> |
12
|
|
|
* @author Robin Appelman <[email protected]> |
13
|
|
|
* @author Robin McCorkell <[email protected]> |
14
|
|
|
* @author Thomas Müller <[email protected]> |
15
|
|
|
* @author Tim Dettrick <[email protected]> |
16
|
|
|
* @author Vincent Petry <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
19
|
|
|
* @license AGPL-3.0 |
20
|
|
|
* |
21
|
|
|
* This code is free software: you can redistribute it and/or modify |
22
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
23
|
|
|
* as published by the Free Software Foundation. |
24
|
|
|
* |
25
|
|
|
* This program is distributed in the hope that it will be useful, |
26
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
27
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28
|
|
|
* GNU Affero General Public License for more details. |
29
|
|
|
* |
30
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
31
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
namespace OCA\Files_External\Lib\Storage; |
36
|
|
|
|
37
|
|
|
use Guzzle\Http\Url; |
38
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException; |
39
|
|
|
use Icewind\Streams\IteratorDirectory; |
40
|
|
|
use OpenCloud; |
41
|
|
|
use OpenCloud\Common\Exceptions; |
42
|
|
|
use OpenCloud\OpenStack; |
43
|
|
|
use OpenCloud\Rackspace; |
44
|
|
|
use OpenCloud\ObjectStore\Resource\DataObject; |
45
|
|
|
use OpenCloud\ObjectStore\Exception; |
46
|
|
|
|
47
|
|
|
class Swift extends \OC\Files\Storage\Common { |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \OpenCloud\ObjectStore\Service |
51
|
|
|
*/ |
52
|
|
|
private $connection; |
53
|
|
|
/** |
54
|
|
|
* @var \OpenCloud\ObjectStore\Resource\Container |
55
|
|
|
*/ |
56
|
|
|
private $container; |
57
|
|
|
/** |
58
|
|
|
* @var \OpenCloud\OpenStack |
59
|
|
|
*/ |
60
|
|
|
private $anchor; |
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $bucket; |
65
|
|
|
/** |
66
|
|
|
* Connection parameters |
67
|
|
|
* |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private $params; |
71
|
|
|
/** |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
private static $tmpFiles = array(); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $path |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
private function normalizePath($path) { |
80
|
|
|
$path = trim($path, '/'); |
81
|
|
|
|
82
|
|
|
if (!$path) { |
83
|
|
|
$path = '.'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$path = str_replace('#', '%23', $path); |
87
|
|
|
|
88
|
|
|
return $path; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
const SUBCONTAINER_FILE = '.subcontainers'; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* translate directory path to container name |
95
|
|
|
* |
96
|
|
|
* @param string $path |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
private function getContainerName($path) { |
|
|
|
|
100
|
|
|
$path = trim(trim($this->root, '/') . "/" . $path, '/.'); |
|
|
|
|
101
|
|
|
return str_replace('/', '\\', $path); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $path |
106
|
|
|
*/ |
107
|
|
|
private function doesObjectExist($path) { |
108
|
|
|
try { |
109
|
|
|
$this->getContainer()->getPartialObject($path); |
110
|
|
|
return true; |
111
|
|
|
} catch (ClientErrorResponseException $e) { |
|
|
|
|
112
|
|
|
// Expected response is "404 Not Found", so only log if it isn't |
113
|
|
|
if ($e->getResponse()->getStatusCode() !== 404) { |
114
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
115
|
|
|
} |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function __construct($params) { |
121
|
|
|
if ((empty($params['key']) and empty($params['password'])) |
122
|
|
|
or empty($params['user']) or empty($params['bucket']) |
123
|
|
|
or empty($params['region']) |
124
|
|
|
) { |
125
|
|
|
throw new \Exception("API Key or password, Username, Bucket and Region have to be configured."); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->id = 'swift::' . $params['user'] . md5($params['bucket']); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$bucketUrl = Url::factory($params['bucket']); |
131
|
|
|
if ($bucketUrl->isAbsolute()) { |
132
|
|
|
$this->bucket = end(($bucketUrl->getPathSegments())); |
|
|
|
|
133
|
|
|
$params['endpoint_url'] = $bucketUrl->addPath('..')->normalizePath(); |
134
|
|
|
} else { |
135
|
|
|
$this->bucket = $params['bucket']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (empty($params['url'])) { |
139
|
|
|
$params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (empty($params['service_name'])) { |
143
|
|
|
$params['service_name'] = 'cloudFiles'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->params = $params; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function mkdir($path) { |
150
|
|
|
$path = $this->normalizePath($path); |
151
|
|
|
|
152
|
|
|
if ($this->is_dir($path)) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($path !== '.') { |
157
|
|
|
$path .= '/'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
try { |
161
|
|
|
$customHeaders = array('content-type' => 'httpd/unix-directory'); |
162
|
|
|
$metadataHeaders = DataObject::stockHeaders(array()); |
163
|
|
|
$allHeaders = $customHeaders + $metadataHeaders; |
164
|
|
|
$this->getContainer()->uploadObject($path, '', $allHeaders); |
165
|
|
|
} catch (Exceptions\CreateUpdateError $e) { |
|
|
|
|
166
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function file_exists($path) { |
174
|
|
|
$path = $this->normalizePath($path); |
175
|
|
|
|
176
|
|
|
if ($path !== '.' && $this->is_dir($path)) { |
177
|
|
|
$path .= '/'; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this->doesObjectExist($path); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function rmdir($path) { |
184
|
|
|
$path = $this->normalizePath($path); |
185
|
|
|
|
186
|
|
|
if (!$this->is_dir($path) || !$this->isDeletable($path)) { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$dh = $this->opendir($path); |
191
|
|
View Code Duplication |
while ($file = readdir($dh)) { |
|
|
|
|
192
|
|
|
if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
193
|
|
|
continue; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if ($this->is_dir($path . '/' . $file)) { |
197
|
|
|
$this->rmdir($path . '/' . $file); |
198
|
|
|
} else { |
199
|
|
|
$this->unlink($path . '/' . $file); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
try { |
204
|
|
|
$this->getContainer()->dataObject()->setName($path . '/')->delete(); |
205
|
|
|
} catch (Exceptions\DeleteError $e) { |
|
|
|
|
206
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return true; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function opendir($path) { |
214
|
|
|
$path = $this->normalizePath($path); |
215
|
|
|
|
216
|
|
|
if ($path === '.') { |
217
|
|
|
$path = ''; |
218
|
|
|
} else { |
219
|
|
|
$path .= '/'; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$path = str_replace('%23', '#', $path); // the prefix is sent as a query param, so revert the encoding of # |
223
|
|
|
|
224
|
|
|
try { |
225
|
|
|
$files = array(); |
226
|
|
|
/** @var OpenCloud\Common\Collection $objects */ |
227
|
|
|
$objects = $this->getContainer()->objectList(array( |
228
|
|
|
'prefix' => $path, |
229
|
|
|
'delimiter' => '/' |
230
|
|
|
)); |
231
|
|
|
|
232
|
|
|
/** @var OpenCloud\ObjectStore\Resource\DataObject $object */ |
233
|
|
|
foreach ($objects as $object) { |
234
|
|
|
$file = basename($object->getName()); |
235
|
|
|
if ($file !== basename($path)) { |
236
|
|
|
$files[] = $file; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return IteratorDirectory::wrap($files); |
241
|
|
|
} catch (\Exception $e) { |
242
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function stat($path) { |
249
|
|
|
$path = $this->normalizePath($path); |
250
|
|
|
|
251
|
|
|
if ($path === '.') { |
252
|
|
|
$path = ''; |
253
|
|
|
} else if ($this->is_dir($path)) { |
254
|
|
|
$path .= '/'; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
try { |
258
|
|
|
/** @var DataObject $object */ |
259
|
|
|
$object = $this->getContainer()->getPartialObject($path); |
260
|
|
|
} catch (ClientErrorResponseException $e) { |
|
|
|
|
261
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
262
|
|
|
return false; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$dateTime = \DateTime::createFromFormat(\DateTime::RFC1123, $object->getLastModified()); |
266
|
|
|
if ($dateTime !== false) { |
267
|
|
|
$mtime = $dateTime->getTimestamp(); |
268
|
|
|
} else { |
269
|
|
|
$mtime = null; |
270
|
|
|
} |
271
|
|
|
$objectMetadata = $object->getMetadata(); |
272
|
|
|
$metaTimestamp = $objectMetadata->getProperty('timestamp'); |
273
|
|
|
if (isset($metaTimestamp)) { |
274
|
|
|
$mtime = $metaTimestamp; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
if (!empty($mtime)) { |
278
|
|
|
$mtime = floor($mtime); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$stat = array(); |
282
|
|
|
$stat['size'] = (int)$object->getContentLength(); |
283
|
|
|
$stat['mtime'] = $mtime; |
284
|
|
|
$stat['atime'] = time(); |
285
|
|
|
return $stat; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function filetype($path) { |
289
|
|
|
$path = $this->normalizePath($path); |
290
|
|
|
|
291
|
|
|
if ($path !== '.' && $this->doesObjectExist($path)) { |
292
|
|
|
return 'file'; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if ($path !== '.') { |
296
|
|
|
$path .= '/'; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if ($this->doesObjectExist($path)) { |
300
|
|
|
return 'dir'; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
public function unlink($path) { |
305
|
|
|
$path = $this->normalizePath($path); |
306
|
|
|
|
307
|
|
|
if ($this->is_dir($path)) { |
308
|
|
|
return $this->rmdir($path); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
try { |
312
|
|
|
$this->getContainer()->dataObject()->setName($path)->delete(); |
313
|
|
|
} catch (ClientErrorResponseException $e) { |
|
|
|
|
314
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
315
|
|
|
return false; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return true; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function fopen($path, $mode) { |
322
|
|
|
$path = $this->normalizePath($path); |
323
|
|
|
|
324
|
|
|
switch ($mode) { |
325
|
|
|
case 'r': |
326
|
|
|
case 'rb': |
327
|
|
|
try { |
328
|
|
|
$c = $this->getContainer(); |
329
|
|
|
$streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory(); |
330
|
|
|
$streamInterface = $streamFactory->fromRequest( |
331
|
|
|
$c->getClient() |
332
|
|
|
->get($c->getUrl($path))); |
333
|
|
|
$streamInterface->rewind(); |
334
|
|
|
$stream = $streamInterface->getStream(); |
335
|
|
|
stream_context_set_option($stream, 'swift','content', $streamInterface); |
336
|
|
|
if(!strrpos($streamInterface |
337
|
|
|
->getMetaData('wrapper_data')[0], '404 Not Found')) { |
338
|
|
|
return $stream; |
339
|
|
|
} |
340
|
|
|
return false; |
341
|
|
|
} catch (\Guzzle\Http\Exception\BadResponseException $e) { |
|
|
|
|
342
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
343
|
|
|
return false; |
344
|
|
|
} |
345
|
|
|
case 'w': |
346
|
|
|
case 'wb': |
347
|
|
|
case 'a': |
348
|
|
|
case 'ab': |
349
|
|
|
case 'r+': |
350
|
|
|
case 'w+': |
351
|
|
|
case 'wb+': |
352
|
|
|
case 'a+': |
353
|
|
|
case 'x': |
354
|
|
|
case 'x+': |
355
|
|
|
case 'c': |
356
|
|
|
case 'c+': |
357
|
|
|
if (strrpos($path, '.') !== false) { |
358
|
|
|
$ext = substr($path, strrpos($path, '.')); |
359
|
|
|
} else { |
360
|
|
|
$ext = ''; |
361
|
|
|
} |
362
|
|
|
$tmpFile = \OCP\Files::tmpFile($ext); |
363
|
|
|
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack')); |
364
|
|
|
// Fetch existing file if required |
365
|
|
|
if ($mode[0] !== 'w' && $this->file_exists($path)) { |
366
|
|
|
if ($mode[0] === 'x') { |
367
|
|
|
// File cannot already exist |
368
|
|
|
return false; |
369
|
|
|
} |
370
|
|
|
$source = $this->fopen($path, 'r'); |
371
|
|
|
file_put_contents($tmpFile, $source); |
372
|
|
|
// Seek to end if required |
373
|
|
|
if ($mode[0] === 'a') { |
374
|
|
|
fseek($tmpFile, 0, SEEK_END); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
self::$tmpFiles[$tmpFile] = $path; |
378
|
|
|
|
379
|
|
|
return fopen('close://' . $tmpFile, $mode); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function touch($path, $mtime = null) { |
384
|
|
|
$path = $this->normalizePath($path); |
385
|
|
|
if (is_null($mtime)) { |
386
|
|
|
$mtime = time(); |
387
|
|
|
} |
388
|
|
|
$metadata = array('timestamp' => $mtime); |
389
|
|
|
if ($this->file_exists($path)) { |
390
|
|
|
if ($this->is_dir($path) && $path != '.') { |
391
|
|
|
$path .= '/'; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
$object = $this->getContainer()->getPartialObject($path); |
395
|
|
|
$object->saveMetadata($metadata); |
396
|
|
|
return true; |
397
|
|
|
} else { |
398
|
|
|
$mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
399
|
|
|
$customHeaders = array('content-type' => $mimeType); |
400
|
|
|
$metadataHeaders = DataObject::stockHeaders($metadata); |
401
|
|
|
$allHeaders = $customHeaders + $metadataHeaders; |
402
|
|
|
$this->getContainer()->uploadObject($path, '', $allHeaders); |
403
|
|
|
return true; |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function copy($path1, $path2) { |
408
|
|
|
$path1 = $this->normalizePath($path1); |
409
|
|
|
$path2 = $this->normalizePath($path2); |
410
|
|
|
|
411
|
|
|
$fileType = $this->filetype($path1); |
412
|
|
|
if ($fileType === 'file') { |
413
|
|
|
|
414
|
|
|
// make way |
415
|
|
|
$this->unlink($path2); |
416
|
|
|
|
417
|
|
|
try { |
418
|
|
|
$source = $this->getContainer()->getPartialObject($path1); |
419
|
|
|
$source->copy($this->bucket . '/' . $path2); |
420
|
|
|
} catch (ClientErrorResponseException $e) { |
|
|
|
|
421
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
422
|
|
|
return false; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
} else if ($fileType === 'dir') { |
426
|
|
|
|
427
|
|
|
// make way |
428
|
|
|
$this->unlink($path2); |
429
|
|
|
|
430
|
|
|
try { |
431
|
|
|
$source = $this->getContainer()->getPartialObject($path1 . '/'); |
432
|
|
|
$source->copy($this->bucket . '/' . $path2 . '/'); |
433
|
|
|
} catch (ClientErrorResponseException $e) { |
|
|
|
|
434
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); |
435
|
|
|
return false; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$dh = $this->opendir($path1); |
439
|
|
|
while ($file = readdir($dh)) { |
440
|
|
|
if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
441
|
|
|
continue; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$source = $path1 . '/' . $file; |
445
|
|
|
$target = $path2 . '/' . $file; |
446
|
|
|
$this->copy($source, $target); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
} else { |
450
|
|
|
//file does not exist |
451
|
|
|
return false; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
return true; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
public function rename($path1, $path2) { |
458
|
|
|
$path1 = $this->normalizePath($path1); |
459
|
|
|
$path2 = $this->normalizePath($path2); |
460
|
|
|
|
461
|
|
|
$fileType = $this->filetype($path1); |
462
|
|
|
|
463
|
|
|
if ($fileType === 'dir' || $fileType === 'file') { |
464
|
|
|
|
465
|
|
|
// make way |
466
|
|
|
$this->unlink($path2); |
467
|
|
|
|
468
|
|
|
// copy |
469
|
|
|
if ($this->copy($path1, $path2) === false) { |
470
|
|
|
return false; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
// cleanup |
474
|
|
|
if ($this->unlink($path1) === false) { |
475
|
|
|
$this->unlink($path2); |
476
|
|
|
return false; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
return true; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
return false; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
public function getId() { |
486
|
|
|
return $this->id; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Returns the connection |
491
|
|
|
* |
492
|
|
|
* @return OpenCloud\ObjectStore\Service connected client |
493
|
|
|
* @throws \Exception if connection could not be made |
494
|
|
|
*/ |
495
|
|
|
public function getConnection() { |
496
|
|
|
if (!is_null($this->connection)) { |
497
|
|
|
return $this->connection; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
$settings = array( |
501
|
|
|
'username' => $this->params['user'], |
502
|
|
|
); |
503
|
|
|
|
504
|
|
|
if (!empty($this->params['password'])) { |
505
|
|
|
$settings['password'] = $this->params['password']; |
506
|
|
|
} else if (!empty($this->params['key'])) { |
507
|
|
|
$settings['apiKey'] = $this->params['key']; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
if (!empty($this->params['tenant'])) { |
511
|
|
|
$settings['tenantName'] = $this->params['tenant']; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
if (!empty($this->params['timeout'])) { |
515
|
|
|
$settings['timeout'] = $this->params['timeout']; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
if (isset($settings['apiKey'])) { |
519
|
|
|
$this->anchor = new Rackspace($this->params['url'], $settings); |
|
|
|
|
520
|
|
|
} else { |
521
|
|
|
$this->anchor = new OpenStack($this->params['url'], $settings); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
$connection = $this->anchor->objectStoreService($this->params['service_name'], $this->params['region']); |
525
|
|
|
|
526
|
|
|
if (!empty($this->params['endpoint_url'])) { |
527
|
|
|
$endpoint = $connection->getEndpoint(); |
528
|
|
|
$endpoint->setPublicUrl($this->params['endpoint_url']); |
529
|
|
|
$endpoint->setPrivateUrl($this->params['endpoint_url']); |
530
|
|
|
$connection->setEndpoint($endpoint); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
$this->connection = $connection; |
534
|
|
|
|
535
|
|
|
return $this->connection; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Returns the initialized object store container. |
540
|
|
|
* |
541
|
|
|
* @return OpenCloud\ObjectStore\Resource\Container |
542
|
|
|
*/ |
543
|
|
|
public function getContainer() { |
544
|
|
|
if (!is_null($this->container)) { |
545
|
|
|
return $this->container; |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
try { |
549
|
|
|
$this->container = $this->getConnection()->getContainer($this->bucket); |
550
|
|
|
} catch (ClientErrorResponseException $e) { |
|
|
|
|
551
|
|
|
$this->container = $this->getConnection()->createContainer($this->bucket); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
if (!$this->file_exists('.')) { |
555
|
|
|
$this->mkdir('.'); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
return $this->container; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
public function writeBack($tmpFile) { |
562
|
|
|
if (!isset(self::$tmpFiles[$tmpFile])) { |
563
|
|
|
return false; |
564
|
|
|
} |
565
|
|
|
$fileData = fopen($tmpFile, 'r'); |
566
|
|
|
$this->getContainer()->uploadObject(self::$tmpFiles[$tmpFile], $fileData); |
567
|
|
|
unlink($tmpFile); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
public function hasUpdated($path, $time) { |
571
|
|
|
if ($this->is_file($path)) { |
572
|
|
|
return parent::hasUpdated($path, $time); |
573
|
|
|
} |
574
|
|
|
$path = $this->normalizePath($path); |
575
|
|
|
$dh = $this->opendir($path); |
576
|
|
|
$content = array(); |
577
|
|
|
while (($file = readdir($dh)) !== false) { |
578
|
|
|
$content[] = $file; |
579
|
|
|
} |
580
|
|
|
if ($path === '.') { |
581
|
|
|
$path = ''; |
582
|
|
|
} |
583
|
|
|
$cachedContent = $this->getCache()->getFolderContents($path); |
584
|
|
|
$cachedNames = array_map(function ($content) { |
585
|
|
|
return $content['name']; |
586
|
|
|
}, $cachedContent); |
587
|
|
|
sort($cachedNames); |
588
|
|
|
sort($content); |
589
|
|
|
return $cachedNames != $content; |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* check if curl is installed |
594
|
|
|
*/ |
595
|
|
|
public static function checkDependencies() { |
596
|
|
|
return true; |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
} |
600
|
|
|
|