Completed
Push — master ( f42d5b...a9547a )
by Lukas
13:43
created

Swift::readObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Files\ObjectStore;
26
27
use Guzzle\Http\Exception\ClientErrorResponseException;
28
use OCP\Files\ObjectStore\IObjectStore;
29
use OpenCloud\OpenStack;
30
use OpenCloud\Rackspace;
31
32
class Swift implements IObjectStore {
33
34
	/**
35
	 * @var \OpenCloud\OpenStack
36
	 */
37
	private $client;
38
39
	/**
40
	 * @var array
41
	 */
42
	private $params;
43
44
	/**
45
	 * @var \OpenCloud\ObjectStore\Service
46
	 */
47
	private $objectStoreService;
48
49
	/**
50
	 * @var \OpenCloud\ObjectStore\Resource\Container
51
	 */
52
	private $container;
53
54
	public function __construct($params) {
55
		if (isset($params['bucket'])) {
56
			$params['container'] = $params['bucket'];
57
		}
58
		if (!isset($params['container'])) {
59
			$params['container'] = 'owncloud';
60
		}
61
		if (!isset($params['autocreate'])) {
62
			// should only be true for tests
63
			$params['autocreate'] = false;
64
		}
65
66
		if (isset($params['apiKey'])) {
67
			$this->client = new Rackspace($params['url'], $params);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \OpenCloud\Rackspace($params['url'], $params) of type object<OpenCloud\Rackspace> is incompatible with the declared type object<OpenCloud\OpenStack> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
		} else {
69
			$this->client = new OpenStack($params['url'], $params);
70
		}
71
		$this->params = $params;
72
	}
73
74
	protected function init() {
75
		if ($this->container) {
76
			return;
77
		}
78
79
		// the OpenCloud client library will default to 'cloudFiles' if $serviceName is null
80
		$serviceName = null;
81
		if (isset($this->params['serviceName'])) {
82
			$serviceName = $this->params['serviceName'];
83
		}
84
85
		// the OpenCloud client library will default to 'publicURL' if $urlType is null
86
		$urlType = null;
87
		if (isset($this->params['urlType'])) {
88
			$urlType = $this->params['urlType'];
89
		}
90
		$this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType);
91
92
		try {
93
			$this->container = $this->objectStoreService->getContainer($this->params['container']);
94
		} catch (ClientErrorResponseException $ex) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Http\Exception\ClientErrorResponseException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
95
			// if the container does not exist and autocreate is true try to create the container on the fly
96
			if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) {
97
				$this->container = $this->objectStoreService->createContainer($this->params['container']);
98
			} else {
99
				throw $ex;
100
			}
101
		}
102
	}
103
104
	/**
105
	 * @return string the container name where objects are stored
106
	 */
107
	public function getStorageId() {
108
		return $this->params['container'];
109
	}
110
111
	/**
112
	 * @param string $urn the unified resource name used to identify the object
113
	 * @param resource $stream stream with the data to write
114
	 * @throws Exception from openstack lib when something goes wrong
115
	 */
116
	public function writeObject($urn, $stream) {
117
		$this->init();
118
		$this->container->uploadObject($urn, $stream);
119
	}
120
121
	/**
122
	 * @param string $urn the unified resource name used to identify the object
123
	 * @return resource stream with the read data
124
	 * @throws Exception from openstack lib when something goes wrong
125
	 */
126
	public function readObject($urn) {
127
		$this->init();
128
		$object = $this->container->getObject($urn);
129
130
		// we need to keep a reference to objectContent or
131
		// the stream will be closed before we can do anything with it
132
		/** @var $objectContent \Guzzle\Http\EntityBody * */
133
		$objectContent = $object->getContent();
134
		$objectContent->rewind();
135
136
		$stream = $objectContent->getStream();
137
		// save the object content in the context of the stream to prevent it being gc'd until the stream is closed
138
		stream_context_set_option($stream, 'swift','content', $objectContent);
139
140
		return $stream;
141
	}
142
143
	/**
144
	 * @param string $urn Unified Resource Name
145
	 * @return void
146
	 * @throws Exception from openstack lib when something goes wrong
147
	 */
148
	public function deleteObject($urn) {
149
		$this->init();
150
		// see https://github.com/rackspace/php-opencloud/issues/243#issuecomment-30032242
151
		$this->container->dataObject()->setName($urn)->delete();
152
	}
153
154
	public function deleteContainer($recursive = false) {
155
		$this->init();
156
		$this->container->delete($recursive);
157
	}
158
159
}
160