2012-04-18 22:54:07 +04:00
|
|
|
<?php
|
2018-02-12 17:08:27 +03:00
|
|
|
declare(strict_types=1);
|
2012-04-18 22:54:07 +04:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Benjamin Liles <benliles@arch.tamu.edu>
|
|
|
|
* @author Christian Berendt <berendt@b1-systems.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Christopher Bartz <bartz@dkrz.de>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Daniel Tosello <tosello.daniel@gmail.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Felix Moeller <mail@felixmoeller.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Martin Mattel <martin.mattel@diemattels.at>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Philipp Kapfer <philipp.kapfer@gmx.at>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Tim Dettrick <t.dettrick@uq.edu.au>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2013-10-22 16:59:09 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2013-10-22 16:59:09 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2013-10-22 16:59:09 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2013-10-22 16:59:09 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-10-22 16:59:09 +04:00
|
|
|
*
|
2012-04-18 22:54:07 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2016-04-14 01:18:07 +03:00
|
|
|
namespace OCA\Files_External\Lib\Storage;
|
2012-09-07 20:30:48 +04:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
use GuzzleHttp\Psr7\Uri;
|
2017-01-03 19:26:44 +03:00
|
|
|
use Icewind\Streams\CallbackWrapper;
|
2015-03-10 18:30:13 +03:00
|
|
|
use Icewind\Streams\IteratorDirectory;
|
2018-02-12 17:08:27 +03:00
|
|
|
use OC\Files\ObjectStore\SwiftFactory;
|
|
|
|
use OCP\Files\StorageBadConfigException;
|
|
|
|
use OpenStack\Common\Error\BadResponseError;
|
|
|
|
use OpenStack\ObjectStore\v1\Models\StorageObject;
|
2013-10-22 16:59:09 +04:00
|
|
|
|
|
|
|
class Swift extends \OC\Files\Storage\Common {
|
2018-02-12 17:08:27 +03:00
|
|
|
/** @var SwiftFactory */
|
|
|
|
private $connectionFactory;
|
2014-05-16 00:47:28 +04:00
|
|
|
/**
|
2018-02-12 17:08:27 +03:00
|
|
|
* @var \OpenStack\ObjectStore\v1\Models\Container
|
2014-05-16 00:47:28 +04:00
|
|
|
*/
|
2013-10-22 16:59:09 +04:00
|
|
|
private $container;
|
2014-05-16 00:47:28 +04:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-10-22 16:59:09 +04:00
|
|
|
private $bucket;
|
2014-10-21 18:18:44 +04:00
|
|
|
/**
|
|
|
|
* Connection parameters
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $params;
|
2016-07-05 23:45:05 +03:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
/** @var string */
|
2016-07-05 23:45:05 +03:00
|
|
|
private $id;
|
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
/** @var \OC\Files\ObjectStore\Swift */
|
|
|
|
private $objectStore;
|
|
|
|
|
2014-03-26 20:17:06 +04:00
|
|
|
/**
|
|
|
|
* Key value cache mapping path to data object. Maps path to
|
|
|
|
* \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject for existing
|
|
|
|
* paths and path to false for not existing paths.
|
2018-02-12 17:08:27 +03:00
|
|
|
*
|
2014-03-26 20:17:06 +04:00
|
|
|
* @var \OCP\ICache
|
|
|
|
*/
|
|
|
|
private $objectCache;
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
2018-02-12 17:08:27 +03:00
|
|
|
* @return mixed|string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2018-02-12 17:08:27 +03:00
|
|
|
private function normalizePath(string $path) {
|
2013-10-22 16:59:09 +04:00
|
|
|
$path = trim($path, '/');
|
|
|
|
|
|
|
|
if (!$path) {
|
|
|
|
$path = '.';
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
$path = str_replace('#', '%23', $path);
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
return $path;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2014-09-05 15:22:35 +04:00
|
|
|
const SUBCONTAINER_FILE = '.subcontainers';
|
2013-08-02 17:44:56 +04:00
|
|
|
|
2013-06-01 13:28:02 +04:00
|
|
|
/**
|
2013-08-02 17:44:56 +04:00
|
|
|
* translate directory path to container name
|
2014-09-05 15:22:35 +04:00
|
|
|
*
|
2013-08-02 17:44:56 +04:00
|
|
|
* @param string $path
|
|
|
|
* @return string
|
2013-06-01 13:28:02 +04:00
|
|
|
*/
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2014-03-26 20:17:06 +04:00
|
|
|
* Fetches an object from the API.
|
|
|
|
* If the object is cached already or a
|
|
|
|
* failed "doesn't exist" response was cached,
|
|
|
|
* that one will be returned.
|
|
|
|
*
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string $path
|
2018-02-12 17:08:27 +03:00
|
|
|
* @return StorageObject|bool object
|
2014-03-26 20:17:06 +04:00
|
|
|
* or false if the object did not exist
|
2018-02-12 17:08:27 +03:00
|
|
|
* @throws \OCP\Files\StorageAuthException
|
|
|
|
* @throws \OCP\Files\StorageNotAvailableException
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2018-02-12 17:08:27 +03:00
|
|
|
private function fetchObject(string $path) {
|
2014-03-26 20:17:06 +04:00
|
|
|
if ($this->objectCache->hasKey($path)) {
|
|
|
|
// might be "false" if object did not exist from last check
|
|
|
|
return $this->objectCache->get($path);
|
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2018-02-12 17:08:27 +03:00
|
|
|
$object = $this->getContainer()->getObject($path);
|
|
|
|
$object->retrieve();
|
2014-03-26 20:17:06 +04:00
|
|
|
$this->objectCache->set($path, $object);
|
|
|
|
return $object;
|
2018-02-12 17:08:27 +03:00
|
|
|
} catch (BadResponseError $e) {
|
2015-09-04 04:40:23 +03:00
|
|
|
// Expected response is "404 Not Found", so only log if it isn't
|
|
|
|
if ($e->getResponse()->getStatusCode() !== 404) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2015-09-04 04:40:23 +03:00
|
|
|
}
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->objectCache->set($path, false);
|
2013-10-22 16:59:09 +04:00
|
|
|
return false;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:17:06 +04:00
|
|
|
/**
|
|
|
|
* Returns whether the given path exists.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*
|
|
|
|
* @return bool true if the object exist, false otherwise
|
2018-02-12 17:08:27 +03:00
|
|
|
* @throws \OCP\Files\StorageAuthException
|
|
|
|
* @throws \OCP\Files\StorageNotAvailableException
|
2014-03-26 20:17:06 +04:00
|
|
|
*/
|
|
|
|
private function doesObjectExist($path) {
|
|
|
|
return $this->fetchObject($path) !== false;
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function __construct($params) {
|
2014-09-05 15:22:35 +04:00
|
|
|
if ((empty($params['key']) and empty($params['password']))
|
2018-02-14 17:08:52 +03:00
|
|
|
or (empty($params['user']) && empty($params['userid'])) or empty($params['bucket'])
|
2014-09-05 15:22:35 +04:00
|
|
|
or empty($params['region'])
|
|
|
|
) {
|
2018-02-12 17:08:27 +03:00
|
|
|
throw new StorageBadConfigException("API Key or password, Username, Bucket and Region have to be configured.");
|
2012-04-25 02:11:10 +04:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
2018-02-26 14:58:19 +03:00
|
|
|
$user = $params['user'];
|
2018-02-14 17:08:52 +03:00
|
|
|
$this->id = 'swift::' . $user . md5($params['bucket']);
|
2015-05-20 10:04:43 +03:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
$bucketUrl = new Uri($params['bucket']);
|
|
|
|
if ($bucketUrl->getHost()) {
|
|
|
|
$params['bucket'] = basename($bucketUrl->getPath());
|
|
|
|
$params['endpoint_url'] = (string)$bucketUrl->withPath(dirname($bucketUrl->getPath()));
|
2015-05-20 10:04:43 +03:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
2014-09-05 15:22:35 +04:00
|
|
|
if (empty($params['url'])) {
|
2013-10-22 16:59:09 +04:00
|
|
|
$params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/';
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2014-09-05 15:22:35 +04:00
|
|
|
if (empty($params['service_name'])) {
|
2013-10-22 16:59:09 +04:00
|
|
|
$params['service_name'] = 'cloudFiles';
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
$params['autocreate'] = true;
|
|
|
|
|
2018-02-26 14:58:19 +03:00
|
|
|
if (isset($params['domain'])) {
|
2018-02-14 17:08:52 +03:00
|
|
|
$params['user'] = [
|
2018-02-26 14:58:19 +03:00
|
|
|
'name' => $params['user'],
|
|
|
|
'password' => $params['password'],
|
|
|
|
'domain' => [
|
|
|
|
'name' => $params['domain'],
|
|
|
|
]
|
2018-02-14 17:08:52 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->params = $params;
|
2014-03-26 20:17:06 +04:00
|
|
|
// FIXME: private class...
|
|
|
|
$this->objectCache = new \OC\Cache\CappedMemoryCache();
|
2018-03-16 17:20:16 +03:00
|
|
|
$this->connectionFactory = new SwiftFactory(
|
|
|
|
\OC::$server->getMemCacheFactory()->createDistributed('swift/'),
|
|
|
|
$this->params,
|
|
|
|
\OC::$server->getLogger()
|
|
|
|
);
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->objectStore = new \OC\Files\ObjectStore\Swift($this->params, $this->connectionFactory);
|
|
|
|
$this->bucket = $params['bucket'];
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function mkdir($path) {
|
|
|
|
$path = $this->normalizePath($path);
|
|
|
|
|
|
|
|
if ($this->is_dir($path)) {
|
2012-04-18 22:54:07 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-05 15:22:35 +04:00
|
|
|
if ($path !== '.') {
|
2013-10-22 16:59:09 +04:00
|
|
|
$path .= '/';
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
2012-11-30 19:27:11 +04:00
|
|
|
try {
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->getContainer()->createObject([
|
|
|
|
'name' => $path,
|
|
|
|
'content' => '',
|
|
|
|
'headers' => ['content-type' => 'httpd/unix-directory']
|
|
|
|
]);
|
2014-03-26 20:17:06 +04:00
|
|
|
// invalidate so that the next access gets the real object
|
|
|
|
// with all properties
|
|
|
|
$this->objectCache->remove($path);
|
2018-02-12 17:08:27 +03:00
|
|
|
} catch (BadResponseError $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2012-04-18 22:54:07 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function file_exists($path) {
|
|
|
|
$path = $this->normalizePath($path);
|
2012-04-18 22:54:07 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
if ($path !== '.' && $this->is_dir($path)) {
|
|
|
|
$path .= '/';
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
return $this->doesObjectExist($path);
|
2012-10-22 00:04:45 +04:00
|
|
|
}
|
2012-04-18 22:54:07 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function rmdir($path) {
|
|
|
|
$path = $this->normalizePath($path);
|
|
|
|
|
2014-08-25 16:28:35 +04:00
|
|
|
if (!$this->is_dir($path) || !$this->isDeletable($path)) {
|
2013-10-22 16:59:09 +04:00
|
|
|
return false;
|
2012-10-22 00:04:45 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
$dh = $this->opendir($path);
|
|
|
|
while ($file = readdir($dh)) {
|
2015-09-21 15:09:28 +03:00
|
|
|
if (\OC\Files\Filesystem::isIgnoredDir($file)) {
|
2013-10-22 16:59:09 +04:00
|
|
|
continue;
|
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
if ($this->is_dir($path . '/' . $file)) {
|
|
|
|
$this->rmdir($path . '/' . $file);
|
|
|
|
} else {
|
|
|
|
$this->unlink($path . '/' . $file);
|
|
|
|
}
|
|
|
|
}
|
2012-04-18 22:54:07 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->objectStore->deleteObject($path . '/');
|
2014-03-26 20:17:06 +04:00
|
|
|
$this->objectCache->remove($path . '/');
|
2018-02-12 17:08:27 +03:00
|
|
|
} catch (BadResponseError $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2013-10-22 16:59:09 +04:00
|
|
|
return false;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
return true;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function opendir($path) {
|
|
|
|
$path = $this->normalizePath($path);
|
2012-04-18 22:54:07 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
if ($path === '.') {
|
|
|
|
$path = '';
|
2012-11-30 19:27:11 +04:00
|
|
|
} else {
|
2013-10-22 16:59:09 +04:00
|
|
|
$path .= '/';
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
// $path = str_replace('%23', '#', $path); // the prefix is sent as a query param, so revert the encoding of #
|
2014-06-25 16:56:16 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2018-02-12 17:08:27 +03:00
|
|
|
$files = [];
|
|
|
|
$objects = $this->getContainer()->listObjects([
|
2013-10-23 10:07:59 +04:00
|
|
|
'prefix' => $path,
|
|
|
|
'delimiter' => '/'
|
2018-02-12 17:08:27 +03:00
|
|
|
]);
|
2013-10-22 16:59:09 +04:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
/** @var StorageObject $object */
|
2014-06-25 16:56:16 +04:00
|
|
|
foreach ($objects as $object) {
|
2018-02-12 17:08:27 +03:00
|
|
|
$file = basename($object->name);
|
2017-07-25 15:29:44 +03:00
|
|
|
if ($file !== basename($path) && $file !== '.') {
|
2013-10-22 16:59:09 +04:00
|
|
|
$files[] = $file;
|
|
|
|
}
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2015-03-10 18:30:13 +03:00
|
|
|
return IteratorDirectory::wrap($files);
|
2014-09-05 15:22:35 +04:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2013-10-22 16:59:09 +04:00
|
|
|
return false;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function stat($path) {
|
|
|
|
$path = $this->normalizePath($path);
|
|
|
|
|
2014-09-05 15:22:35 +04:00
|
|
|
if ($path === '.') {
|
|
|
|
$path = '';
|
|
|
|
} else if ($this->is_dir($path)) {
|
2013-10-22 16:59:09 +04:00
|
|
|
$path .= '/';
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2014-03-26 20:17:06 +04:00
|
|
|
$object = $this->fetchObject($path);
|
|
|
|
if (!$object) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-12 17:08:27 +03:00
|
|
|
} catch (BadResponseError $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2013-10-22 16:59:09 +04:00
|
|
|
return false;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
$dateTime = $object->lastModified ? \DateTime::createFromFormat(\DateTime::RFC1123, $object->lastModified) : false;
|
|
|
|
$mtime = $dateTime ? $dateTime->getTimestamp() : null;
|
2014-06-25 16:56:16 +04:00
|
|
|
$objectMetadata = $object->getMetadata();
|
2018-02-12 17:08:27 +03:00
|
|
|
if (isset($objectMetadata['timestamp'])) {
|
|
|
|
$mtime = $objectMetadata['timestamp'];
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
2014-03-26 20:20:40 +04:00
|
|
|
if (!empty($mtime)) {
|
|
|
|
$mtime = floor($mtime);
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
$stat = array();
|
2018-02-12 17:08:27 +03:00
|
|
|
$stat['size'] = (int)$object->contentLength;
|
2013-10-22 16:59:09 +04:00
|
|
|
$stat['mtime'] = $mtime;
|
|
|
|
$stat['atime'] = time();
|
|
|
|
return $stat;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function filetype($path) {
|
2013-10-22 16:59:09 +04:00
|
|
|
$path = $this->normalizePath($path);
|
|
|
|
|
|
|
|
if ($path !== '.' && $this->doesObjectExist($path)) {
|
2012-04-18 22:54:07 +04:00
|
|
|
return 'file';
|
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
|
|
|
if ($path !== '.') {
|
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->doesObjectExist($path)) {
|
|
|
|
return 'dir';
|
|
|
|
}
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function unlink($path) {
|
|
|
|
$path = $this->normalizePath($path);
|
2012-04-18 22:54:07 +04:00
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
if ($this->is_dir($path)) {
|
|
|
|
return $this->rmdir($path);
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->objectStore->deleteObject($path);
|
2014-03-26 20:17:06 +04:00
|
|
|
$this->objectCache->remove($path);
|
|
|
|
$this->objectCache->remove($path . '/');
|
2018-02-12 17:08:27 +03:00
|
|
|
} catch (BadResponseError $e) {
|
2014-03-26 20:17:06 +04:00
|
|
|
if ($e->getResponse()->getStatusCode() !== 404) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2018-02-12 17:08:27 +03:00
|
|
|
throw $e;
|
2014-03-26 20:17:06 +04:00
|
|
|
}
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
|
|
|
return true;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-02 22:53:02 +04:00
|
|
|
public function fopen($path, $mode) {
|
2013-10-22 16:59:09 +04:00
|
|
|
$path = $this->normalizePath($path);
|
|
|
|
|
|
|
|
switch ($mode) {
|
2017-04-04 14:12:29 +03:00
|
|
|
case 'a':
|
|
|
|
case 'ab':
|
|
|
|
case 'a+':
|
|
|
|
return false;
|
2012-04-18 22:54:07 +04:00
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2018-02-12 17:08:27 +03:00
|
|
|
return $this->objectStore->readObject($path);
|
|
|
|
} catch (BadResponseError $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2013-10-22 16:59:09 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-18 22:54:07 +04:00
|
|
|
case 'w':
|
|
|
|
case 'wb':
|
|
|
|
case 'r+':
|
|
|
|
case 'w+':
|
|
|
|
case 'wb+':
|
|
|
|
case 'x':
|
|
|
|
case 'x+':
|
|
|
|
case 'c':
|
|
|
|
case 'c+':
|
2013-10-22 16:59:09 +04:00
|
|
|
if (strrpos($path, '.') !== false) {
|
|
|
|
$ext = substr($path, strrpos($path, '.'));
|
|
|
|
} else {
|
|
|
|
$ext = '';
|
|
|
|
}
|
2015-08-19 00:49:29 +03:00
|
|
|
$tmpFile = \OCP\Files::tmpFile($ext);
|
2015-08-10 04:06:45 +03:00
|
|
|
// Fetch existing file if required
|
|
|
|
if ($mode[0] !== 'w' && $this->file_exists($path)) {
|
|
|
|
if ($mode[0] === 'x') {
|
|
|
|
// File cannot already exist
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
$source = $this->fopen($path, 'r');
|
|
|
|
file_put_contents($tmpFile, $source);
|
|
|
|
}
|
2017-01-03 19:26:44 +03:00
|
|
|
$handle = fopen($tmpFile, $mode);
|
|
|
|
return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
|
|
|
|
$this->writeBack($tmpFile, $path);
|
|
|
|
});
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function touch($path, $mtime = null) {
|
|
|
|
$path = $this->normalizePath($path);
|
2014-06-25 16:56:16 +04:00
|
|
|
if (is_null($mtime)) {
|
|
|
|
$mtime = time();
|
|
|
|
}
|
2018-02-12 17:08:27 +03:00
|
|
|
$metadata = ['timestamp' => $mtime];
|
2013-10-22 16:59:09 +04:00
|
|
|
if ($this->file_exists($path)) {
|
2017-05-10 15:12:58 +03:00
|
|
|
if ($this->is_dir($path) && $path !== '.') {
|
2013-10-22 16:59:09 +04:00
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:17:06 +04:00
|
|
|
$object = $this->fetchObject($path);
|
2018-02-12 17:08:27 +03:00
|
|
|
if ($object->mergeMetadata($metadata)) {
|
2014-03-26 20:17:06 +04:00
|
|
|
// invalidate target object to force repopulation on fetch
|
|
|
|
$this->objectCache->remove($path);
|
|
|
|
}
|
2014-06-25 16:56:16 +04:00
|
|
|
return true;
|
2013-10-22 16:59:09 +04:00
|
|
|
} else {
|
2015-08-19 00:49:29 +03:00
|
|
|
$mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path);
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->getContainer()->createObject([
|
|
|
|
'name' => $path,
|
|
|
|
'content' => '',
|
|
|
|
'headers' => ['content-type' => 'httpd/unix-directory']
|
|
|
|
]);
|
2014-03-26 20:17:06 +04:00
|
|
|
// invalidate target object to force repopulation on fetch
|
|
|
|
$this->objectCache->remove($path);
|
2014-06-25 16:56:16 +04:00
|
|
|
return true;
|
2012-04-19 02:25:47 +04:00
|
|
|
}
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-02 22:53:02 +04:00
|
|
|
public function copy($path1, $path2) {
|
2013-10-22 16:59:09 +04:00
|
|
|
$path1 = $this->normalizePath($path1);
|
|
|
|
$path2 = $this->normalizePath($path2);
|
2012-04-18 22:54:07 +04:00
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
$fileType = $this->filetype($path1);
|
2018-02-12 17:08:27 +03:00
|
|
|
if ($fileType) {
|
2014-06-25 16:56:16 +04:00
|
|
|
// make way
|
|
|
|
$this->unlink($path2);
|
2018-02-12 17:08:27 +03:00
|
|
|
}
|
2014-06-25 16:56:16 +04:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
if ($fileType === 'file') {
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2014-03-26 20:17:06 +04:00
|
|
|
$source = $this->fetchObject($path1);
|
2018-02-12 17:08:27 +03:00
|
|
|
$source->copy([
|
|
|
|
'destination' => $this->bucket . '/' . $path2
|
|
|
|
]);
|
2014-03-26 20:17:06 +04:00
|
|
|
// invalidate target object to force repopulation on fetch
|
|
|
|
$this->objectCache->remove($path2);
|
|
|
|
$this->objectCache->remove($path2 . '/');
|
2018-02-12 17:08:27 +03:00
|
|
|
} catch (BadResponseError $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2013-10-22 16:59:09 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-06-25 16:56:16 +04:00
|
|
|
|
|
|
|
} else if ($fileType === 'dir') {
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2014-03-26 20:17:06 +04:00
|
|
|
$source = $this->fetchObject($path1 . '/');
|
2018-02-12 17:08:27 +03:00
|
|
|
$source->copy([
|
|
|
|
'destination' => $this->bucket . '/' . $path2 . '/'
|
|
|
|
]);
|
2014-03-26 20:17:06 +04:00
|
|
|
// invalidate target object to force repopulation on fetch
|
|
|
|
$this->objectCache->remove($path2);
|
|
|
|
$this->objectCache->remove($path2 . '/');
|
2018-02-12 17:08:27 +03:00
|
|
|
} catch (BadResponseError $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'level' => \OCP\Util::ERROR,
|
|
|
|
'app' => 'files_external',
|
|
|
|
]);
|
2013-10-22 16:59:09 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-11 17:52:21 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
$dh = $this->opendir($path1);
|
|
|
|
while ($file = readdir($dh)) {
|
2015-09-21 15:09:28 +03:00
|
|
|
if (\OC\Files\Filesystem::isIgnoredDir($file)) {
|
2013-10-22 16:59:09 +04:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-19 02:25:47 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
$source = $path1 . '/' . $file;
|
|
|
|
$target = $path2 . '/' . $file;
|
|
|
|
$this->copy($source, $target);
|
|
|
|
}
|
2014-06-25 16:56:16 +04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//file does not exist
|
|
|
|
return false;
|
2012-04-19 02:25:47 +04:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
|
|
|
return true;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function rename($path1, $path2) {
|
|
|
|
$path1 = $this->normalizePath($path1);
|
|
|
|
$path2 = $this->normalizePath($path2);
|
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
$fileType = $this->filetype($path1);
|
|
|
|
|
|
|
|
if ($fileType === 'dir' || $fileType === 'file') {
|
|
|
|
// copy
|
2013-10-22 16:59:09 +04:00
|
|
|
if ($this->copy($path1, $path2) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
// cleanup
|
2013-10-22 16:59:09 +04:00
|
|
|
if ($this->unlink($path1) === false) {
|
2018-02-12 17:08:27 +03:00
|
|
|
throw new \Exception('failed to remove original');
|
2013-10-22 16:59:09 +04:00
|
|
|
$this->unlink($path2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
return true;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
return false;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
2012-04-19 02:25:47 +04:00
|
|
|
}
|
|
|
|
|
2014-10-21 18:18:44 +04:00
|
|
|
/**
|
|
|
|
* Returns the initialized object store container.
|
|
|
|
*
|
2018-02-12 17:08:27 +03:00
|
|
|
* @return \OpenStack\ObjectStore\v1\Models\Container
|
|
|
|
* @throws \OCP\Files\StorageAuthException
|
|
|
|
* @throws \OCP\Files\StorageNotAvailableException
|
2014-10-21 18:18:44 +04:00
|
|
|
*/
|
|
|
|
public function getContainer() {
|
2018-02-12 17:08:27 +03:00
|
|
|
if (is_null($this->container)) {
|
|
|
|
$this->container = $this->connectionFactory->getContainer();
|
2014-10-21 18:18:44 +04:00
|
|
|
|
2018-02-12 17:08:27 +03:00
|
|
|
if (!$this->file_exists('.')) {
|
|
|
|
$this->mkdir('.');
|
|
|
|
}
|
2014-10-21 18:18:44 +04:00
|
|
|
}
|
|
|
|
return $this->container;
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:26:44 +03:00
|
|
|
public function writeBack($tmpFile, $path) {
|
2014-06-25 16:56:16 +04:00
|
|
|
$fileData = fopen($tmpFile, 'r');
|
2018-02-12 17:08:27 +03:00
|
|
|
$this->objectStore->writeObject($path, $fileData);
|
2014-03-26 20:17:06 +04:00
|
|
|
// invalidate target object to force repopulation on fetch
|
2017-08-14 16:03:30 +03:00
|
|
|
$this->objectCache->remove($path);
|
2013-10-22 16:59:09 +04:00
|
|
|
unlink($tmpFile);
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
2013-08-02 17:44:56 +04:00
|
|
|
|
2014-09-05 15:23:10 +04:00
|
|
|
public function hasUpdated($path, $time) {
|
|
|
|
if ($this->is_file($path)) {
|
|
|
|
return parent::hasUpdated($path, $time);
|
|
|
|
}
|
|
|
|
$path = $this->normalizePath($path);
|
|
|
|
$dh = $this->opendir($path);
|
|
|
|
$content = array();
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
$content[] = $file;
|
|
|
|
}
|
|
|
|
if ($path === '.') {
|
|
|
|
$path = '';
|
|
|
|
}
|
|
|
|
$cachedContent = $this->getCache()->getFolderContents($path);
|
|
|
|
$cachedNames = array_map(function ($content) {
|
|
|
|
return $content['name'];
|
|
|
|
}, $cachedContent);
|
|
|
|
sort($cachedNames);
|
|
|
|
sort($content);
|
2017-05-10 15:12:58 +03:00
|
|
|
return $cachedNames !== $content;
|
2014-09-05 15:23:10 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 17:44:56 +04:00
|
|
|
/**
|
|
|
|
* check if curl is installed
|
|
|
|
*/
|
|
|
|
public static function checkDependencies() {
|
2015-03-12 23:43:41 +03:00
|
|
|
return true;
|
2013-08-02 17:44:56 +04:00
|
|
|
}
|
|
|
|
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|