|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sonata project. |
| 5 | + * |
| 6 | + * (c) Thomas Rabaix <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Sonata\MediaBundle\Metadata; |
| 13 | + |
| 14 | +use Sonata\MediaBundle\Metadata\MetadataBuilderInterface; |
| 15 | +use Sonata\MediaBundle\Model\MediaInterface; |
| 16 | +use \AmazonS3 as AmazonS3; |
| 17 | +use \CFMimeTypes; |
| 18 | + |
| 19 | +class AmazonMetadataBuilder implements MetadataBuilderInterface |
| 20 | +{ |
| 21 | + |
| 22 | + protected $settings; |
| 23 | + |
| 24 | + protected $acl = array( |
| 25 | + 'private' => AmazonS3::ACL_PRIVATE, |
| 26 | + 'public' => AmazonS3::ACL_PUBLIC, |
| 27 | + 'open' => AmazonS3::ACL_OPEN, |
| 28 | + 'auth_read' => AmazonS3::ACL_AUTH_READ, |
| 29 | + 'owner_read' => AmazonS3::ACL_OWNER_READ, |
| 30 | + 'owner_full_control' => AmazonS3::ACL_OWNER_FULL_CONTROL, |
| 31 | + ); |
| 32 | + |
| 33 | + /** |
| 34 | + * @param array $settings |
| 35 | + */ |
| 36 | + public function __construct(array $settings) |
| 37 | + { |
| 38 | + $this->settings = $settings; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get data passed from the config |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + protected function getDefaultMetadata() |
| 47 | + { |
| 48 | + //merge acl |
| 49 | + $output = array(); |
| 50 | + if (isset($this->settings['acl']) && !empty($this->settings['acl'])) { |
| 51 | + $output['acl'] = $this->acl[$this->settings['acl']]; |
| 52 | + } |
| 53 | + |
| 54 | + //merge storage |
| 55 | + if (isset($this->settings['storage'])) { |
| 56 | + if ($this->settings['storage'] == 'standard') { |
| 57 | + $output['storage'] = AmazonS3::STORAGE_STANDARD; |
| 58 | + } elseif ($this->settings['storage'] == 'reduced') { |
| 59 | + $output['storage'] = AmazonS3::STORAGE_REDUCED; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + //merge meta |
| 64 | + if (isset($this->settings['meta']) && !empty($this->settings['meta'])) { |
| 65 | + $output['meta'] = $this->settings['meta']; |
| 66 | + } |
| 67 | + |
| 68 | + //merge cache control header |
| 69 | + if (isset($this->settings['cache_control']) && !empty($this->settings['cache_control'])) { |
| 70 | + $output['headers']['Cache-Control'] = $this->settings['cache_control']; |
| 71 | + } |
| 72 | + |
| 73 | + //merge encryption |
| 74 | + if (isset($this->settings['encryption']) && !empty($this->settings['encryption'])) { |
| 75 | + if ($this->settings['encryption'] == 'aes256') { |
| 76 | + $output['encryption'] = 'AES256'; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $output; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets the correct content-type |
| 85 | + * |
| 86 | + * @param string $filename |
| 87 | + * |
| 88 | + * @return array |
| 89 | + */ |
| 90 | + protected function getContentType($filename) |
| 91 | + { |
| 92 | + $extension = pathinfo($filename, PATHINFO_EXTENSION); |
| 93 | + $contentType = CFMimeTypes::get_mimetype($extension); |
| 94 | + |
| 95 | + return array('contentType'=> $contentType); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * {@inheritdoc} |
| 100 | + */ |
| 101 | + public function get(MediaInterface $media, $filename) |
| 102 | + { |
| 103 | + return array_replace_recursive( |
| 104 | + $this->getDefaultMetadata(), |
| 105 | + $this->getContentType($filename) |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments