@@ -34,108 +34,108 @@ |
||
34 | 34 | use OCP\Files\StorageAuthException; |
35 | 35 | |
36 | 36 | class Swift implements IObjectStore { |
37 | - /** |
|
38 | - * @var array |
|
39 | - */ |
|
40 | - private $params; |
|
41 | - |
|
42 | - /** @var SwiftFactory */ |
|
43 | - private $swiftFactory; |
|
44 | - |
|
45 | - public function __construct($params, SwiftFactory $connectionFactory = null) { |
|
46 | - $this->swiftFactory = $connectionFactory ?: new SwiftFactory( |
|
47 | - \OC::$server->getMemCacheFactory()->createDistributed('swift::'), |
|
48 | - $params, |
|
49 | - \OC::$server->getLogger() |
|
50 | - ); |
|
51 | - $this->params = $params; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @return \OpenStack\ObjectStore\v1\Models\Container |
|
56 | - * @throws StorageAuthException |
|
57 | - * @throws \OCP\Files\StorageNotAvailableException |
|
58 | - */ |
|
59 | - private function getContainer() { |
|
60 | - return $this->swiftFactory->getContainer(); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @return string the container name where objects are stored |
|
65 | - */ |
|
66 | - public function getStorageId() { |
|
67 | - if (isset($this->params['bucket'])) { |
|
68 | - return $this->params['bucket']; |
|
69 | - } |
|
70 | - |
|
71 | - return $this->params['container']; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @param string $urn the unified resource name used to identify the object |
|
76 | - * @param resource $stream stream with the data to write |
|
77 | - * @throws \Exception from openstack lib when something goes wrong |
|
78 | - */ |
|
79 | - public function writeObject($urn, $stream) { |
|
80 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('swiftwrite'); |
|
81 | - file_put_contents($tmpFile, $stream); |
|
82 | - $handle = fopen($tmpFile, 'rb'); |
|
83 | - |
|
84 | - $this->getContainer()->createObject([ |
|
85 | - 'name' => $urn, |
|
86 | - 'stream' => stream_for($handle) |
|
87 | - ]); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $urn the unified resource name used to identify the object |
|
92 | - * @return resource stream with the read data |
|
93 | - * @throws \Exception from openstack or GuzzleHttp libs when something goes wrong |
|
94 | - * @throws NotFoundException if file does not exist |
|
95 | - */ |
|
96 | - public function readObject($urn) { |
|
97 | - try { |
|
98 | - $publicUri = $this->getContainer()->getObject($urn)->getPublicUri(); |
|
99 | - $tokenId = $this->swiftFactory->getCachedTokenId(); |
|
100 | - |
|
101 | - $response = (new Client())->request('GET', $publicUri, |
|
102 | - [ |
|
103 | - 'stream' => true, |
|
104 | - 'headers' => [ |
|
105 | - 'X-Auth-Token' => $tokenId, |
|
106 | - 'Cache-Control' => 'no-cache' |
|
107 | - ], |
|
108 | - ] |
|
109 | - ); |
|
110 | - } catch (BadResponseException $e) { |
|
111 | - if ($e->getResponse() && $e->getResponse()->getStatusCode() === 404) { |
|
112 | - throw new NotFoundException("object $urn not found in object store"); |
|
113 | - } else { |
|
114 | - throw $e; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - return RetryWrapper::wrap($response->getBody()->detach()); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @param string $urn Unified Resource Name |
|
123 | - * @return void |
|
124 | - * @throws \Exception from openstack lib when something goes wrong |
|
125 | - */ |
|
126 | - public function deleteObject($urn) { |
|
127 | - $this->getContainer()->getObject($urn)->delete(); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @return void |
|
132 | - * @throws \Exception from openstack lib when something goes wrong |
|
133 | - */ |
|
134 | - public function deleteContainer() { |
|
135 | - $this->getContainer()->delete(); |
|
136 | - } |
|
137 | - |
|
138 | - public function objectExists($urn) { |
|
139 | - return $this->getContainer()->objectExists($urn); |
|
140 | - } |
|
37 | + /** |
|
38 | + * @var array |
|
39 | + */ |
|
40 | + private $params; |
|
41 | + |
|
42 | + /** @var SwiftFactory */ |
|
43 | + private $swiftFactory; |
|
44 | + |
|
45 | + public function __construct($params, SwiftFactory $connectionFactory = null) { |
|
46 | + $this->swiftFactory = $connectionFactory ?: new SwiftFactory( |
|
47 | + \OC::$server->getMemCacheFactory()->createDistributed('swift::'), |
|
48 | + $params, |
|
49 | + \OC::$server->getLogger() |
|
50 | + ); |
|
51 | + $this->params = $params; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @return \OpenStack\ObjectStore\v1\Models\Container |
|
56 | + * @throws StorageAuthException |
|
57 | + * @throws \OCP\Files\StorageNotAvailableException |
|
58 | + */ |
|
59 | + private function getContainer() { |
|
60 | + return $this->swiftFactory->getContainer(); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @return string the container name where objects are stored |
|
65 | + */ |
|
66 | + public function getStorageId() { |
|
67 | + if (isset($this->params['bucket'])) { |
|
68 | + return $this->params['bucket']; |
|
69 | + } |
|
70 | + |
|
71 | + return $this->params['container']; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @param string $urn the unified resource name used to identify the object |
|
76 | + * @param resource $stream stream with the data to write |
|
77 | + * @throws \Exception from openstack lib when something goes wrong |
|
78 | + */ |
|
79 | + public function writeObject($urn, $stream) { |
|
80 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile('swiftwrite'); |
|
81 | + file_put_contents($tmpFile, $stream); |
|
82 | + $handle = fopen($tmpFile, 'rb'); |
|
83 | + |
|
84 | + $this->getContainer()->createObject([ |
|
85 | + 'name' => $urn, |
|
86 | + 'stream' => stream_for($handle) |
|
87 | + ]); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $urn the unified resource name used to identify the object |
|
92 | + * @return resource stream with the read data |
|
93 | + * @throws \Exception from openstack or GuzzleHttp libs when something goes wrong |
|
94 | + * @throws NotFoundException if file does not exist |
|
95 | + */ |
|
96 | + public function readObject($urn) { |
|
97 | + try { |
|
98 | + $publicUri = $this->getContainer()->getObject($urn)->getPublicUri(); |
|
99 | + $tokenId = $this->swiftFactory->getCachedTokenId(); |
|
100 | + |
|
101 | + $response = (new Client())->request('GET', $publicUri, |
|
102 | + [ |
|
103 | + 'stream' => true, |
|
104 | + 'headers' => [ |
|
105 | + 'X-Auth-Token' => $tokenId, |
|
106 | + 'Cache-Control' => 'no-cache' |
|
107 | + ], |
|
108 | + ] |
|
109 | + ); |
|
110 | + } catch (BadResponseException $e) { |
|
111 | + if ($e->getResponse() && $e->getResponse()->getStatusCode() === 404) { |
|
112 | + throw new NotFoundException("object $urn not found in object store"); |
|
113 | + } else { |
|
114 | + throw $e; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + return RetryWrapper::wrap($response->getBody()->detach()); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @param string $urn Unified Resource Name |
|
123 | + * @return void |
|
124 | + * @throws \Exception from openstack lib when something goes wrong |
|
125 | + */ |
|
126 | + public function deleteObject($urn) { |
|
127 | + $this->getContainer()->getObject($urn)->delete(); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @return void |
|
132 | + * @throws \Exception from openstack lib when something goes wrong |
|
133 | + */ |
|
134 | + public function deleteContainer() { |
|
135 | + $this->getContainer()->delete(); |
|
136 | + } |
|
137 | + |
|
138 | + public function objectExists($urn) { |
|
139 | + return $this->getContainer()->objectExists($urn); |
|
140 | + } |
|
141 | 141 | } |