1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the yii2-tinify package. |
5
|
|
|
* |
6
|
|
|
* (c) Vladimir Kuprienko <[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 vintage\tinify; |
13
|
|
|
|
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\InvalidConfigException; |
16
|
|
|
use Tinify\AccountException; |
17
|
|
|
use Tinify\Source; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Component for Tinify PHP SDK provides uploading files to AWS S3 storage. |
21
|
|
|
* |
22
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
23
|
|
|
* @since 2.1 |
24
|
|
|
*/ |
25
|
|
|
class UploadedFileS3 extends UploadedFile |
26
|
|
|
{ |
27
|
|
|
const PARAM_KEY_AWS_ACCESS_KEY_ID = 'aws-access-key-id'; |
28
|
|
|
const PARAM_KEY_AWS_SECRET_ACCESS_KEY = 'aws-secret-access-key'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AWS region. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $region; |
36
|
|
|
/** |
37
|
|
|
* Path in bucket. |
38
|
|
|
* Example: images-bucket/uploads |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $path; |
43
|
|
|
/** |
44
|
|
|
* AWS access key id. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $accessKeyId; |
49
|
|
|
/** |
50
|
|
|
* AWS secret access key. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $secretAccessKey; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function init() |
60
|
|
|
{ |
61
|
|
|
parent::init(); |
62
|
|
|
|
63
|
|
|
if ( |
64
|
|
|
empty(Yii::$app->params[self::PARAM_KEY_AWS_ACCESS_KEY_ID]) || |
65
|
|
|
empty(Yii::$app->params[self::PARAM_KEY_AWS_SECRET_ACCESS_KEY]) |
66
|
|
|
) { |
67
|
|
|
throw new InvalidConfigException('You must provide AWS credentials'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->accessKeyId = Yii::$app->params[self::PARAM_KEY_AWS_ACCESS_KEY_ID]; |
71
|
|
|
$this->secretAccessKey = Yii::$app->params[self::PARAM_KEY_AWS_SECRET_ACCESS_KEY]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set AWS region. |
76
|
|
|
* |
77
|
|
|
* @param string $region |
78
|
|
|
* |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
|
|
public function setRegion($region) |
82
|
|
|
{ |
83
|
|
|
$this->region = $region; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set bucket name and path to save. |
90
|
|
|
* |
91
|
|
|
* @param string $path |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
|
|
public function setPath($path) |
96
|
|
|
{ |
97
|
|
|
$this->path = $path; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
|
|
protected function compressFile($tempFile, $resultFile) |
106
|
|
|
{ |
107
|
|
|
$result = false; |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
$source = Source::fromFile($tempFile); |
111
|
|
|
|
112
|
|
|
if (!empty($this->saveMetadata)) { |
113
|
|
|
$source = $source->preserve($this->saveMetadata); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$result = $source->store([ |
117
|
|
|
'service' => 's3', |
118
|
|
|
'region' => $this->region, |
119
|
|
|
'path' => $this->path . '/' . $resultFile, |
120
|
|
|
'aws_access_key_id' => $this->accessKeyId, |
121
|
|
|
'aws_secret_access_key' => $this->secretAccessKey, |
122
|
|
|
]); |
123
|
|
|
} catch (AccountException $ex) { |
124
|
|
|
throw $ex; |
125
|
|
|
} catch (\Exception $ex) { |
126
|
|
|
Yii::$app->getErrorHandler()->logException($ex); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return (bool) $result; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|