2012-04-18 22:54:07 +04:00
|
|
|
<?php
|
|
|
|
/**
|
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>
|
|
|
|
* @author Felix Moeller <mail@felixmoeller.de>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
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>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
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
|
|
|
*
|
2016-01-12 17:02:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
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
|
|
|
|
2012-09-07 20:30:48 +04:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2015-05-20 10:04:43 +03:00
|
|
|
use Guzzle\Http\Url;
|
2014-06-25 16:56:16 +04:00
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException;
|
2015-03-10 18:30:13 +03:00
|
|
|
use Icewind\Streams\IteratorDirectory;
|
2014-06-25 16:56:16 +04:00
|
|
|
use OpenCloud;
|
|
|
|
use OpenCloud\Common\Exceptions;
|
|
|
|
use OpenCloud\OpenStack;
|
2014-09-05 15:22:35 +04:00
|
|
|
use OpenCloud\Rackspace;
|
2014-06-25 16:56:16 +04:00
|
|
|
use OpenCloud\ObjectStore\Resource\DataObject;
|
|
|
|
use OpenCloud\ObjectStore\Exception;
|
2013-10-22 16:59:09 +04:00
|
|
|
|
|
|
|
class Swift extends \OC\Files\Storage\Common {
|
|
|
|
|
2014-05-16 00:47:28 +04:00
|
|
|
/**
|
2014-06-25 16:56:16 +04:00
|
|
|
* @var \OpenCloud\ObjectStore\Service
|
2014-05-16 00:47:28 +04:00
|
|
|
*/
|
2013-10-22 16:59:09 +04:00
|
|
|
private $connection;
|
2014-05-16 00:47:28 +04:00
|
|
|
/**
|
2014-06-25 16:56:16 +04:00
|
|
|
* @var \OpenCloud\ObjectStore\Resource\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 \OpenCloud\OpenStack
|
|
|
|
*/
|
2013-10-22 16:59:09 +04:00
|
|
|
private $anchor;
|
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;
|
2014-05-16 00:47:28 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-10-22 16:59:09 +04:00
|
|
|
private static $tmpFiles = array();
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2013-10-22 16:59:09 +04:00
|
|
|
private function normalizePath($path) {
|
|
|
|
$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
|
|
|
*/
|
2013-08-02 17:44:56 +04:00
|
|
|
private function getContainerName($path) {
|
2014-09-05 15:22:35 +04:00
|
|
|
$path = trim(trim($this->root, '/') . "/" . $path, '/.');
|
2013-08-02 17:44:56 +04:00
|
|
|
return str_replace('/', '\\', $path);
|
2013-06-01 13:28:02 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
*/
|
2013-10-22 16:59:09 +04:00
|
|
|
private function doesObjectExist($path) {
|
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getContainer()->getPartialObject($path);
|
2013-10-22 16:59:09 +04:00
|
|
|
return true;
|
2014-06-25 16:56:16 +04:00
|
|
|
} catch (ClientErrorResponseException $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) {
|
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
}
|
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
|
|
|
public function __construct($params) {
|
2014-09-05 15:22:35 +04:00
|
|
|
if ((empty($params['key']) and empty($params['password']))
|
|
|
|
or empty($params['user']) or empty($params['bucket'])
|
|
|
|
or empty($params['region'])
|
|
|
|
) {
|
2013-10-22 16:59:09 +04:00
|
|
|
throw new \Exception("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
|
|
|
|
|
|
|
$this->id = 'swift::' . $params['user'] . md5($params['bucket']);
|
2015-05-20 10:04:43 +03:00
|
|
|
|
|
|
|
$bucketUrl = Url::factory($params['bucket']);
|
|
|
|
if ($bucketUrl->isAbsolute()) {
|
|
|
|
$this->bucket = end(($bucketUrl->getPathSegments()));
|
|
|
|
$params['endpoint_url'] = $bucketUrl->addPath('..')->normalizePath();
|
|
|
|
} else {
|
|
|
|
$this->bucket = $params['bucket'];
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->params = $params;
|
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 {
|
2014-06-25 16:56:16 +04:00
|
|
|
$customHeaders = array('content-type' => 'httpd/unix-directory');
|
|
|
|
$metadataHeaders = DataObject::stockHeaders(array());
|
|
|
|
$allHeaders = $customHeaders + $metadataHeaders;
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getContainer()->uploadObject($path, '', $allHeaders);
|
2013-10-22 16:59:09 +04:00
|
|
|
} catch (Exceptions\CreateUpdateError $e) {
|
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
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 {
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getContainer()->dataObject()->setName($path . '/')->delete();
|
2013-10-22 16:59:09 +04:00
|
|
|
} catch (Exceptions\DeleteError $e) {
|
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
$path = str_replace('%23', '#', $path); // the prefix is sent as a query param, so revert the encoding of #
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
|
|
|
$files = array();
|
2014-06-25 16:56:16 +04:00
|
|
|
/** @var OpenCloud\Common\Collection $objects */
|
2014-10-21 18:18:44 +04:00
|
|
|
$objects = $this->getContainer()->objectList(array(
|
2013-10-23 10:07:59 +04:00
|
|
|
'prefix' => $path,
|
|
|
|
'delimiter' => '/'
|
2013-10-22 16:59:09 +04:00
|
|
|
));
|
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
/** @var OpenCloud\ObjectStore\Resource\DataObject $object */
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
$file = basename($object->getName());
|
2013-10-22 16:59:09 +04:00
|
|
|
if ($file !== basename($path)) {
|
|
|
|
$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) {
|
2013-10-22 16:59:09 +04:00
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
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-09-05 15:22:35 +04:00
|
|
|
/** @var DataObject $object */
|
2014-10-21 18:18:44 +04:00
|
|
|
$object = $this->getContainer()->getPartialObject($path);
|
2014-06-25 16:56:16 +04:00
|
|
|
} catch (ClientErrorResponseException $e) {
|
2013-10-22 16:59:09 +04:00
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
return false;
|
2012-04-18 22:54:07 +04:00
|
|
|
}
|
|
|
|
|
2014-06-25 16:56:16 +04:00
|
|
|
$dateTime = \DateTime::createFromFormat(\DateTime::RFC1123, $object->getLastModified());
|
|
|
|
if ($dateTime !== false) {
|
|
|
|
$mtime = $dateTime->getTimestamp();
|
|
|
|
} else {
|
|
|
|
$mtime = null;
|
|
|
|
}
|
|
|
|
$objectMetadata = $object->getMetadata();
|
|
|
|
$metaTimestamp = $objectMetadata->getProperty('timestamp');
|
|
|
|
if (isset($metaTimestamp)) {
|
|
|
|
$mtime = $metaTimestamp;
|
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();
|
2014-09-05 15:22:35 +04:00
|
|
|
$stat['size'] = (int)$object->getContentLength();
|
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 {
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getContainer()->dataObject()->setName($path)->delete();
|
2014-06-25 16:56:16 +04:00
|
|
|
} catch (ClientErrorResponseException $e) {
|
2013-10-22 16:59:09 +04:00
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
2012-04-18 22:54:07 +04:00
|
|
|
return false;
|
|
|
|
}
|
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) {
|
2012-04-18 22:54:07 +04:00
|
|
|
case 'r':
|
|
|
|
case 'rb':
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2015-05-28 11:18:06 +03:00
|
|
|
$c = $this->getContainer();
|
|
|
|
$streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory();
|
|
|
|
$streamInterface = $streamFactory->fromRequest(
|
|
|
|
$c->getClient()
|
|
|
|
->get($c->getUrl($path)));
|
|
|
|
$streamInterface->rewind();
|
|
|
|
$stream = $streamInterface->getStream();
|
|
|
|
stream_context_set_option($stream, 'swift','content', $streamInterface);
|
2016-01-12 08:47:52 +03:00
|
|
|
if(!strrpos($streamInterface
|
|
|
|
->getMetaData('wrapper_data')[0], '404 Not Found')) {
|
2015-12-10 08:36:28 +03:00
|
|
|
return $stream;
|
|
|
|
}
|
2012-10-11 17:52:21 +04:00
|
|
|
return false;
|
2015-05-28 11:18:06 +03:00
|
|
|
} catch (\Guzzle\Http\Exception\BadResponseException $e) {
|
2013-10-22 16:59:09 +04:00
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-18 22:54:07 +04:00
|
|
|
case 'w':
|
|
|
|
case 'wb':
|
|
|
|
case 'a':
|
|
|
|
case 'ab':
|
|
|
|
case 'r+':
|
|
|
|
case 'w+':
|
|
|
|
case 'wb+':
|
|
|
|
case 'a+':
|
|
|
|
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);
|
2013-01-28 18:34:15 +04:00
|
|
|
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
|
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);
|
2015-08-10 04:06:45 +03:00
|
|
|
// Seek to end if required
|
|
|
|
if ($mode[0] === 'a') {
|
|
|
|
fseek($tmpFile, 0, SEEK_END);
|
|
|
|
}
|
2013-10-22 16:59:09 +04:00
|
|
|
}
|
|
|
|
self::$tmpFiles[$tmpFile] = $path;
|
2012-04-18 22:54:07 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
return fopen('close://' . $tmpFile, $mode);
|
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();
|
|
|
|
}
|
|
|
|
$metadata = array('timestamp' => $mtime);
|
2013-10-22 16:59:09 +04:00
|
|
|
if ($this->file_exists($path)) {
|
|
|
|
if ($this->is_dir($path) && $path != '.') {
|
|
|
|
$path .= '/';
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:18:44 +04:00
|
|
|
$object = $this->getContainer()->getPartialObject($path);
|
2014-06-25 16:56:16 +04:00
|
|
|
$object->saveMetadata($metadata);
|
|
|
|
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);
|
2014-10-21 14:58:26 +04:00
|
|
|
$customHeaders = array('content-type' => $mimeType);
|
2014-06-25 16:56:16 +04:00
|
|
|
$metadataHeaders = DataObject::stockHeaders($metadata);
|
|
|
|
$allHeaders = $customHeaders + $metadataHeaders;
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getContainer()->uploadObject($path, '', $allHeaders);
|
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);
|
|
|
|
if ($fileType === 'file') {
|
|
|
|
|
|
|
|
// make way
|
|
|
|
$this->unlink($path2);
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
$source = $this->getContainer()->getPartialObject($path1);
|
2014-09-05 15:22:35 +04:00
|
|
|
$source->copy($this->bucket . '/' . $path2);
|
2014-06-25 16:56:16 +04:00
|
|
|
} catch (ClientErrorResponseException $e) {
|
2013-10-22 16:59:09 +04:00
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-25 16:56:16 +04:00
|
|
|
|
|
|
|
} else if ($fileType === 'dir') {
|
|
|
|
|
|
|
|
// make way
|
|
|
|
$this->unlink($path2);
|
2012-10-11 17:52:21 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
try {
|
2014-10-21 18:18:44 +04:00
|
|
|
$source = $this->getContainer()->getPartialObject($path1 . '/');
|
2014-09-05 15:22:35 +04:00
|
|
|
$source->copy($this->bucket . '/' . $path2 . '/');
|
2014-06-25 16:56:16 +04:00
|
|
|
} catch (ClientErrorResponseException $e) {
|
2013-10-22 16:59:09 +04:00
|
|
|
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
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') {
|
|
|
|
|
|
|
|
// make way
|
|
|
|
$this->unlink($path2);
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
$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 connection
|
|
|
|
*
|
|
|
|
* @return OpenCloud\ObjectStore\Service connected client
|
|
|
|
* @throws \Exception if connection could not be made
|
|
|
|
*/
|
2013-10-22 16:59:09 +04:00
|
|
|
public function getConnection() {
|
2014-10-21 18:18:44 +04:00
|
|
|
if (!is_null($this->connection)) {
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings = array(
|
|
|
|
'username' => $this->params['user'],
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($this->params['password'])) {
|
|
|
|
$settings['password'] = $this->params['password'];
|
|
|
|
} else if (!empty($this->params['key'])) {
|
|
|
|
$settings['apiKey'] = $this->params['key'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->params['tenant'])) {
|
|
|
|
$settings['tenantName'] = $this->params['tenant'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->params['timeout'])) {
|
|
|
|
$settings['timeout'] = $this->params['timeout'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($settings['apiKey'])) {
|
|
|
|
$this->anchor = new Rackspace($this->params['url'], $settings);
|
|
|
|
} else {
|
|
|
|
$this->anchor = new OpenStack($this->params['url'], $settings);
|
|
|
|
}
|
|
|
|
|
2015-05-20 10:04:43 +03:00
|
|
|
$connection = $this->anchor->objectStoreService($this->params['service_name'], $this->params['region']);
|
|
|
|
|
|
|
|
if (!empty($this->params['endpoint_url'])) {
|
|
|
|
$endpoint = $connection->getEndpoint();
|
|
|
|
$endpoint->setPublicUrl($this->params['endpoint_url']);
|
|
|
|
$endpoint->setPrivateUrl($this->params['endpoint_url']);
|
|
|
|
$connection->setEndpoint($endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->connection = $connection;
|
2014-10-21 18:18:44 +04:00
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:18:44 +04:00
|
|
|
/**
|
|
|
|
* Returns the initialized object store container.
|
|
|
|
*
|
|
|
|
* @return OpenCloud\ObjectStore\Resource\Container
|
|
|
|
*/
|
|
|
|
public function getContainer() {
|
|
|
|
if (!is_null($this->container)) {
|
|
|
|
return $this->container;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->container = $this->getConnection()->getContainer($this->bucket);
|
|
|
|
} catch (ClientErrorResponseException $e) {
|
|
|
|
$this->container = $this->getConnection()->createContainer($this->bucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->file_exists('.')) {
|
|
|
|
$this->mkdir('.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->container;
|
|
|
|
}
|
|
|
|
|
2013-10-22 16:59:09 +04:00
|
|
|
public function writeBack($tmpFile) {
|
|
|
|
if (!isset(self::$tmpFiles[$tmpFile])) {
|
|
|
|
return false;
|
2012-04-19 02:25:47 +04:00
|
|
|
}
|
2014-06-25 16:56:16 +04:00
|
|
|
$fileData = fopen($tmpFile, 'r');
|
2014-10-21 18:18:44 +04:00
|
|
|
$this->getContainer()->uploadObject(self::$tmpFiles[$tmpFile], $fileData);
|
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);
|
|
|
|
return $cachedNames != $content;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|