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