From 3d7aff84e53ab344855a83b2162527d0baae954a Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind@owncloud.com>
Date: Fri, 5 Sep 2014 13:22:35 +0200
Subject: [PATCH] Fix configuration of rackspace

---
 apps/files_external/appinfo/app.php |  4 +--
 apps/files_external/lib/swift.php   | 46 ++++++++++++++++++-----------
 2 files changed, 30 insertions(+), 20 deletions(-)

diff --git a/apps/files_external/appinfo/app.php b/apps/files_external/appinfo/app.php
index 50a73bd5f9..3486b8db51 100644
--- a/apps/files_external/appinfo/app.php
+++ b/apps/files_external/appinfo/app.php
@@ -111,9 +111,9 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\Swift', array(
 		'user' => (string)$l->t('Username'),
 		'bucket' => (string)$l->t('Bucket'),
 		'region' => '&'.$l->t('Region (optional for OpenStack Object Storage)'),
-		'key' => '*'.$l->t('API Key (required for Rackspace Cloud Files)'),
+		'key' => '&*'.$l->t('API Key (required for Rackspace Cloud Files)'),
 		'tenant' => '&'.$l->t('Tenantname (required for OpenStack Object Storage)'),
-		'password' => '*'.$l->t('Password (required for OpenStack Object Storage)'),
+		'password' => '&*'.$l->t('Password (required for OpenStack Object Storage)'),
 		'service_name' => '&'.$l->t('Service Name (required for OpenStack Object Storage)'),
 		'url' => '&'.$l->t('URL of identity endpoint (required for OpenStack Object Storage)'),
 		'timeout' => '&'.$l->t('Timeout of HTTP requests in seconds'),
diff --git a/apps/files_external/lib/swift.php b/apps/files_external/lib/swift.php
index 47ab329467..32c7dd57af 100644
--- a/apps/files_external/lib/swift.php
+++ b/apps/files_external/lib/swift.php
@@ -26,6 +26,7 @@ use Guzzle\Http\Exception\ClientErrorResponseException;
 use OpenCloud;
 use OpenCloud\Common\Exceptions;
 use OpenCloud\OpenStack;
+use OpenCloud\Rackspace;
 use OpenCloud\ObjectStore\Resource\DataObject;
 use OpenCloud\ObjectStore\Exception;
 
@@ -67,15 +68,16 @@ class Swift extends \OC\Files\Storage\Common {
 		return $path;
 	}
 
-	const SUBCONTAINER_FILE='.subcontainers';
+	const SUBCONTAINER_FILE = '.subcontainers';
 
 	/**
 	 * translate directory path to container name
+	 *
 	 * @param string $path
 	 * @return string
 	 */
 	private function getContainerName($path) {
-		$path=trim(trim($this->root, '/') . "/".$path, '/.');
+		$path = trim(trim($this->root, '/') . "/" . $path, '/.');
 		return str_replace('/', '\\', $path);
 	}
 
@@ -93,20 +95,21 @@ class Swift extends \OC\Files\Storage\Common {
 	}
 
 	public function __construct($params) {
-		if ((!isset($params['key']) and !isset($params['password']))
-		 	or !isset($params['user']) or !isset($params['bucket'])
-			or !isset($params['region'])) {
+		if ((empty($params['key']) and empty($params['password']))
+			or empty($params['user']) or empty($params['bucket'])
+			or empty($params['region'])
+		) {
 			throw new \Exception("API Key or password, Username, Bucket and Region have to be configured.");
 		}
 
 		$this->id = 'swift::' . $params['user'] . md5($params['bucket']);
 		$this->bucket = $params['bucket'];
 
-		if (!isset($params['url'])) {
+		if (empty($params['url'])) {
 			$params['url'] = 'https://identity.api.rackspacecloud.com/v2.0/';
 		}
 
-		if (!isset($params['service_name'])) {
+		if (empty($params['service_name'])) {
 			$params['service_name'] = 'cloudFiles';
 		}
 
@@ -114,21 +117,25 @@ class Swift extends \OC\Files\Storage\Common {
 			'username' => $params['user'],
 		);
 
-		if (isset($params['password'])) {
+		if (!empty($params['password'])) {
 			$settings['password'] = $params['password'];
-		} else if (isset($params['key'])) {
+		} else if (!empty($params['key'])) {
 			$settings['apiKey'] = $params['key'];
 		}
 
-		if (isset($params['tenant'])) {
+		if (!empty($params['tenant'])) {
 			$settings['tenantName'] = $params['tenant'];
 		}
 
-		if (isset($params['timeout'])) {
+		if (!empty($params['timeout'])) {
 			$settings['timeout'] = $params['timeout'];
 		}
 
-		$this->anchor = new OpenStack($params['url'], $settings);
+		if (isset($settings['apiKey'])) {
+			$this->anchor = new Rackspace($params['url'], $settings);
+		} else {
+			$this->anchor = new OpenStack($params['url'], $settings);
+		}
 
 		$this->connection = $this->anchor->objectStoreService($params['service_name'], $params['region']);
 
@@ -150,7 +157,7 @@ class Swift extends \OC\Files\Storage\Common {
 			return false;
 		}
 
-		if($path !== '.') {
+		if ($path !== '.') {
 			$path .= '/';
 		}
 
@@ -236,7 +243,7 @@ class Swift extends \OC\Files\Storage\Common {
 
 			\OC\Files\Stream\Dir::register('swift' . $path, $files);
 			return opendir('fakedir://swift' . $path);
-		} catch (Exception $e) {
+		} catch (\Exception $e) {
 			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
 			return false;
 		}
@@ -246,11 +253,14 @@ class Swift extends \OC\Files\Storage\Common {
 	public function stat($path) {
 		$path = $this->normalizePath($path);
 
-		if ($this->is_dir($path) && $path != '.') {
+		if ($path === '.') {
+			$path = '';
+		} else if ($this->is_dir($path)) {
 			$path .= '/';
 		}
 
 		try {
+			/** @var DataObject $object */
 			$object = $this->container->getPartialObject($path);
 		} catch (ClientErrorResponseException $e) {
 			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
@@ -274,7 +284,7 @@ class Swift extends \OC\Files\Storage\Common {
 		}
 
 		$stat = array();
-		$stat['size'] = (int) $object->getContentLength();
+		$stat['size'] = (int)$object->getContentLength();
 		$stat['mtime'] = $mtime;
 		$stat['atime'] = time();
 		return $stat;
@@ -416,7 +426,7 @@ class Swift extends \OC\Files\Storage\Common {
 
 			try {
 				$source = $this->container->getPartialObject($path1);
-				$source->copy($this->bucket.'/'.$path2);
+				$source->copy($this->bucket . '/' . $path2);
 			} catch (ClientErrorResponseException $e) {
 				\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
 				return false;
@@ -429,7 +439,7 @@ class Swift extends \OC\Files\Storage\Common {
 
 			try {
 				$source = $this->container->getPartialObject($path1 . '/');
-				$source->copy($this->bucket.'/'.$path2 . '/');
+				$source->copy($this->bucket . '/' . $path2 . '/');
 			} catch (ClientErrorResponseException $e) {
 				\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
 				return false;
-- 
GitLab