1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\MFUploader\components; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
use yii\base\InvalidConfigException; |
8
|
|
|
use Aws\S3\{S3MultiRegionClient, S3ClientInterface}; |
9
|
|
|
use Itstructure\MFUploader\models\Mediafile; |
10
|
|
|
use Itstructure\MFUploader\models\upload\S3Upload; |
11
|
|
|
use Itstructure\MFUploader\interfaces\{UploadModelInterface, UploadComponentInterface}; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class S3UploadComponent |
15
|
|
|
* Component class to upload files in Amazon S3 bucket. |
16
|
|
|
* |
17
|
|
|
* @property array $uploadDirs Directory for uploaded files. |
18
|
|
|
* @property array|callable $credentials AWS access key ID and secret access key. |
19
|
|
|
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html |
20
|
|
|
* @property string $region Region to connect to. |
21
|
|
|
* @property string $clientVersion S3 client version. |
22
|
|
|
* @property string $s3DefaultBucket Amazon web services S3 default bucket. |
23
|
|
|
* @property array $s3Buckets Buckets for upload depending on the owner. |
24
|
|
|
* @property S3MultiRegionClient|S3ClientInterface $s3Client Amazon web services SDK S3 client. |
25
|
|
|
* |
26
|
|
|
* @package Itstructure\MFUploader\components |
27
|
|
|
* |
28
|
|
|
* @author Andrey Girnik <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class S3UploadComponent extends BaseUploadComponent implements UploadComponentInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Directory for uploaded files. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $uploadDirs = [ |
38
|
|
|
UploadModelInterface::FILE_TYPE_IMAGE => 'images', |
39
|
|
|
UploadModelInterface::FILE_TYPE_AUDIO => 'audio', |
40
|
|
|
UploadModelInterface::FILE_TYPE_VIDEO => 'video', |
41
|
|
|
UploadModelInterface::FILE_TYPE_APP => 'application', |
42
|
|
|
UploadModelInterface::FILE_TYPE_TEXT => 'text', |
43
|
|
|
UploadModelInterface::FILE_TYPE_OTHER => 'other', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* AWS access key ID and secret access key. |
48
|
|
|
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html |
49
|
|
|
* |
50
|
|
|
* @var array|callable |
51
|
|
|
*/ |
52
|
|
|
public $credentials; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Region to connect to. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public $region = 'us-west-2'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* S3 client version. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public $clientVersion = 'latest'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Amazon web services S3 default bucket. |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
public $s3DefaultBucket; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Buckets to upload files depending on the owner. |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
*/ |
80
|
|
|
public $s3Buckets = []; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Amazon web services SDK S3 client. |
84
|
|
|
* |
85
|
|
|
* @var S3MultiRegionClient|S3ClientInterface |
86
|
|
|
*/ |
87
|
|
|
private $s3Client; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Initialize. |
91
|
|
|
*/ |
92
|
|
|
public function init() |
93
|
|
|
{ |
94
|
|
|
if (!is_array($this->credentials) && !is_callable($this->credentials)) { |
95
|
|
|
throw new InvalidConfigException('Credentials are not defined correctly.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->s3Client = new S3MultiRegionClient([ |
|
|
|
|
99
|
|
|
'version' => $this->clientVersion, |
100
|
|
|
'region' => $this->region, |
101
|
|
|
'credentials' => $this->credentials, |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Sets a mediafile model for upload file. |
107
|
|
|
* |
108
|
|
|
* @param Mediafile $mediafileModel |
109
|
|
|
* |
110
|
|
|
* @return UploadModelInterface |
111
|
|
|
*/ |
112
|
|
|
public function setModelForSave(Mediafile $mediafileModel): UploadModelInterface |
113
|
|
|
{ |
114
|
|
|
/* @var UploadModelInterface $object */ |
115
|
|
|
$object = Yii::createObject(ArrayHelper::merge([ |
116
|
|
|
'class' => S3Upload::class, |
117
|
|
|
'mediafileModel' => $mediafileModel, |
118
|
|
|
'uploadDirs' => $this->uploadDirs, |
119
|
|
|
's3Client' => $this->s3Client, |
120
|
|
|
's3DefaultBucket' => $this->s3DefaultBucket, |
121
|
|
|
's3Buckets' => $this->s3Buckets, |
122
|
|
|
], $this->getBaseConfigForSave()) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $object; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Sets a mediafile model for delete file. |
130
|
|
|
* |
131
|
|
|
* @param Mediafile $mediafileModel |
132
|
|
|
* |
133
|
|
|
* @return UploadModelInterface |
134
|
|
|
*/ |
135
|
|
|
public function setModelForDelete(Mediafile $mediafileModel): UploadModelInterface |
136
|
|
|
{ |
137
|
|
|
/* @var UploadModelInterface $object */ |
138
|
|
|
$object = Yii::createObject([ |
139
|
|
|
'class' => S3Upload::class, |
140
|
|
|
'mediafileModel' => $mediafileModel, |
141
|
|
|
's3Client' => $this->s3Client, |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
return $object; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..