1 | <?php |
||
2 | /** |
||
3 | * This file is part of the Simple S3 package. |
||
4 | * |
||
5 | * (c) Mauro Cassani<https://github.com/mauretto78> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | * |
||
10 | */ |
||
11 | |||
12 | namespace Matecat\SimpleS3\Commands\Handlers; |
||
13 | |||
14 | use Aws\ResultInterface; |
||
15 | use Matecat\SimpleS3\Commands\CommandHandler; |
||
16 | use Matecat\SimpleS3\Components\Validators\S3ObjectSafeNameValidator; |
||
17 | use Matecat\SimpleS3\Components\Validators\S3StorageClassNameValidator; |
||
18 | use Matecat\SimpleS3\Exceptions\InvalidS3NameException; |
||
19 | use Matecat\SimpleS3\Helpers\File; |
||
20 | use Matecat\SimpleS3\Helpers\FilenameValidator; |
||
0 ignored issues
–
show
|
|||
21 | |||
22 | class UploadItemFromBody extends CommandHandler |
||
23 | { |
||
24 | /** |
||
25 | * Upload a content to S3. |
||
26 | * For a complete reference of put object see: |
||
27 | * https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html?highlight=put |
||
28 | * |
||
29 | * @param array $params |
||
30 | * |
||
31 | * @return bool |
||
32 | * @throws \Exception |
||
33 | */ |
||
34 | public function handle($params = []) |
||
35 | { |
||
36 | $bucketName = $params['bucket']; |
||
37 | $keyName = $params['key']; |
||
38 | $body = $params['body']; |
||
39 | |||
40 | if (isset($params['bucket_check']) and true === $params['bucket_check']) { |
||
41 | $this->client->createBucketIfItDoesNotExist(['bucket' => $bucketName]); |
||
42 | } |
||
43 | |||
44 | if (false === S3ObjectSafeNameValidator::isValid($keyName)) { |
||
45 | throw new InvalidS3NameException(sprintf('%s is not a valid S3 object name. ['.implode(', ', S3ObjectSafeNameValidator::validate($keyName)).']', $keyName)); |
||
46 | } |
||
47 | |||
48 | if ((isset($params['storage']) and false === S3StorageClassNameValidator::isValid($params['storage']))) { |
||
49 | throw new \InvalidArgumentException(S3StorageClassNameValidator::validate($params['storage'])[0]); |
||
50 | } |
||
51 | |||
52 | if ($this->client->hasEncoder()) { |
||
53 | $keyName = $this->client->getEncoder()->encode($keyName); |
||
54 | } |
||
55 | |||
56 | return $this->upload($bucketName, $keyName, $body, (isset($params['storage'])) ? $params['storage'] : null, (isset($params['meta'])) ? $params['meta'] : null); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param array $params |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | public function validateParams($params = []) |
||
65 | { |
||
66 | return ( |
||
67 | isset($params['bucket']) and |
||
68 | isset($params['key']) and |
||
69 | isset($params['body']) |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param string $bucketName |
||
75 | * @param string $keyName |
||
76 | * @param string $body |
||
77 | * @param null $storage |
||
0 ignored issues
–
show
|
|||
78 | * @param null $meta |
||
0 ignored issues
–
show
|
|||
79 | * |
||
80 | * @return bool |
||
81 | * @throws \Exception |
||
82 | */ |
||
83 | private function upload($bucketName, $keyName, $body, $storage = null, $meta = null) |
||
84 | { |
||
85 | try { |
||
86 | $config = [ |
||
87 | 'Bucket' => $bucketName, |
||
88 | 'Key' => $keyName, |
||
89 | 'Body' => $body, |
||
90 | 'MetadataDirective' => 'REPLACE', |
||
91 | ]; |
||
92 | |||
93 | if (null != $storage) { |
||
0 ignored issues
–
show
|
|||
94 | $config['StorageClass'] = $storage; |
||
95 | } |
||
96 | |||
97 | if (null != $meta) { |
||
0 ignored issues
–
show
|
|||
98 | $config['Metadata'] = $meta; |
||
99 | } |
||
100 | |||
101 | $config['Metadata']['original_name'] = File::getBaseName($keyName); |
||
102 | |||
103 | $result = $this->client->getConn()->putObject($config); |
||
104 | |||
105 | if (($result instanceof ResultInterface) and $result['@metadata']['statusCode'] === 200) { |
||
106 | if (null !== $this->commandHandlerLogger) { |
||
107 | $this->commandHandlerLogger->log($this, sprintf('File \'%s\' was successfully uploaded in \'%s\' bucket', $keyName, $bucketName)); |
||
108 | } |
||
109 | |||
110 | if (null == $storage and $this->client->hasCache()) { |
||
111 | $version = null; |
||
112 | if (isset($result['@metadata']['headers']['x-amz-version-id'])) { |
||
113 | $version = $result['@metadata']['headers']['x-amz-version-id']; |
||
114 | } |
||
115 | |||
116 | $this->client->getCache()->set($bucketName, $keyName, '', $version); |
||
117 | } |
||
118 | |||
119 | return true; |
||
120 | } |
||
121 | |||
122 | if (null !== $this->commandHandlerLogger) { |
||
123 | $this->commandHandlerLogger->log($this, sprintf('Something went wrong during upload of file \'%s\' in \'%s\' bucket', $keyName, $bucketName), 'warning'); |
||
124 | } |
||
125 | |||
126 | return false; |
||
127 | } catch (\InvalidArgumentException $e) { |
||
128 | if (null !== $this->commandHandlerLogger) { |
||
129 | $this->commandHandlerLogger->logExceptionAndReturnFalse($e); |
||
130 | } |
||
131 | |||
132 | throw $e; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths