| @@ 12-372 (lines=361) @@ | ||
| 9 | * |
|
| 10 | * @author Patrik Karisch <[email protected]> |
|
| 11 | */ |
|
| 12 | class GCS implements Adapter, Adapter\MetadataSupporter, Adapter\ListKeysAware |
|
| 13 | { |
|
| 14 | protected $service; |
|
| 15 | protected $bucket; |
|
| 16 | protected $options; |
|
| 17 | protected $bucketExists; |
|
| 18 | protected $metadata = array(); |
|
| 19 | protected $detectContentType; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * @param \Google_Service_Storage $service The storage service class with authenticated |
|
| 23 | * client and full access scope |
|
| 24 | * @param string $bucket The bucket name |
|
| 25 | * @param array $options Options can be directory and acl |
|
| 26 | * @param bool $detectContentType Whether to detect the content type or not |
|
| 27 | */ |
|
| 28 | public function __construct( |
|
| 29 | \Google_Service_Storage $service, |
|
| 30 | $bucket, |
|
| 31 | array $options = array(), |
|
| 32 | $detectContentType = false |
|
| 33 | ) { |
|
| 34 | $this->service = $service; |
|
| 35 | $this->bucket = $bucket; |
|
| 36 | $this->options = array_replace( |
|
| 37 | array( |
|
| 38 | 'directory' => '', |
|
| 39 | 'acl' => 'private', |
|
| 40 | ), |
|
| 41 | $options |
|
| 42 | ); |
|
| 43 | ||
| 44 | $this->detectContentType = $detectContentType; |
|
| 45 | } |
|
| 46 | ||
| 47 | /** |
|
| 48 | * @return array The actual options |
|
| 49 | */ |
|
| 50 | public function getOptions() |
|
| 51 | { |
|
| 52 | return $this->options; |
|
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * @param array $options The new options |
|
| 57 | */ |
|
| 58 | public function setOptions($options) |
|
| 59 | { |
|
| 60 | $this->options = array_replace($this->options, $options); |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * @return string The current bucket name |
|
| 65 | */ |
|
| 66 | public function getBucket() |
|
| 67 | { |
|
| 68 | return $this->bucket; |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * Sets a new bucket name. |
|
| 73 | * |
|
| 74 | * @param string $bucket The new bucket name |
|
| 75 | */ |
|
| 76 | public function setBucket($bucket) |
|
| 77 | { |
|
| 78 | $this->bucket = $bucket; |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * {@inheritdoc} |
|
| 83 | */ |
|
| 84 | public function read($key) |
|
| 85 | { |
|
| 86 | $this->ensureBucketExists(); |
|
| 87 | $path = $this->computePath($key); |
|
| 88 | ||
| 89 | $object = $this->getObjectData($path); |
|
| 90 | if ($object === false) { |
|
| 91 | return false; |
|
| 92 | } |
|
| 93 | ||
| 94 | $request = new \Google_Http_Request($object->getMediaLink()); |
|
| 95 | $this->service->getClient()->getAuth()->sign($request); |
|
| 96 | ||
| 97 | $response = $this->service->getClient()->getIo()->executeRequest($request); |
|
| 98 | ||
| 99 | if ($response[2] == 200) { |
|
| 100 | $this->setMetadata($key, $object->getMetadata()); |
|
| 101 | ||
| 102 | return $response[0]; |
|
| 103 | } |
|
| 104 | ||
| 105 | return false; |
|
| 106 | } |
|
| 107 | ||
| 108 | /** |
|
| 109 | * {@inheritdoc} |
|
| 110 | */ |
|
| 111 | public function write($key, $content) |
|
| 112 | { |
|
| 113 | $this->ensureBucketExists(); |
|
| 114 | $path = $this->computePath($key); |
|
| 115 | ||
| 116 | $metadata = $this->getMetadata($key); |
|
| 117 | $options = array( |
|
| 118 | 'uploadType' => 'multipart', |
|
| 119 | 'data' => $content, |
|
| 120 | ); |
|
| 121 | ||
| 122 | /* |
|
| 123 | * If the ContentType was not already set in the metadata, then we autodetect |
|
| 124 | * it to prevent everything being served up as application/octet-stream. |
|
| 125 | */ |
|
| 126 | if (!isset($metadata['ContentType']) && $this->detectContentType) { |
|
| 127 | $finfo = new \finfo(FILEINFO_MIME_TYPE); |
|
| 128 | $options['mimeType'] = $finfo->buffer($content); |
|
| 129 | unset($metadata['ContentType']); |
|
| 130 | } elseif (isset($metadata['ContentType'])) { |
|
| 131 | $options['mimeType'] = $metadata['ContentType']; |
|
| 132 | unset($metadata['ContentType']); |
|
| 133 | } |
|
| 134 | ||
| 135 | $object = new \Google_Service_Storage_StorageObject(); |
|
| 136 | $object->name = $path; |
|
| 137 | ||
| 138 | if (isset($metadata['ContentDisposition'])) { |
|
| 139 | $object->setContentDisposition($metadata['ContentDisposition']); |
|
| 140 | unset($metadata['ContentDisposition']); |
|
| 141 | } |
|
| 142 | ||
| 143 | if (isset($metadata['CacheControl'])) { |
|
| 144 | $object->setCacheControl($metadata['CacheControl']); |
|
| 145 | unset($metadata['CacheControl']); |
|
| 146 | } |
|
| 147 | ||
| 148 | if (isset($metadata['ContentLanguage'])) { |
|
| 149 | $object->setContentLanguage($metadata['ContentLanguage']); |
|
| 150 | unset($metadata['ContentLanguage']); |
|
| 151 | } |
|
| 152 | ||
| 153 | if (isset($metadata['ContentEncoding'])) { |
|
| 154 | $object->setContentEncoding($metadata['ContentEncoding']); |
|
| 155 | unset($metadata['ContentEncoding']); |
|
| 156 | } |
|
| 157 | ||
| 158 | $object->setMetadata($metadata); |
|
| 159 | ||
| 160 | try { |
|
| 161 | $object = $this->service->objects->insert($this->bucket, $object, $options); |
|
| 162 | ||
| 163 | if ($this->options['acl'] == 'public') { |
|
| 164 | $acl = new \Google_Service_Storage_ObjectAccessControl(); |
|
| 165 | $acl->setEntity('allUsers'); |
|
| 166 | $acl->setRole('READER'); |
|
| 167 | ||
| 168 | $this->service->objectAccessControls->insert($this->bucket, $path, $acl); |
|
| 169 | } |
|
| 170 | ||
| 171 | return $object->getSize(); |
|
| 172 | } catch (\Google_Service_Exception $e) { |
|
| 173 | return false; |
|
| 174 | } |
|
| 175 | } |
|
| 176 | ||
| 177 | /** |
|
| 178 | * {@inheritdoc} |
|
| 179 | */ |
|
| 180 | public function exists($key) |
|
| 181 | { |
|
| 182 | $this->ensureBucketExists(); |
|
| 183 | $path = $this->computePath($key); |
|
| 184 | ||
| 185 | try { |
|
| 186 | $this->service->objects->get($this->bucket, $path); |
|
| 187 | } catch (\Google_Service_Exception $e) { |
|
| 188 | return false; |
|
| 189 | } |
|
| 190 | ||
| 191 | return true; |
|
| 192 | } |
|
| 193 | ||
| 194 | /** |
|
| 195 | * {@inheritdoc} |
|
| 196 | */ |
|
| 197 | public function keys() |
|
| 198 | { |
|
| 199 | return $this->listKeys(); |
|
| 200 | } |
|
| 201 | ||
| 202 | /** |
|
| 203 | * {@inheritdoc} |
|
| 204 | */ |
|
| 205 | public function mtime($key) |
|
| 206 | { |
|
| 207 | $this->ensureBucketExists(); |
|
| 208 | $path = $this->computePath($key); |
|
| 209 | ||
| 210 | $object = $this->getObjectData($path); |
|
| 211 | ||
| 212 | return $object ? strtotime($object->getUpdated()) : false; |
|
| 213 | } |
|
| 214 | ||
| 215 | /** |
|
| 216 | * {@inheritdoc} |
|
| 217 | */ |
|
| 218 | public function delete($key) |
|
| 219 | { |
|
| 220 | $this->ensureBucketExists(); |
|
| 221 | $path = $this->computePath($key); |
|
| 222 | ||
| 223 | try { |
|
| 224 | $this->service->objects->delete($this->bucket, $path); |
|
| 225 | } catch (\Google_Service_Exception $e) { |
|
| 226 | return false; |
|
| 227 | } |
|
| 228 | ||
| 229 | return true; |
|
| 230 | } |
|
| 231 | ||
| 232 | /** |
|
| 233 | * {@inheritdoc} |
|
| 234 | */ |
|
| 235 | public function rename($sourceKey, $targetKey) |
|
| 236 | { |
|
| 237 | $this->ensureBucketExists(); |
|
| 238 | $sourcePath = $this->computePath($sourceKey); |
|
| 239 | $targetPath = $this->computePath($targetKey); |
|
| 240 | ||
| 241 | $object = $this->getObjectData($sourcePath); |
|
| 242 | if ($object === false) { |
|
| 243 | return false; |
|
| 244 | } |
|
| 245 | ||
| 246 | try { |
|
| 247 | $this->service->objects->copy($this->bucket, $sourcePath, $this->bucket, $targetPath, $object); |
|
| 248 | $this->delete($sourcePath); |
|
| 249 | } catch (\Google_Service_Exception $e) { |
|
| 250 | return false; |
|
| 251 | } |
|
| 252 | ||
| 253 | return true; |
|
| 254 | } |
|
| 255 | ||
| 256 | /** |
|
| 257 | * {@inheritdoc} |
|
| 258 | */ |
|
| 259 | public function isDirectory($key) |
|
| 260 | { |
|
| 261 | if ($this->exists($key.'/')) { |
|
| 262 | return true; |
|
| 263 | } |
|
| 264 | ||
| 265 | return false; |
|
| 266 | } |
|
| 267 | ||
| 268 | /** |
|
| 269 | * {@inheritdoc} |
|
| 270 | */ |
|
| 271 | public function listKeys($prefix = '') |
|
| 272 | { |
|
| 273 | $this->ensureBucketExists(); |
|
| 274 | ||
| 275 | $options = array(); |
|
| 276 | if ((string) $prefix != '') { |
|
| 277 | $options['prefix'] = $this->computePath($prefix); |
|
| 278 | } elseif (!empty($this->options['directory'])) { |
|
| 279 | $options['prefix'] = $this->options['directory']; |
|
| 280 | } |
|
| 281 | ||
| 282 | $list = $this->service->objects->listObjects($this->bucket, $options); |
|
| 283 | $keys = array(); |
|
| 284 | ||
| 285 | // FIXME: Temporary workaround for google/google-api-php-client#375 |
|
| 286 | $reflectionClass = new \ReflectionClass('Google_Service_Storage_Objects'); |
|
| 287 | $reflectionProperty = $reflectionClass->getProperty('collection_key'); |
|
| 288 | $reflectionProperty->setAccessible(true); |
|
| 289 | $reflectionProperty->setValue($list, 'items'); |
|
| 290 | ||
| 291 | /** @var \Google_Service_Storage_StorageObject $object */ |
|
| 292 | foreach ($list as $object) { |
|
| 293 | $keys[] = $object->name; |
|
| 294 | } |
|
| 295 | ||
| 296 | sort($keys); |
|
| 297 | ||
| 298 | return $keys; |
|
| 299 | } |
|
| 300 | ||
| 301 | /** |
|
| 302 | * {@inheritdoc} |
|
| 303 | */ |
|
| 304 | public function setMetadata($key, $content) |
|
| 305 | { |
|
| 306 | $path = $this->computePath($key); |
|
| 307 | ||
| 308 | $this->metadata[$path] = $content; |
|
| 309 | } |
|
| 310 | ||
| 311 | /** |
|
| 312 | * {@inheritdoc} |
|
| 313 | */ |
|
| 314 | public function getMetadata($key) |
|
| 315 | { |
|
| 316 | $path = $this->computePath($key); |
|
| 317 | ||
| 318 | return isset($this->metadata[$path]) ? $this->metadata[$path] : array(); |
|
| 319 | } |
|
| 320 | ||
| 321 | /** |
|
| 322 | * Ensures the specified bucket exists. |
|
| 323 | * |
|
| 324 | * @throws \RuntimeException if the bucket does not exists |
|
| 325 | */ |
|
| 326 | protected function ensureBucketExists() |
|
| 327 | { |
|
| 328 | if ($this->bucketExists) { |
|
| 329 | return; |
|
| 330 | } |
|
| 331 | ||
| 332 | try { |
|
| 333 | $this->service->buckets->get($this->bucket); |
|
| 334 | $this->bucketExists = true; |
|
| 335 | ||
| 336 | return; |
|
| 337 | } catch (\Google_Service_Exception $e) { |
|
| 338 | $this->bucketExists = false; |
|
| 339 | ||
| 340 | throw new \RuntimeException( |
|
| 341 | sprintf( |
|
| 342 | 'The configured bucket "%s" does not exist.', |
|
| 343 | $this->bucket |
|
| 344 | ) |
|
| 345 | ); |
|
| 346 | } |
|
| 347 | } |
|
| 348 | ||
| 349 | protected function computePath($key) |
|
| 350 | { |
|
| 351 | if (empty($this->options['directory'])) { |
|
| 352 | return $key; |
|
| 353 | } |
|
| 354 | ||
| 355 | return sprintf('%s/%s', $this->options['directory'], $key); |
|
| 356 | } |
|
| 357 | ||
| 358 | /** |
|
| 359 | * @param string $path |
|
| 360 | * @param array $options |
|
| 361 | * |
|
| 362 | * @return bool|\Google_Service_Storage_StorageObject |
|
| 363 | */ |
|
| 364 | private function getObjectData($path, $options = array()) |
|
| 365 | { |
|
| 366 | try { |
|
| 367 | return $this->service->objects->get($this->bucket, $path, $options); |
|
| 368 | } catch (\Google_Service_Exception $e) { |
|
| 369 | return false; |
|
| 370 | } |
|
| 371 | } |
|
| 372 | } |
|
| 373 | ||
| @@ 15-377 (lines=363) @@ | ||
| 12 | * @author Patrik Karisch <[email protected]> |
|
| 13 | * @deprecated 0.4 Deprecated since version 0.5 Use \Gaufrette\Adapter\GCS\GCS instead. |
|
| 14 | */ |
|
| 15 | class GoogleCloudStorage implements Adapter, |
|
| 16 | MetadataSupporter, |
|
| 17 | ListKeysAware |
|
| 18 | { |
|
| 19 | protected $service; |
|
| 20 | protected $bucket; |
|
| 21 | protected $options; |
|
| 22 | protected $bucketExists; |
|
| 23 | protected $metadata = array(); |
|
| 24 | protected $detectContentType; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @param \Google_Service_Storage $service The storage service class with authenticated |
|
| 28 | * client and full access scope |
|
| 29 | * @param string $bucket The bucket name |
|
| 30 | * @param array $options Options can be directory and acl |
|
| 31 | * @param bool $detectContentType Whether to detect the content type or not |
|
| 32 | */ |
|
| 33 | public function __construct( |
|
| 34 | \Google_Service_Storage $service, |
|
| 35 | $bucket, |
|
| 36 | array $options = array(), |
|
| 37 | $detectContentType = false |
|
| 38 | ) { |
|
| 39 | $this->service = $service; |
|
| 40 | $this->bucket = $bucket; |
|
| 41 | $this->options = array_replace( |
|
| 42 | array( |
|
| 43 | 'directory' => '', |
|
| 44 | 'acl' => 'private', |
|
| 45 | ), |
|
| 46 | $options |
|
| 47 | ); |
|
| 48 | ||
| 49 | $this->detectContentType = $detectContentType; |
|
| 50 | } |
|
| 51 | ||
| 52 | /** |
|
| 53 | * @return array The actual options |
|
| 54 | */ |
|
| 55 | public function getOptions() |
|
| 56 | { |
|
| 57 | return $this->options; |
|
| 58 | } |
|
| 59 | ||
| 60 | /** |
|
| 61 | * @param array $options The new options |
|
| 62 | */ |
|
| 63 | public function setOptions($options) |
|
| 64 | { |
|
| 65 | $this->options = array_replace($this->options, $options); |
|
| 66 | } |
|
| 67 | ||
| 68 | /** |
|
| 69 | * @return string The current bucket name |
|
| 70 | */ |
|
| 71 | public function getBucket() |
|
| 72 | { |
|
| 73 | return $this->bucket; |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Sets a new bucket name. |
|
| 78 | * |
|
| 79 | * @param string $bucket The new bucket name |
|
| 80 | */ |
|
| 81 | public function setBucket($bucket) |
|
| 82 | { |
|
| 83 | $this->bucket = $bucket; |
|
| 84 | } |
|
| 85 | ||
| 86 | /** |
|
| 87 | * {@inheritdoc} |
|
| 88 | */ |
|
| 89 | public function read($key) |
|
| 90 | { |
|
| 91 | $this->ensureBucketExists(); |
|
| 92 | $path = $this->computePath($key); |
|
| 93 | ||
| 94 | $object = $this->getObjectData($path); |
|
| 95 | if ($object === false) { |
|
| 96 | return false; |
|
| 97 | } |
|
| 98 | ||
| 99 | $request = new \Google_Http_Request($object->getMediaLink()); |
|
| 100 | $this->service->getClient()->getAuth()->sign($request); |
|
| 101 | ||
| 102 | $response = $this->service->getClient()->getIo()->executeRequest($request); |
|
| 103 | ||
| 104 | if ($response[2] == 200) { |
|
| 105 | $this->setMetadata($key, $object->getMetadata()); |
|
| 106 | ||
| 107 | return $response[0]; |
|
| 108 | } |
|
| 109 | ||
| 110 | return false; |
|
| 111 | } |
|
| 112 | ||
| 113 | /** |
|
| 114 | * {@inheritdoc} |
|
| 115 | */ |
|
| 116 | public function write($key, $content) |
|
| 117 | { |
|
| 118 | $this->ensureBucketExists(); |
|
| 119 | $path = $this->computePath($key); |
|
| 120 | ||
| 121 | $metadata = $this->getMetadata($key); |
|
| 122 | $options = array( |
|
| 123 | 'uploadType' => 'multipart', |
|
| 124 | 'data' => $content, |
|
| 125 | ); |
|
| 126 | ||
| 127 | /* |
|
| 128 | * If the ContentType was not already set in the metadata, then we autodetect |
|
| 129 | * it to prevent everything being served up as application/octet-stream. |
|
| 130 | */ |
|
| 131 | if (!isset($metadata['ContentType']) && $this->detectContentType) { |
|
| 132 | $finfo = new \finfo(FILEINFO_MIME_TYPE); |
|
| 133 | $options['mimeType'] = $finfo->buffer($content); |
|
| 134 | unset($metadata['ContentType']); |
|
| 135 | } elseif (isset($metadata['ContentType'])) { |
|
| 136 | $options['mimeType'] = $metadata['ContentType']; |
|
| 137 | unset($metadata['ContentType']); |
|
| 138 | } |
|
| 139 | ||
| 140 | $object = new \Google_Service_Storage_StorageObject(); |
|
| 141 | $object->name = $path; |
|
| 142 | ||
| 143 | if (isset($metadata['ContentDisposition'])) { |
|
| 144 | $object->setContentDisposition($metadata['ContentDisposition']); |
|
| 145 | unset($metadata['ContentDisposition']); |
|
| 146 | } |
|
| 147 | ||
| 148 | if (isset($metadata['CacheControl'])) { |
|
| 149 | $object->setCacheControl($metadata['CacheControl']); |
|
| 150 | unset($metadata['CacheControl']); |
|
| 151 | } |
|
| 152 | ||
| 153 | if (isset($metadata['ContentLanguage'])) { |
|
| 154 | $object->setContentLanguage($metadata['ContentLanguage']); |
|
| 155 | unset($metadata['ContentLanguage']); |
|
| 156 | } |
|
| 157 | ||
| 158 | if (isset($metadata['ContentEncoding'])) { |
|
| 159 | $object->setContentEncoding($metadata['ContentEncoding']); |
|
| 160 | unset($metadata['ContentEncoding']); |
|
| 161 | } |
|
| 162 | ||
| 163 | $object->setMetadata($metadata); |
|
| 164 | ||
| 165 | try { |
|
| 166 | $object = $this->service->objects->insert($this->bucket, $object, $options); |
|
| 167 | ||
| 168 | if ($this->options['acl'] == 'public') { |
|
| 169 | $acl = new \Google_Service_Storage_ObjectAccessControl(); |
|
| 170 | $acl->setEntity('allUsers'); |
|
| 171 | $acl->setRole('READER'); |
|
| 172 | ||
| 173 | $this->service->objectAccessControls->insert($this->bucket, $path, $acl); |
|
| 174 | } |
|
| 175 | ||
| 176 | return $object->getSize(); |
|
| 177 | } catch (\Google_Service_Exception $e) { |
|
| 178 | return false; |
|
| 179 | } |
|
| 180 | } |
|
| 181 | ||
| 182 | /** |
|
| 183 | * {@inheritdoc} |
|
| 184 | */ |
|
| 185 | public function exists($key) |
|
| 186 | { |
|
| 187 | $this->ensureBucketExists(); |
|
| 188 | $path = $this->computePath($key); |
|
| 189 | ||
| 190 | try { |
|
| 191 | $this->service->objects->get($this->bucket, $path); |
|
| 192 | } catch (\Google_Service_Exception $e) { |
|
| 193 | return false; |
|
| 194 | } |
|
| 195 | ||
| 196 | return true; |
|
| 197 | } |
|
| 198 | ||
| 199 | /** |
|
| 200 | * {@inheritdoc} |
|
| 201 | */ |
|
| 202 | public function keys() |
|
| 203 | { |
|
| 204 | return $this->listKeys(); |
|
| 205 | } |
|
| 206 | ||
| 207 | /** |
|
| 208 | * {@inheritdoc} |
|
| 209 | */ |
|
| 210 | public function mtime($key) |
|
| 211 | { |
|
| 212 | $this->ensureBucketExists(); |
|
| 213 | $path = $this->computePath($key); |
|
| 214 | ||
| 215 | $object = $this->getObjectData($path); |
|
| 216 | ||
| 217 | return $object ? strtotime($object->getUpdated()) : false; |
|
| 218 | } |
|
| 219 | ||
| 220 | /** |
|
| 221 | * {@inheritdoc} |
|
| 222 | */ |
|
| 223 | public function delete($key) |
|
| 224 | { |
|
| 225 | $this->ensureBucketExists(); |
|
| 226 | $path = $this->computePath($key); |
|
| 227 | ||
| 228 | try { |
|
| 229 | $this->service->objects->delete($this->bucket, $path); |
|
| 230 | } catch (\Google_Service_Exception $e) { |
|
| 231 | return false; |
|
| 232 | } |
|
| 233 | ||
| 234 | return true; |
|
| 235 | } |
|
| 236 | ||
| 237 | /** |
|
| 238 | * {@inheritdoc} |
|
| 239 | */ |
|
| 240 | public function rename($sourceKey, $targetKey) |
|
| 241 | { |
|
| 242 | $this->ensureBucketExists(); |
|
| 243 | $sourcePath = $this->computePath($sourceKey); |
|
| 244 | $targetPath = $this->computePath($targetKey); |
|
| 245 | ||
| 246 | $object = $this->getObjectData($sourcePath); |
|
| 247 | if ($object === false) { |
|
| 248 | return false; |
|
| 249 | } |
|
| 250 | ||
| 251 | try { |
|
| 252 | $this->service->objects->copy($this->bucket, $sourcePath, $this->bucket, $targetPath, $object); |
|
| 253 | $this->delete($sourcePath); |
|
| 254 | } catch (\Google_Service_Exception $e) { |
|
| 255 | return false; |
|
| 256 | } |
|
| 257 | ||
| 258 | return true; |
|
| 259 | } |
|
| 260 | ||
| 261 | /** |
|
| 262 | * {@inheritdoc} |
|
| 263 | */ |
|
| 264 | public function isDirectory($key) |
|
| 265 | { |
|
| 266 | if ($this->exists($key.'/')) { |
|
| 267 | return true; |
|
| 268 | } |
|
| 269 | ||
| 270 | return false; |
|
| 271 | } |
|
| 272 | ||
| 273 | /** |
|
| 274 | * {@inheritdoc} |
|
| 275 | */ |
|
| 276 | public function listKeys($prefix = '') |
|
| 277 | { |
|
| 278 | $this->ensureBucketExists(); |
|
| 279 | ||
| 280 | $options = array(); |
|
| 281 | if ((string) $prefix != '') { |
|
| 282 | $options['prefix'] = $this->computePath($prefix); |
|
| 283 | } elseif (!empty($this->options['directory'])) { |
|
| 284 | $options['prefix'] = $this->options['directory']; |
|
| 285 | } |
|
| 286 | ||
| 287 | $list = $this->service->objects->listObjects($this->bucket, $options); |
|
| 288 | $keys = array(); |
|
| 289 | ||
| 290 | // FIXME: Temporary workaround for google/google-api-php-client#375 |
|
| 291 | $reflectionClass = new \ReflectionClass('Google_Service_Storage_Objects'); |
|
| 292 | $reflectionProperty = $reflectionClass->getProperty('collection_key'); |
|
| 293 | $reflectionProperty->setAccessible(true); |
|
| 294 | $reflectionProperty->setValue($list, 'items'); |
|
| 295 | ||
| 296 | /** @var \Google_Service_Storage_StorageObject $object */ |
|
| 297 | foreach ($list as $object) { |
|
| 298 | $keys[] = $object->name; |
|
| 299 | } |
|
| 300 | ||
| 301 | sort($keys); |
|
| 302 | ||
| 303 | return $keys; |
|
| 304 | } |
|
| 305 | ||
| 306 | /** |
|
| 307 | * {@inheritdoc} |
|
| 308 | */ |
|
| 309 | public function setMetadata($key, $content) |
|
| 310 | { |
|
| 311 | $path = $this->computePath($key); |
|
| 312 | ||
| 313 | $this->metadata[$path] = $content; |
|
| 314 | } |
|
| 315 | ||
| 316 | /** |
|
| 317 | * {@inheritdoc} |
|
| 318 | */ |
|
| 319 | public function getMetadata($key) |
|
| 320 | { |
|
| 321 | $path = $this->computePath($key); |
|
| 322 | ||
| 323 | return isset($this->metadata[$path]) ? $this->metadata[$path] : array(); |
|
| 324 | } |
|
| 325 | ||
| 326 | /** |
|
| 327 | * Ensures the specified bucket exists. |
|
| 328 | * |
|
| 329 | * @throws \RuntimeException if the bucket does not exists |
|
| 330 | */ |
|
| 331 | protected function ensureBucketExists() |
|
| 332 | { |
|
| 333 | if ($this->bucketExists) { |
|
| 334 | return; |
|
| 335 | } |
|
| 336 | ||
| 337 | try { |
|
| 338 | $this->service->buckets->get($this->bucket); |
|
| 339 | $this->bucketExists = true; |
|
| 340 | ||
| 341 | return; |
|
| 342 | } catch (\Google_Service_Exception $e) { |
|
| 343 | $this->bucketExists = false; |
|
| 344 | ||
| 345 | throw new \RuntimeException( |
|
| 346 | sprintf( |
|
| 347 | 'The configured bucket "%s" does not exist.', |
|
| 348 | $this->bucket |
|
| 349 | ) |
|
| 350 | ); |
|
| 351 | } |
|
| 352 | } |
|
| 353 | ||
| 354 | protected function computePath($key) |
|
| 355 | { |
|
| 356 | if (empty($this->options['directory'])) { |
|
| 357 | return $key; |
|
| 358 | } |
|
| 359 | ||
| 360 | return sprintf('%s/%s', $this->options['directory'], $key); |
|
| 361 | } |
|
| 362 | ||
| 363 | /** |
|
| 364 | * @param string $path |
|
| 365 | * @param array $options |
|
| 366 | * |
|
| 367 | * @return bool|\Google_Service_Storage_StorageObject |
|
| 368 | */ |
|
| 369 | private function getObjectData($path, $options = array()) |
|
| 370 | { |
|
| 371 | try { |
|
| 372 | return $this->service->objects->get($this->bucket, $path, $options); |
|
| 373 | } catch (\Google_Service_Exception $e) { |
|
| 374 | return false; |
|
| 375 | } |
|
| 376 | } |
|
| 377 | } |
|
| 378 | ||