@@ -34,27 +34,31 @@ discard block |
||
34 | 34 | |
35 | 35 | $this->_onlyUploaded = $onlyUploaded; |
36 | 36 | |
37 | - if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) |
|
38 | - throw new BadRequestHttpException("Attribute or class name not set."); |
|
37 | + if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) { |
|
38 | + throw new BadRequestHttpException("Attribute or class name not set."); |
|
39 | + } |
|
39 | 40 | |
40 | 41 | // Загружаем полученные данные |
41 | 42 | $this->_instance = $file; |
42 | 43 | $this->_attribute = $data['attribute']; |
43 | 44 | |
44 | - if (!file_exists($this->_instance->tempName)) |
|
45 | - throw new ErrorException("Tmp file not found on disk."); |
|
45 | + if (!file_exists($this->_instance->tempName)) { |
|
46 | + throw new ErrorException("Tmp file not found on disk."); |
|
47 | + } |
|
46 | 48 | |
47 | 49 | // Инициализируем класс владельца файла для валидаций и ставим сценарий |
48 | 50 | $this->_owner = new $data['modelClass']; |
49 | 51 | |
50 | - if (isset($data['scenario'])) |
|
51 | - $this->_owner->setScenario($data['scenario']); |
|
52 | + if (isset($data['scenario'])) { |
|
53 | + $this->_owner->setScenario($data['scenario']); |
|
54 | + } |
|
52 | 55 | |
53 | 56 | |
54 | 57 | if (isset($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'])) { |
55 | 58 | foreach ($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'] as $validator) { |
56 | - if (!$validator->validate($this->_instance, $error)) |
|
57 | - throw new BadRequestHttpException($error); |
|
59 | + if (!$validator->validate($this->_instance, $error)) { |
|
60 | + throw new BadRequestHttpException($error); |
|
61 | + } |
|
58 | 62 | } |
59 | 63 | |
60 | 64 | } |
@@ -70,10 +74,12 @@ discard block |
||
70 | 74 | $this->_model->content_type = $this->_instance->type; |
71 | 75 | $this->_model->size = $this->_instance->size; |
72 | 76 | $this->_model->type = $this->detectType(); |
73 | - if ($identity) |
|
74 | - $this->_model->user_id = $identity->id; |
|
75 | - if ($this->_model->type == FileType::VIDEO) |
|
76 | - $this->_model->video_status = 0; |
|
77 | + if ($identity) { |
|
78 | + $this->_model->user_id = $identity->id; |
|
79 | + } |
|
80 | + if ($this->_model->type == FileType::VIDEO) { |
|
81 | + $this->_model->video_status = 0; |
|
82 | + } |
|
77 | 83 | |
78 | 84 | //Генерируем полный новый адрес сохранения файла |
79 | 85 | $this->_fullPath = Yii::$app->getModule('files')->storageFullPath . DIRECTORY_SEPARATOR . $this->_model->filename; |
@@ -85,10 +91,12 @@ discard block |
||
85 | 91 | public function detectType() |
86 | 92 | { |
87 | 93 | $contentTypeArray = explode('/', $this->_model->content_type); |
88 | - if ($contentTypeArray[0] == 'image') |
|
89 | - return FileType::IMAGE; |
|
90 | - if ($contentTypeArray[0] == 'video') |
|
91 | - return FileType::VIDEO; |
|
94 | + if ($contentTypeArray[0] == 'image') { |
|
95 | + return FileType::IMAGE; |
|
96 | + } |
|
97 | + if ($contentTypeArray[0] == 'video') { |
|
98 | + return FileType::VIDEO; |
|
99 | + } |
|
92 | 100 | return FileType::FILE; |
93 | 101 | } |
94 | 102 | |
@@ -101,10 +109,11 @@ discard block |
||
101 | 109 | $path = Yii::$app->getModule('files')->storageFullPath . $this->_model->filename; |
102 | 110 | |
103 | 111 | if ($this->_model->save()) { |
104 | - if (!$this->_onlyUploaded) |
|
105 | - copy($this->_instance->tempName, $this->_fullPath); |
|
106 | - else |
|
107 | - $this->_instance->saveAs($this->_fullPath, false); |
|
112 | + if (!$this->_onlyUploaded) { |
|
113 | + copy($this->_instance->tempName, $this->_fullPath); |
|
114 | + } else { |
|
115 | + $this->_instance->saveAs($this->_fullPath, false); |
|
116 | + } |
|
108 | 117 | } |
109 | 118 | |
110 | 119 | if ($this->_model->type == FileType::IMAGE) { |
@@ -33,11 +33,13 @@ discard block |
||
33 | 33 | { |
34 | 34 | $this->_file = $file; |
35 | 35 | |
36 | - if ($this->_file->type != FileType::IMAGE) |
|
37 | - throw new ErrorException('This file is not an image.'); |
|
36 | + if ($this->_file->type != FileType::IMAGE) { |
|
37 | + throw new ErrorException('This file is not an image.'); |
|
38 | + } |
|
38 | 39 | |
39 | - if (!file_exists($this->_file->rootPath)) |
|
40 | - throw new ErrorException('File not found on disk'); |
|
40 | + if (!file_exists($this->_file->rootPath)) { |
|
41 | + throw new ErrorException('File not found on disk'); |
|
42 | + } |
|
41 | 43 | |
42 | 44 | $this->_maxHeight = $maxHeight; |
43 | 45 | $this->_maxWidth = $maxWidth; |
@@ -51,16 +53,18 @@ discard block |
||
51 | 53 | */ |
52 | 54 | public function execute(): bool |
53 | 55 | { |
54 | - if ($this->_file->content_type == 'image/svg+xml') |
|
55 | - return true; |
|
56 | + if ($this->_file->content_type == 'image/svg+xml') { |
|
57 | + return true; |
|
58 | + } |
|
56 | 59 | |
57 | 60 | $image = new SimpleImage(); |
58 | 61 | $image->load($this->_file->rootPath); |
59 | 62 | |
60 | 63 | if ($image->getWidth() > $this->_maxWidth || $image->getHeight() > $this->_maxHeight) { |
61 | 64 | $image->resizeToWidth($this->_maxWidth); |
62 | - if ($this->_file->content_type == 'image/png') |
|
63 | - $this->_imageType = IMAGETYPE_PNG; |
|
65 | + if ($this->_file->content_type == 'image/png') { |
|
66 | + $this->_imageType = IMAGETYPE_PNG; |
|
67 | + } |
|
64 | 68 | $image->save($this->_file->rootPath, $this->_imageType, $this->_compression); |
65 | 69 | $this->_file->size = filesize($this->_file->rootPath); |
66 | 70 | return $this->_file->save(false, ['size']); |
@@ -31,8 +31,9 @@ discard block |
||
31 | 31 | $this->width = $width; |
32 | 32 | $this->webp = $webp; |
33 | 33 | |
34 | - if ($this->model->type != FileType::IMAGE) |
|
35 | - throw new ErrorException('File is not an image.'); |
|
34 | + if ($this->model->type != FileType::IMAGE) { |
|
35 | + throw new ErrorException('File is not an image.'); |
|
36 | + } |
|
36 | 37 | } |
37 | 38 | |
38 | 39 | /** |
@@ -40,8 +41,9 @@ discard block |
||
40 | 41 | */ |
41 | 42 | public function getUrl() |
42 | 43 | { |
43 | - if ($this->model->isSvg()) |
|
44 | - return $this->model->getRootPath(); |
|
44 | + if ($this->model->isSvg()) { |
|
45 | + return $this->model->getRootPath(); |
|
46 | + } |
|
45 | 47 | |
46 | 48 | $this->fileName = Yii::$app->getModule('files')->cacheFullPath . DIRECTORY_SEPARATOR . $this->model->makeNameWithSize($this->model->filename, |
47 | 49 | $this->width, 0); |
@@ -50,14 +52,17 @@ discard block |
||
50 | 52 | |
51 | 53 | $this->prepareFolder(); |
52 | 54 | |
53 | - if (!file_exists($this->fileName) || filesize($this->fileName) == 0) |
|
54 | - $this->createPreview(); |
|
55 | + if (!file_exists($this->fileName) || filesize($this->fileName) == 0) { |
|
56 | + $this->createPreview(); |
|
57 | + } |
|
55 | 58 | |
56 | - if ($this->webp && !file_exists($this->fileNameWebp)) |
|
57 | - $this->createPreviewWebp(); |
|
59 | + if ($this->webp && !file_exists($this->fileNameWebp)) { |
|
60 | + $this->createPreviewWebp(); |
|
61 | + } |
|
58 | 62 | |
59 | - if ($this->webp) |
|
60 | - return $this->fileNameWebp; |
|
63 | + if ($this->webp) { |
|
64 | + return $this->fileNameWebp; |
|
65 | + } |
|
61 | 66 | |
62 | 67 | return $this->fileName; |
63 | 68 | } |
@@ -79,8 +84,9 @@ discard block |
||
79 | 84 | } |
80 | 85 | |
81 | 86 | $saveType = $img->image_type; |
82 | - if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) |
|
83 | - $saveType = IMG_JPEG; |
|
87 | + if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) { |
|
88 | + $saveType = IMG_JPEG; |
|
89 | + } |
|
84 | 90 | $img->save($this->fileName, $saveType); |
85 | 91 | } |
86 | 92 | |
@@ -99,20 +105,24 @@ discard block |
||
99 | 105 | */ |
100 | 106 | protected function prepareFolder() |
101 | 107 | { |
102 | - if (!file_exists(Yii::$app->getModule('files')->cacheFullPath)) |
|
103 | - mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
108 | + if (!file_exists(Yii::$app->getModule('files')->cacheFullPath)) { |
|
109 | + mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
110 | + } |
|
104 | 111 | $folders = []; |
105 | 112 | $lastFolder = '/'; |
106 | 113 | $explodes = explode('/', $this->fileName); |
107 | 114 | array_pop($explodes); |
108 | - if (empty($explodes)) |
|
109 | - return; |
|
115 | + if (empty($explodes)) { |
|
116 | + return; |
|
117 | + } |
|
110 | 118 | foreach ($explodes as $folder) { |
111 | - if (empty($folder)) |
|
112 | - continue; |
|
119 | + if (empty($folder)) { |
|
120 | + continue; |
|
121 | + } |
|
113 | 122 | $lastFolder = $lastFolder . $folder . '/'; |
114 | - if (!file_exists($lastFolder)) |
|
115 | - mkdir($lastFolder); |
|
123 | + if (!file_exists($lastFolder)) { |
|
124 | + mkdir($lastFolder); |
|
125 | + } |
|
116 | 126 | $folders[] = $lastFolder; |
117 | 127 | } |
118 | 128 | } |
@@ -68,9 +68,11 @@ discard block |
||
68 | 68 | ] |
69 | 69 | )->execute(); |
70 | 70 | |
71 | - if ($ids) foreach ($ids as $id) { |
|
71 | + if ($ids) { |
|
72 | + foreach ($ids as $id) { |
|
72 | 73 | if (empty($id)) |
73 | 74 | continue; |
75 | + } |
|
74 | 76 | $file = File::findOne($id); |
75 | 77 | if ($file) { |
76 | 78 | $file->object_id = $this->owner->id; |
@@ -105,8 +107,9 @@ discard block |
||
105 | 107 | in_array($this->owner->scenario, $params['requiredOn']) && |
106 | 108 | !in_array($this->owner->scenario, $params['requiredExcept']) && |
107 | 109 | !isset($this->_values[$attributeIds][1]) |
108 | - ) |
|
109 | - $this->owner->addError($attributeName, $params['requiredMessage']); |
|
110 | + ) { |
|
111 | + $this->owner->addError($attributeName, $params['requiredMessage']); |
|
112 | + } |
|
110 | 113 | } |
111 | 114 | } |
112 | 115 | |
@@ -123,8 +126,8 @@ discard block |
||
123 | 126 | $validators = $owner->validators; |
124 | 127 | |
125 | 128 | // Пробегаемся по валидаторам и вычисляем, какие из них касаются наших файл-полей |
126 | - if ($validators) |
|
127 | - foreach ($validators as $key => $validator) { |
|
129 | + if ($validators) { |
|
130 | + foreach ($validators as $key => $validator) { |
|
128 | 131 | |
129 | 132 | // Сначала пробегаемся по файловым валидаторам |
130 | 133 | if ($validator::className() == 'yii\validators\FileValidator' || $validator::className() == 'floor12\files\components\ReformatValidator') { |
@@ -132,6 +135,7 @@ discard block |
||
132 | 135 | |
133 | 136 | if (is_string($params)) { |
134 | 137 | $field = $params; |
138 | + } |
|
135 | 139 | $params = []; |
136 | 140 | } |
137 | 141 | |
@@ -167,8 +171,10 @@ discard block |
||
167 | 171 | } |
168 | 172 | |
169 | 173 | // Добавляем дефолтный валидатор для прилетающих айдишников |
170 | - if ($this->attributes) foreach ($this->attributes as $fieldName => $fieldParams) { |
|
174 | + if ($this->attributes) { |
|
175 | + foreach ($this->attributes as $fieldName => $fieldParams) { |
|
171 | 176 | $validator = Validator::createValidator('safe', $owner, ["{$fieldName}_ids"]); |
177 | + } |
|
172 | 178 | $validators->append($validator); |
173 | 179 | } |
174 | 180 | } |
@@ -189,8 +195,9 @@ discard block |
||
189 | 195 | */ |
190 | 196 | public function canSetProperty($name, $checkVars = true) |
191 | 197 | { |
192 | - if (array_key_exists($this->getRealAttributeName($name), $this->attributes)) |
|
193 | - return true; |
|
198 | + if (array_key_exists($this->getRealAttributeName($name), $this->attributes)) { |
|
199 | + return true; |
|
200 | + } |
|
194 | 201 | |
195 | 202 | return parent::canSetProperty($name, $checkVars = true); |
196 | 203 | } |
@@ -216,8 +223,8 @@ discard block |
||
216 | 223 | isset($this->attributes[$att_name]['validator']) && |
217 | 224 | isset($this->attributes[$att_name]['validator']['yii\validators\FileValidator']) && |
218 | 225 | $this->attributes[$att_name]['validator']['yii\validators\FileValidator']->maxFiles > 1 |
219 | - ) |
|
220 | - $this->cachedFiles[$att_name] = File::find() |
|
226 | + ) { |
|
227 | + $this->cachedFiles[$att_name] = File::find() |
|
221 | 228 | ->where( |
222 | 229 | [ |
223 | 230 | 'object_id' => $this->owner->id, |
@@ -226,7 +233,7 @@ discard block |
||
226 | 233 | ]) |
227 | 234 | ->orderBy('ordering ASC') |
228 | 235 | ->all(); |
229 | - else { |
|
236 | + } else { |
|
230 | 237 | $this->cachedFiles[$att_name] = File::find() |
231 | 238 | ->where( |
232 | 239 | [ |
@@ -251,8 +258,9 @@ discard block |
||
251 | 258 | { |
252 | 259 | $attribute = $this->getRealAttributeName($name); |
253 | 260 | |
254 | - if (array_key_exists($attribute, $this->attributes)) |
|
255 | - $this->_values[$attribute] = $value; |
|
261 | + if (array_key_exists($attribute, $this->attributes)) { |
|
262 | + $this->_values[$attribute] = $value; |
|
263 | + } |
|
256 | 264 | } |
257 | 265 | |
258 | 266 |
@@ -86,29 +86,37 @@ discard block |
||
86 | 86 | { |
87 | 87 | $icon = IconHelper::FILE; |
88 | 88 | |
89 | - if ($this->content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') |
|
90 | - $icon = IconHelper::FILE_WORD; |
|
89 | + if ($this->content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') { |
|
90 | + $icon = IconHelper::FILE_WORD; |
|
91 | + } |
|
91 | 92 | |
92 | - if ($this->content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') |
|
93 | - $icon = IconHelper::FILE_EXCEL; |
|
93 | + if ($this->content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') { |
|
94 | + $icon = IconHelper::FILE_EXCEL; |
|
95 | + } |
|
94 | 96 | |
95 | - if ($this->content_type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation') |
|
96 | - $icon = IconHelper::FILE_POWERPOINT; |
|
97 | + if ($this->content_type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation') { |
|
98 | + $icon = IconHelper::FILE_POWERPOINT; |
|
99 | + } |
|
97 | 100 | |
98 | - if ($this->content_type == 'application/x-zip-compressed') |
|
99 | - $icon = IconHelper::FILE_ARCHIVE; |
|
101 | + if ($this->content_type == 'application/x-zip-compressed') { |
|
102 | + $icon = IconHelper::FILE_ARCHIVE; |
|
103 | + } |
|
100 | 104 | |
101 | - if ($this->content_type == 'application/octet-stream') |
|
102 | - $icon = IconHelper::FILE_ARCHIVE; |
|
105 | + if ($this->content_type == 'application/octet-stream') { |
|
106 | + $icon = IconHelper::FILE_ARCHIVE; |
|
107 | + } |
|
103 | 108 | |
104 | - if (preg_match('/audio/', $this->content_type)) |
|
105 | - $icon = IconHelper::FILE_AUDIO; |
|
109 | + if (preg_match('/audio/', $this->content_type)) { |
|
110 | + $icon = IconHelper::FILE_AUDIO; |
|
111 | + } |
|
106 | 112 | |
107 | - if (preg_match('/pdf/', $this->content_type)) |
|
108 | - $icon = IconHelper::FILE_PDF; |
|
113 | + if (preg_match('/pdf/', $this->content_type)) { |
|
114 | + $icon = IconHelper::FILE_PDF; |
|
115 | + } |
|
109 | 116 | |
110 | - if ($this->type == FileType::VIDEO) |
|
111 | - $icon = IconHelper::FILE_VIDEO; |
|
117 | + if ($this->type == FileType::VIDEO) { |
|
118 | + $icon = IconHelper::FILE_VIDEO; |
|
119 | + } |
|
112 | 120 | |
113 | 121 | return $icon; |
114 | 122 | } |
@@ -232,8 +240,9 @@ discard block |
||
232 | 240 | |
233 | 241 | public function getRootPreviewPath() |
234 | 242 | { |
235 | - if ($this->isSvg()) |
|
236 | - return $this->getRootPath(); |
|
243 | + if ($this->isSvg()) { |
|
244 | + return $this->getRootPath(); |
|
245 | + } |
|
237 | 246 | |
238 | 247 | return Yii::$app->getModule('files')->storageFullPath . DIRECTORY_SEPARATOR . $this->filename . '.jpg'; |
239 | 248 | } |
@@ -256,8 +265,9 @@ discard block |
||
256 | 265 | |
257 | 266 | public function getHref() |
258 | 267 | { |
259 | - if (Yii::$app->getModule('files')->hostStatic) |
|
260 | - return Yii::$app->getModule('files')->hostStatic . $this->filename; |
|
268 | + if (Yii::$app->getModule('files')->hostStatic) { |
|
269 | + return Yii::$app->getModule('files')->hostStatic . $this->filename; |
|
270 | + } |
|
261 | 271 | return Url::to(['/files/default/get', 'hash' => $this->hash]); |
262 | 272 | } |
263 | 273 | |
@@ -333,8 +343,9 @@ discard block |
||
333 | 343 | isset($this->behaviors['files']) && |
334 | 344 | isset($this->behaviors['files']->attributes[$this->field]) && |
335 | 345 | isset($this->behaviors['files']->attributes[$this->field]['watermark']) |
336 | - ) |
|
337 | - $this->behaviors['files']->attributes[$this->field]['watermark']; |
|
346 | + ) { |
|
347 | + $this->behaviors['files']->attributes[$this->field]['watermark']; |
|
348 | + } |
|
338 | 349 | } |
339 | 350 | |
340 | 351 | /** |
@@ -355,14 +366,17 @@ discard block |
||
355 | 366 | */ |
356 | 367 | public function getPreviewWebPath($width = 0, $height = 0, $webp = false) |
357 | 368 | { |
358 | - if (!file_exists($this->getRootPath())) |
|
359 | - return null; |
|
369 | + if (!file_exists($this->getRootPath())) { |
|
370 | + return null; |
|
371 | + } |
|
360 | 372 | |
361 | - if ($this->type != FileType::IMAGE) |
|
362 | - throw new \ErrorException('Requiested file is not an image and its implsible to resize it.'); |
|
373 | + if ($this->type != FileType::IMAGE) { |
|
374 | + throw new \ErrorException('Requiested file is not an image and its implsible to resize it.'); |
|
375 | + } |
|
363 | 376 | |
364 | - if (Yii::$app->getModule('files')->hostStatic) |
|
365 | - return Yii::$app->getModule('files')->hostStatic . $this->makeNameWithSize($this->filename, $width, $height, $webp = false); |
|
377 | + if (Yii::$app->getModule('files')->hostStatic) { |
|
378 | + return Yii::$app->getModule('files')->hostStatic . $this->makeNameWithSize($this->filename, $width, $height, $webp = false); |
|
379 | + } |
|
366 | 380 | |
367 | 381 | return Url::toRoute(['/files/default/image', 'hash' => $this->hash, 'width' => $width, 'height' => $height, 'webp' => $webp]); |
368 | 382 | } |
@@ -379,8 +393,9 @@ discard block |
||
379 | 393 | { |
380 | 394 | $extension = pathinfo($this->rootPath, PATHINFO_EXTENSION); |
381 | 395 | $rootPath = str_replace(".{$extension}", '', $name) . "_w" . $width . "h" . $height . ".{$extension}"; |
382 | - if ($webp) |
|
383 | - $rootPath = str_replace($extension, 'webp', $rootPath); |
|
396 | + if ($webp) { |
|
397 | + $rootPath = str_replace($extension, 'webp', $rootPath); |
|
398 | + } |
|
384 | 399 | return $rootPath; |
385 | 400 | } |
386 | 401 | |
@@ -395,8 +410,9 @@ discard block |
||
395 | 410 | */ |
396 | 411 | public function getPreviewRootPath($width = 0, $height = 0, $webp = false) |
397 | 412 | { |
398 | - if ($this->type != FileType::IMAGE) |
|
399 | - throw new \ErrorException('Requiested file is not an image and its implsible to resize it.'); |
|
413 | + if ($this->type != FileType::IMAGE) { |
|
414 | + throw new \ErrorException('Requiested file is not an image and its implsible to resize it.'); |
|
415 | + } |
|
400 | 416 | return $this->makeNameWithSize($this->rootPath, $width, $height, $webp); |
401 | 417 | } |
402 | 418 |