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