Completed
Pull Request — master (#4112)
by Robin
15:00
created
lib/private/Files/ObjectStore/Swift.php 3 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -28,7 +28,6 @@
 block discarded – undo
28 28
 use OCP\Files\ObjectStore\IObjectStore;
29 29
 use OCP\Files\StorageAuthException;
30 30
 use OCP\Files\StorageNotAvailableException;
31
-use OpenCloud\Common\Exceptions\EndpointError;
32 31
 use OpenCloud\Common\Service\Catalog;
33 32
 use OpenCloud\Common\Service\CatalogItem;
34 33
 use OpenCloud\ObjectStore\Service;
Please login to merge, or discard this patch.
Indentation   +193 added lines, -193 removed lines patch added patch discarded remove patch
@@ -37,198 +37,198 @@
 block discarded – undo
37 37
 
38 38
 class Swift implements IObjectStore {
39 39
 
40
-	/**
41
-	 * @var \OpenCloud\OpenStack
42
-	 */
43
-	private $client;
44
-
45
-	/**
46
-	 * @var array
47
-	 */
48
-	private $params;
49
-
50
-	/**
51
-	 * @var \OpenCloud\ObjectStore\Service
52
-	 */
53
-	private $objectStoreService;
54
-
55
-	/**
56
-	 * @var \OpenCloud\ObjectStore\Resource\Container
57
-	 */
58
-	private $container;
59
-
60
-	public function __construct($params) {
61
-		if (isset($params['bucket'])) {
62
-			$params['container'] = $params['bucket'];
63
-		}
64
-		if (!isset($params['container'])) {
65
-			$params['container'] = 'owncloud';
66
-		}
67
-		if (!isset($params['autocreate'])) {
68
-			// should only be true for tests
69
-			$params['autocreate'] = false;
70
-		}
71
-
72
-		if (isset($params['apiKey'])) {
73
-			$this->client = new Rackspace($params['url'], $params);
74
-		} else {
75
-			$this->client = new OpenStack($params['url'], $params);
76
-		}
77
-		$this->params = $params;
78
-	}
79
-
80
-	protected function init() {
81
-		if ($this->container) {
82
-			return;
83
-		}
84
-
85
-		try {
86
-			$this->client->authenticate();
87
-		} catch (ClientErrorResponseException $e) {
88
-			$statusCode = $e->getResponse()->getStatusCode();
89
-			if ($statusCode == 412) {
90
-				throw new StorageAuthException('Precondition failed, verify the keystone url', $e);
91
-			} else if ($statusCode === 401) {
92
-				throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e);
93
-			} else {
94
-				throw new StorageAuthException('Unknown error', $e);
95
-			}
96
-		}
97
-
98
-		/** @var Catalog $catalog */
99
-		$catalog = $this->client->getCatalog();
100
-
101
-		if (isset($this->params['serviceName'])) {
102
-			$serviceName = $this->params['serviceName'];
103
-		} else {
104
-			$serviceName = Service::DEFAULT_NAME;
105
-		}
106
-
107
-		if (isset($this->params['urlType'])) {
108
-			$urlType = $this->params['urlType'];
109
-			if ($urlType !== 'internalURL' && $urlType !== 'publicURL') {
110
-				throw new StorageNotAvailableException('Invalid url type');
111
-			}
112
-		} else {
113
-			$urlType = Service::DEFAULT_URL_TYPE;
114
-		}
115
-
116
-		$catalogItem = $this->getCatalogForService($catalog, $this->params['serviceName']);
117
-		if (!$catalogItem) {
118
-			$available = implode(', ', $this->getAvailableServiceNames($catalog));
119
-			throw new StorageNotAvailableException(
120
-				"Service $serviceName not found in service catalog, available services: $available"
121
-			);
122
-		} else if (isset($this->params['region'])) {
123
-			$this->validateRegion($catalogItem, $this->params['region']);
124
-		}
125
-
126
-		$this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType);
127
-
128
-		try {
129
-			$this->container = $this->objectStoreService->getContainer($this->params['container']);
130
-		} catch (ClientErrorResponseException $ex) {
131
-			// if the container does not exist and autocreate is true try to create the container on the fly
132
-			if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) {
133
-				$this->container = $this->objectStoreService->createContainer($this->params['container']);
134
-			} else {
135
-				throw $ex;
136
-			}
137
-		}
138
-	}
139
-
140
-	/**
141
-	 * @param Catalog $catalog
142
-	 * @param $name
143
-	 * @return null|CatalogItem
144
-	 */
145
-	private function getCatalogForService(Catalog $catalog, $name) {
146
-		foreach ($catalog->getItems() as $item) {
147
-			/** @var CatalogItem $item */
148
-			if ($item->hasType(Service::DEFAULT_TYPE) && $item->hasName($name)) {
149
-				return $item;
150
-			}
151
-		}
152
-
153
-		return null;
154
-	}
155
-
156
-	private function validateRegion(CatalogItem $item, $region) {
157
-		$endPoints = $item->getEndpoints();
158
-		foreach ($endPoints as $endPoint) {
159
-			if ($endPoint->region === $region) {
160
-				return;
161
-			}
162
-		}
163
-
164
-		$availableRegions = implode(', ', array_map(function ($endpoint) {
165
-			return $endpoint->region;
166
-		}, $endPoints));
167
-
168
-		throw new StorageNotAvailableException("Invalid region '$region', available regions: $availableRegions");
169
-	}
170
-
171
-	private function getAvailableServiceNames(Catalog $catalog) {
172
-		return array_map(function (CatalogItem $item) {
173
-			return $item->getName();
174
-		}, array_filter($catalog->getItems(), function (CatalogItem $item) {
175
-			return $item->hasType(Service::DEFAULT_TYPE);
176
-		}));
177
-	}
178
-
179
-	/**
180
-	 * @return string the container name where objects are stored
181
-	 */
182
-	public function getStorageId() {
183
-		return $this->params['container'];
184
-	}
185
-
186
-	/**
187
-	 * @param string $urn the unified resource name used to identify the object
188
-	 * @param resource $stream stream with the data to write
189
-	 * @throws Exception from openstack lib when something goes wrong
190
-	 */
191
-	public function writeObject($urn, $stream) {
192
-		$this->init();
193
-		$this->container->uploadObject($urn, $stream);
194
-	}
195
-
196
-	/**
197
-	 * @param string $urn the unified resource name used to identify the object
198
-	 * @return resource stream with the read data
199
-	 * @throws Exception from openstack lib when something goes wrong
200
-	 */
201
-	public function readObject($urn) {
202
-		$this->init();
203
-		$object = $this->container->getObject($urn);
204
-
205
-		// we need to keep a reference to objectContent or
206
-		// the stream will be closed before we can do anything with it
207
-		/** @var $objectContent \Guzzle\Http\EntityBody * */
208
-		$objectContent = $object->getContent();
209
-		$objectContent->rewind();
210
-
211
-		$stream = $objectContent->getStream();
212
-		// save the object content in the context of the stream to prevent it being gc'd until the stream is closed
213
-		stream_context_set_option($stream, 'swift', 'content', $objectContent);
214
-
215
-		return $stream;
216
-	}
217
-
218
-	/**
219
-	 * @param string $urn Unified Resource Name
220
-	 * @return void
221
-	 * @throws Exception from openstack lib when something goes wrong
222
-	 */
223
-	public function deleteObject($urn) {
224
-		$this->init();
225
-		// see https://github.com/rackspace/php-opencloud/issues/243#issuecomment-30032242
226
-		$this->container->dataObject()->setName($urn)->delete();
227
-	}
228
-
229
-	public function deleteContainer($recursive = false) {
230
-		$this->init();
231
-		$this->container->delete($recursive);
232
-	}
40
+    /**
41
+     * @var \OpenCloud\OpenStack
42
+     */
43
+    private $client;
44
+
45
+    /**
46
+     * @var array
47
+     */
48
+    private $params;
49
+
50
+    /**
51
+     * @var \OpenCloud\ObjectStore\Service
52
+     */
53
+    private $objectStoreService;
54
+
55
+    /**
56
+     * @var \OpenCloud\ObjectStore\Resource\Container
57
+     */
58
+    private $container;
59
+
60
+    public function __construct($params) {
61
+        if (isset($params['bucket'])) {
62
+            $params['container'] = $params['bucket'];
63
+        }
64
+        if (!isset($params['container'])) {
65
+            $params['container'] = 'owncloud';
66
+        }
67
+        if (!isset($params['autocreate'])) {
68
+            // should only be true for tests
69
+            $params['autocreate'] = false;
70
+        }
71
+
72
+        if (isset($params['apiKey'])) {
73
+            $this->client = new Rackspace($params['url'], $params);
74
+        } else {
75
+            $this->client = new OpenStack($params['url'], $params);
76
+        }
77
+        $this->params = $params;
78
+    }
79
+
80
+    protected function init() {
81
+        if ($this->container) {
82
+            return;
83
+        }
84
+
85
+        try {
86
+            $this->client->authenticate();
87
+        } catch (ClientErrorResponseException $e) {
88
+            $statusCode = $e->getResponse()->getStatusCode();
89
+            if ($statusCode == 412) {
90
+                throw new StorageAuthException('Precondition failed, verify the keystone url', $e);
91
+            } else if ($statusCode === 401) {
92
+                throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e);
93
+            } else {
94
+                throw new StorageAuthException('Unknown error', $e);
95
+            }
96
+        }
97
+
98
+        /** @var Catalog $catalog */
99
+        $catalog = $this->client->getCatalog();
100
+
101
+        if (isset($this->params['serviceName'])) {
102
+            $serviceName = $this->params['serviceName'];
103
+        } else {
104
+            $serviceName = Service::DEFAULT_NAME;
105
+        }
106
+
107
+        if (isset($this->params['urlType'])) {
108
+            $urlType = $this->params['urlType'];
109
+            if ($urlType !== 'internalURL' && $urlType !== 'publicURL') {
110
+                throw new StorageNotAvailableException('Invalid url type');
111
+            }
112
+        } else {
113
+            $urlType = Service::DEFAULT_URL_TYPE;
114
+        }
115
+
116
+        $catalogItem = $this->getCatalogForService($catalog, $this->params['serviceName']);
117
+        if (!$catalogItem) {
118
+            $available = implode(', ', $this->getAvailableServiceNames($catalog));
119
+            throw new StorageNotAvailableException(
120
+                "Service $serviceName not found in service catalog, available services: $available"
121
+            );
122
+        } else if (isset($this->params['region'])) {
123
+            $this->validateRegion($catalogItem, $this->params['region']);
124
+        }
125
+
126
+        $this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType);
127
+
128
+        try {
129
+            $this->container = $this->objectStoreService->getContainer($this->params['container']);
130
+        } catch (ClientErrorResponseException $ex) {
131
+            // if the container does not exist and autocreate is true try to create the container on the fly
132
+            if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) {
133
+                $this->container = $this->objectStoreService->createContainer($this->params['container']);
134
+            } else {
135
+                throw $ex;
136
+            }
137
+        }
138
+    }
139
+
140
+    /**
141
+     * @param Catalog $catalog
142
+     * @param $name
143
+     * @return null|CatalogItem
144
+     */
145
+    private function getCatalogForService(Catalog $catalog, $name) {
146
+        foreach ($catalog->getItems() as $item) {
147
+            /** @var CatalogItem $item */
148
+            if ($item->hasType(Service::DEFAULT_TYPE) && $item->hasName($name)) {
149
+                return $item;
150
+            }
151
+        }
152
+
153
+        return null;
154
+    }
155
+
156
+    private function validateRegion(CatalogItem $item, $region) {
157
+        $endPoints = $item->getEndpoints();
158
+        foreach ($endPoints as $endPoint) {
159
+            if ($endPoint->region === $region) {
160
+                return;
161
+            }
162
+        }
163
+
164
+        $availableRegions = implode(', ', array_map(function ($endpoint) {
165
+            return $endpoint->region;
166
+        }, $endPoints));
167
+
168
+        throw new StorageNotAvailableException("Invalid region '$region', available regions: $availableRegions");
169
+    }
170
+
171
+    private function getAvailableServiceNames(Catalog $catalog) {
172
+        return array_map(function (CatalogItem $item) {
173
+            return $item->getName();
174
+        }, array_filter($catalog->getItems(), function (CatalogItem $item) {
175
+            return $item->hasType(Service::DEFAULT_TYPE);
176
+        }));
177
+    }
178
+
179
+    /**
180
+     * @return string the container name where objects are stored
181
+     */
182
+    public function getStorageId() {
183
+        return $this->params['container'];
184
+    }
185
+
186
+    /**
187
+     * @param string $urn the unified resource name used to identify the object
188
+     * @param resource $stream stream with the data to write
189
+     * @throws Exception from openstack lib when something goes wrong
190
+     */
191
+    public function writeObject($urn, $stream) {
192
+        $this->init();
193
+        $this->container->uploadObject($urn, $stream);
194
+    }
195
+
196
+    /**
197
+     * @param string $urn the unified resource name used to identify the object
198
+     * @return resource stream with the read data
199
+     * @throws Exception from openstack lib when something goes wrong
200
+     */
201
+    public function readObject($urn) {
202
+        $this->init();
203
+        $object = $this->container->getObject($urn);
204
+
205
+        // we need to keep a reference to objectContent or
206
+        // the stream will be closed before we can do anything with it
207
+        /** @var $objectContent \Guzzle\Http\EntityBody * */
208
+        $objectContent = $object->getContent();
209
+        $objectContent->rewind();
210
+
211
+        $stream = $objectContent->getStream();
212
+        // save the object content in the context of the stream to prevent it being gc'd until the stream is closed
213
+        stream_context_set_option($stream, 'swift', 'content', $objectContent);
214
+
215
+        return $stream;
216
+    }
217
+
218
+    /**
219
+     * @param string $urn Unified Resource Name
220
+     * @return void
221
+     * @throws Exception from openstack lib when something goes wrong
222
+     */
223
+    public function deleteObject($urn) {
224
+        $this->init();
225
+        // see https://github.com/rackspace/php-opencloud/issues/243#issuecomment-30032242
226
+        $this->container->dataObject()->setName($urn)->delete();
227
+    }
228
+
229
+    public function deleteContainer($recursive = false) {
230
+        $this->init();
231
+        $this->container->delete($recursive);
232
+    }
233 233
 
234 234
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
 			}
162 162
 		}
163 163
 
164
-		$availableRegions = implode(', ', array_map(function ($endpoint) {
164
+		$availableRegions = implode(', ', array_map(function($endpoint) {
165 165
 			return $endpoint->region;
166 166
 		}, $endPoints));
167 167
 
@@ -169,9 +169,9 @@  discard block
 block discarded – undo
169 169
 	}
170 170
 
171 171
 	private function getAvailableServiceNames(Catalog $catalog) {
172
-		return array_map(function (CatalogItem $item) {
172
+		return array_map(function(CatalogItem $item) {
173 173
 			return $item->getName();
174
-		}, array_filter($catalog->getItems(), function (CatalogItem $item) {
174
+		}, array_filter($catalog->getItems(), function(CatalogItem $item) {
175 175
 			return $item->hasType(Service::DEFAULT_TYPE);
176 176
 		}));
177 177
 	}
Please login to merge, or discard this patch.