-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadFileBehavior.php
243 lines (206 loc) · 5.67 KB
/
UploadFileBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* Yii upload file behaviour
*
* @author Demin Dmitry <sizemail@gmail.com>
* @link http://size.perm.ru
* @copyright 2014 SiZE
* @since 1.15
*/
class UploadFileBehavior extends CActiveRecordBehavior {
/**
* @var string Validator name according to CValidator::createValidator name attribute
*/
public $validatorName = 'file';
/**
* @var string Model file attribute
*/
public $attribute = 'file';
/**
* @var string Upload path alias. Default to root /upload dir.
*/
public $pathAlias = 'webroot.upload';
/**
* File Validator properties
*/
public $allowEmpty;
public $types;
public $mimeTypes;
public $minSize;
public $maxSize;
public $tooLarge;
public $tooSmall;
public $wrongType;
public $wrongMimeType;
public $maxFiles;
public $tooMany;
public $safe;
/**
* Base Validator properties
*/
public $message;
public $skipOnError;
/**
* Some properties to set from config
*/
protected $on;
protected $except;
protected $enableClientValidation;
/**
* Additional properties
*/
/**
* @var bool Save original name on true or generate new.
*/
public $originalName = false;
/**
* @var array Scenarios
* This can be a string value with scenario name or scenario name
* as a key with file validator params
* array( 'insert', 'update' )
* array( 'insert' => array( 'types' => 'jpg,png,gif' ), 'update' )
*/
public $scenarios = array( 'insert', 'update' );
/**
* @var string Path to upload dir. Generates from $pathAlias.
*/
public $path;
/**
* @param array $rules additional validators
*/
public $rules = array();
public function attach( $owner ){
parent::attach( $owner );
}
protected function setupFileValidatorParams(){
$params = array();
if ( $this->allowEmpty !== null ) {
$params[ 'allowEmpty' ] = $this->allowEmpty;
}
if ( $this->types !== null ) {
$params[ 'types' ] = $this->types;
}
if ( $this->mimeTypes !== null ) {
$params[ 'mimeTypes' ] = $this->mimeTypes;
}
if ( $this->minSize !== null ) {
$params[ 'minSize' ] = $this->minSize;
}
if ( $this->maxSize !== null ) {
$params[ 'maxSize' ] = $this->maxSize;
}
if ( $this->tooLarge !== null ) {
$params[ 'tooLarge' ] = $this->tooLarge;
}
if ( $this->tooSmall !== null ) {
$params[ 'tooSmall' ] = $this->tooSmall;
}
if ( $this->wrongType !== null ) {
$params[ 'wrongType' ] = $this->wrongType;
}
if ( $this->wrongMimeType !== null ) {
$params[ 'wrongMimeType' ] = $this->wrongMimeType;
}
if ( $this->maxFiles !== null ) {
$params[ 'maxFiles' ] = $this->maxFiles;
}
if ( $this->tooMany !== null ) {
$params[ 'tooMany' ] = $this->tooMany;
}
if ( $this->safe !== null ) {
$params[ 'safe' ] = $this->safe;
}
if ( $this->message !== null ) {
$params[ 'message' ] = $this->message;
}
if ( $this->skipOnError !== null ) {
$params[ 'skipOnError' ] = $this->skipOnError;
}
if ( $this->enableClientValidation !== null ) {
$params[ 'enableClientValidation' ] = $this->enableClientValidation;
}
if ( $this->on !== null ) {
$params[ 'on' ] = $this->on;
}
if ( $this->except !== null ) {
$params[ 'except' ] = $this->except;
}
foreach( $this->scenarios as $scenario => $items ) if ( is_array( $items ) && $this->owner->scenario == $scenario ) {
foreach( $items as $k => $v ) if ( property_exists( $this, $k ) ) {
$params[ $k ] = $v;
}
}
return $params;
}
protected function scenarioPass(){
$pass = false;
foreach( $this->scenarios as $k => $v ){
$pass = is_array( $v ) ? $this->owner->scenario == $k : $this->owner->scenario == $v;
if ( $pass ) {
break;
}
}
return $pass;
}
public function beforeValidate( $event ){
if ( !$this->scenarioPass() ) {
return;
}
$fileValidator = CValidator::createValidator( $this->validatorName, $this->owner, $this->attribute, $this->setupFileValidatorParams() );
$this->owner->validatorList->add( $fileValidator );
}
public function afterValidate( $event ){
if ( !$this->scenarioPass() ) {
return;
}
if ( $this->owner->hasErrors() && !$this->owner->hasErrors( $this->attribute ) ) {
$file = CUploadedFile::getInstance( $this->owner, $this->attribute );
if ( $file !== null ) {
$this->owner->addError( $this->attribute, 'Не забудьте заново указать файл в поле «'.$this->owner->getAttributeLabel( $this->attribute ).'».' );
}
}
}
public function beforeSave( $event ){
if ( !$this->scenarioPass() ) {
return;
}
if ( !( $file = CUploadedFile::getInstance( $this->owner, $this->attribute ) ) ) {
return;
}
// Delete current file before new upload
$this->deleteCurrentFile();
$filename = $this->originalName ? $file->name : $this->generateFileName( $file );
$this->owner->{$this->attribute} = $filename;
$file->saveAs( $this->uploadPath.$filename );
}
public function beforeDelete( $event ){
$this->deleteCurrentFile();
}
public function getUploadPath(){
if ( !$this->path ) {
$path = Yii::getPathOfAlias( $this->pathAlias ).DIRECTORY_SEPARATOR;
if ( !is_dir( $path ) ) {
mkdir( $path, 0777, true );
chmod( $path, 0777 );
}
$this->path = $path;
}
return $this->path;
}
public function deleteCurrentFile(){
$filePath = $this->uploadPath.$this->owner->{$this->attribute};
if ( @is_file( $filePath ) ) {
@unlink( $filePath );
}
}
public function generateFileName( CUploadedFile $file, $length = 8 ){
$extension = $file->getExtensionName();
$name = mb_substr( md5( uniqid( mt_rand(), true ) ), 0, $length );
$filename = $name.'.'.$extension;
if ( file_exists( $this->uploadPath.$filename ) ) {
return $this->generateFileName( $file, $length );
} else {
return $filename;
}
}
}