limit-zero /
assets
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the limit0/assets package. |
||
| 5 | * |
||
| 6 | * (c) Limit Zero, LLC <[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 Limit0\Assets\StorageEngine; |
||
| 13 | |||
| 14 | use Aws\S3\S3Client; |
||
| 15 | use Aws\S3\Exception\S3Exception; |
||
| 16 | use Limit0\Assets\Asset; |
||
| 17 | use Limit0\Assets\AssetFactory; |
||
| 18 | use Limit0\Assets\Exception\StorageException; |
||
| 19 | use Limit0\Assets\StorageEngineInterface; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * This class supports storing, retrieve, and deleting files to Amazon S3 storage. |
||
| 23 | * At the moment, no additional storage parameters can be set -- it's best practice |
||
| 24 | * to make these the defaults for your bucket to prevent objects being out of sync |
||
| 25 | * with your AWS policies. |
||
| 26 | * |
||
| 27 | * Per AWS best practices, this passes through the authentication of requests to |
||
| 28 | * the AWS SDK itself, for more info: |
||
| 29 | * @see http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html#environment-credentials |
||
| 30 | * |
||
| 31 | * @see setBucket The bucket must be set when configuring this storage engine. |
||
| 32 | * @see setAcl The default ACL is `private`. |
||
| 33 | * |
||
| 34 | * @author Josh Worden <[email protected]> |
||
| 35 | */ |
||
| 36 | class AmazonS3StorageEngine implements StorageEngineInterface |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $bucket; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $acl = 'private'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var S3Client |
||
| 50 | */ |
||
| 51 | private $client; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $region = 'us-east-1'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function getAcl() |
||
| 62 | { |
||
| 63 | return $this->acl; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function getBucket() |
||
| 70 | { |
||
| 71 | if (null === $this->bucket) { |
||
| 72 | throw StorageException::invalidConfiguration('bucket'); |
||
| 73 | } |
||
| 74 | return $this->bucket; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Instantiates and returns an S3Client instance |
||
| 79 | * @return S3Client |
||
| 80 | */ |
||
| 81 | public function getClient() |
||
| 82 | { |
||
| 83 | if (null === $this->client) { |
||
| 84 | $this->client = S3Client::factory([ |
||
|
0 ignored issues
–
show
|
|||
| 85 | 'version' => 'latest', |
||
| 86 | 'region' => $this->getRegion() |
||
| 87 | ]); |
||
| 88 | } |
||
| 89 | return $this->client; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function getRegion() |
||
| 96 | { |
||
| 97 | return $this->region; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | public function remove($identifier) |
||
| 104 | { |
||
| 105 | try { |
||
| 106 | $this->getClient()->deleteObject([ |
||
| 107 | 'Bucket' => $this->getBucket(), |
||
| 108 | 'Key' => $identifier |
||
| 109 | ]); |
||
| 110 | } catch (S3Exception $e) { |
||
| 111 | throw new StorageException($e->getMessage()); |
||
| 112 | } |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | public function retrieve($identifier) |
||
| 121 | { |
||
| 122 | $path = explode('/', $identifier); |
||
| 123 | $fileName = array_pop($path); |
||
| 124 | $localPath = sprintf('%s/%s', sys_get_temp_dir(), $fileName); |
||
| 125 | |||
| 126 | try { |
||
| 127 | $this->getClient()->getObject([ |
||
| 128 | 'Bucket' => $this->getBucket(), |
||
| 129 | 'Key' => $identifier, |
||
| 130 | 'SaveAs' => $localPath |
||
| 131 | ]); |
||
| 132 | |||
| 133 | $asset = AssetFactory::createFromPath($localPath); |
||
| 134 | $asset->setFilename($fileName)->setFilepath(implode('/', $path)); |
||
| 135 | return $asset; |
||
| 136 | |||
| 137 | } catch (S3Exception $e) { |
||
| 138 | throw new StorageException($e->getMessage()); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Sets the ACL the asset should be stored with. |
||
| 144 | * |
||
| 145 | * @param string |
||
| 146 | */ |
||
| 147 | public function setAcl($acl) |
||
| 148 | { |
||
| 149 | $this->acl = $acl; |
||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Sets the bucket the assets should be stored to. |
||
| 155 | * |
||
| 156 | * @param string |
||
| 157 | */ |
||
| 158 | public function setBucket($bucket) |
||
| 159 | { |
||
| 160 | $this->bucket = $bucket; |
||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Override the S3 region to connect to |
||
| 166 | * @param string |
||
| 167 | */ |
||
| 168 | public function setRegion($region) |
||
| 169 | { |
||
| 170 | $this->region = $region; |
||
| 171 | return $this; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function store(Asset $asset, $path = null, $filename = null) |
||
| 178 | { |
||
| 179 | $filename = (null === $filename) ? $asset->getBasename() : $filename; |
||
| 180 | $key = sprintf('%s/%s', $path, $filename); |
||
| 181 | |||
| 182 | try { |
||
| 183 | $this->getClient()->putObject([ |
||
| 184 | 'ACL' => $this->getAcl(), |
||
| 185 | 'Bucket' => $this->getBucket(), |
||
| 186 | 'Key' => $key, |
||
| 187 | 'ContentType' => $asset->getMimeType(), |
||
| 188 | 'Body' => fopen($asset->getPathname(), 'r') |
||
| 189 | ]); |
||
| 190 | |||
| 191 | } catch (S3Exception $e) { |
||
| 192 | throw new StorageException($e->getMessage()); |
||
| 193 | } |
||
| 194 | |||
| 195 | return true; |
||
| 196 | } |
||
| 197 | } |
||
| 198 |
This method has been deprecated.