@@ -29,17 +29,21 @@ discard block |
||
29 | 29 | |
30 | 30 | $this->model = $model; |
31 | 31 | |
32 | - if (!$filePath || !$className || !$fieldName || !$storagePath) |
|
33 | - throw new ErrorException("Empty params not allowed."); |
|
32 | + if (!$filePath || !$className || !$fieldName || !$storagePath) { |
|
33 | + throw new ErrorException("Empty params not allowed."); |
|
34 | + } |
|
34 | 35 | |
35 | - if (!file_exists($storagePath)) |
|
36 | - throw new ErrorException("File storage not found on disk."); |
|
36 | + if (!file_exists($storagePath)) { |
|
37 | + throw new ErrorException("File storage not found on disk."); |
|
38 | + } |
|
37 | 39 | |
38 | - if (!file_exists($filePath)) |
|
39 | - throw new ErrorException("File not found on disk."); |
|
40 | + if (!file_exists($filePath)) { |
|
41 | + throw new ErrorException("File not found on disk."); |
|
42 | + } |
|
40 | 43 | |
41 | - if (!is_writable($storagePath)) |
|
42 | - throw new ErrorException("File storage is not writable."); |
|
44 | + if (!is_writable($storagePath)) { |
|
45 | + throw new ErrorException("File storage is not writable."); |
|
46 | + } |
|
43 | 47 | $this->filePath = $filePath; |
44 | 48 | $this->fileName = $fileName; |
45 | 49 | $this->fieldName = $fieldName; |
@@ -65,16 +69,19 @@ discard block |
||
65 | 69 | $this->model->field = $this->fieldName; |
66 | 70 | $this->model->class = $this->className; |
67 | 71 | $this->model->filename = $filename; |
68 | - if ($this->model->filename) |
|
69 | - $this->model->title = $this->model->filename; |
|
70 | - else |
|
71 | - $this->model->title = rand(0, 99999); #такой прикол ) |
|
72 | + if ($this->model->filename) { |
|
73 | + $this->model->title = $this->model->filename; |
|
74 | + } else { |
|
75 | + $this->model->title = rand(0, 99999); |
|
76 | + } |
|
77 | + #такой прикол ) |
|
72 | 78 | $this->model->content_type = $this->model->mime_content_type($new_path); |
73 | 79 | $this->model->type = $this->detectType(); |
74 | 80 | $this->model->size = filesize($new_path); |
75 | 81 | $this->model->created = time(); |
76 | - if ($this->model->type == FileType::VIDEO) |
|
77 | - $this->model->video_status = 0; |
|
82 | + if ($this->model->type == FileType::VIDEO) { |
|
83 | + $this->model->video_status = 0; |
|
84 | + } |
|
78 | 85 | |
79 | 86 | if ($this->model->save()) { |
80 | 87 | |
@@ -110,10 +117,12 @@ discard block |
||
110 | 117 | private function detectType() |
111 | 118 | { |
112 | 119 | $contentTypeArray = explode('/', $this->model->content_type); |
113 | - if ($contentTypeArray[0] == 'image') |
|
114 | - return FileType::IMAGE; |
|
115 | - if ($contentTypeArray[0] == 'video') |
|
116 | - return FileType::VIDEO; |
|
120 | + if ($contentTypeArray[0] == 'image') { |
|
121 | + return FileType::IMAGE; |
|
122 | + } |
|
123 | + if ($contentTypeArray[0] == 'video') { |
|
124 | + return FileType::VIDEO; |
|
125 | + } |
|
117 | 126 | return FileType::FILE; |
118 | 127 | } |
119 | 128 | } |
@@ -21,8 +21,9 @@ |
||
21 | 21 | */ |
22 | 22 | public function run(): ?string |
23 | 23 | { |
24 | - if ($this->model->type !== FileType::VIDEO) |
|
25 | - return null; |
|
24 | + if ($this->model->type !== FileType::VIDEO) { |
|
25 | + return null; |
|
26 | + } |
|
26 | 27 | $source = Html::tag('source', null, ['src' => $this->model->getHref(), 'type' => $this->model->content_type]); |
27 | 28 | return Html::tag('video', $source, $this->options); |
28 | 29 | } |
@@ -46,14 +46,17 @@ discard block |
||
46 | 46 | private function loadAndCheckModel(string $hash): void |
47 | 47 | { |
48 | 48 | $this->model = File::findOne(['hash' => $hash]); |
49 | - if (!$this->model) |
|
50 | - throw new NotFoundHttpException("Запрашиваемый файл не найден"); |
|
49 | + if (!$this->model) { |
|
50 | + throw new NotFoundHttpException("Запрашиваемый файл не найден"); |
|
51 | + } |
|
51 | 52 | |
52 | - if (!$this->model->isImage() && !$this->model->isVideo()) |
|
53 | - throw new NotFoundHttpException("Запрашиваемый файл не является изображением"); |
|
53 | + if (!$this->model->isImage() && !$this->model->isVideo()) { |
|
54 | + throw new NotFoundHttpException("Запрашиваемый файл не является изображением"); |
|
55 | + } |
|
54 | 56 | |
55 | - if (!file_exists($this->model->rootPath)) |
|
56 | - throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
57 | + if (!file_exists($this->model->rootPath)) { |
|
58 | + throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
59 | + } |
|
57 | 60 | } |
58 | 61 | |
59 | 62 | /** |
@@ -65,8 +68,9 @@ discard block |
||
65 | 68 | { |
66 | 69 | $filename = Yii::createObject(ImagePreviewer::class, [$this->model, $width, $webp])->getUrl(); |
67 | 70 | |
68 | - if (!file_exists($filename)) |
|
69 | - throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
71 | + if (!file_exists($filename)) { |
|
72 | + throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
73 | + } |
|
70 | 74 | |
71 | 75 | $response = Yii::$app->response; |
72 | 76 | $response->format = Response::FORMAT_RAW; |
@@ -14,11 +14,13 @@ |
||
14 | 14 | { |
15 | 15 | $model = File::findOne(['hash' => $hash]); |
16 | 16 | |
17 | - if (!$model) |
|
18 | - throw new NotFoundHttpException("Запрашиваемый файл не найден"); |
|
17 | + if (!$model) { |
|
18 | + throw new NotFoundHttpException("Запрашиваемый файл не найден"); |
|
19 | + } |
|
19 | 20 | |
20 | - if (!file_exists($model->rootPath)) |
|
21 | - throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
21 | + if (!file_exists($model->rootPath)) { |
|
22 | + throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
23 | + } |
|
22 | 24 | |
23 | 25 | Yii::$app->response->headers->set('Last-Modified', date("c", $model->created)); |
24 | 26 | Yii::$app->response->headers->set('Cache-Control', 'public, max-age=' . (60 * 60 * 24 * 15)); |
@@ -30,8 +30,9 @@ discard block |
||
30 | 30 | $this->width = $width; |
31 | 31 | $this->webp = $webp; |
32 | 32 | |
33 | - if (!$this->model->isImage() && !$this->model->isVideo()) |
|
34 | - throw new ErrorException('File is not an image or video.'); |
|
33 | + if (!$this->model->isImage() && !$this->model->isVideo()) { |
|
34 | + throw new ErrorException('File is not an image or video.'); |
|
35 | + } |
|
35 | 36 | } |
36 | 37 | |
37 | 38 | /** |
@@ -41,8 +42,9 @@ discard block |
||
41 | 42 | */ |
42 | 43 | public function getUrl() |
43 | 44 | { |
44 | - if ($this->model->isSvg()) |
|
45 | - return $this->model->getRootPath(); |
|
45 | + if ($this->model->isSvg()) { |
|
46 | + return $this->model->getRootPath(); |
|
47 | + } |
|
46 | 48 | |
47 | 49 | $cachePath = Yii::$app->getModule('files')->cacheFullPath; |
48 | 50 | $jpegName = $this->model->makeNameWithSize($this->model->filename, $this->width); |
@@ -56,21 +58,25 @@ discard block |
||
56 | 58 | $sourceImagePath = $this->model->rootPath; |
57 | 59 | if ($this->model->isVideo()) { |
58 | 60 | $sourceImagePath = $this->fileName . '.jpeg'; |
59 | - if (!is_file($sourceImagePath)) |
|
60 | - Yii::createObject(VideoFrameExtractor::class, [ |
|
61 | + if (!is_file($sourceImagePath)) { |
|
62 | + Yii::createObject(VideoFrameExtractor::class, [ |
|
61 | 63 | $this->model->rootPath, |
62 | 64 | $sourceImagePath |
63 | 65 | ])->extract(); |
66 | + } |
|
64 | 67 | } |
65 | 68 | |
66 | - if (!is_file($this->fileName) || filesize($this->fileName) == 0) |
|
67 | - $this->createPreview($sourceImagePath); |
|
69 | + if (!is_file($this->fileName) || filesize($this->fileName) == 0) { |
|
70 | + $this->createPreview($sourceImagePath); |
|
71 | + } |
|
68 | 72 | |
69 | - if ($this->webp && !file_exists($this->fileNameWebp)) |
|
70 | - $this->createPreviewWebp(); |
|
73 | + if ($this->webp && !file_exists($this->fileNameWebp)) { |
|
74 | + $this->createPreviewWebp(); |
|
75 | + } |
|
71 | 76 | |
72 | - if ($this->webp) |
|
73 | - return $this->fileNameWebp; |
|
77 | + if ($this->webp) { |
|
78 | + return $this->fileNameWebp; |
|
79 | + } |
|
74 | 80 | |
75 | 81 | return $this->fileName; |
76 | 82 | } |
@@ -80,19 +86,23 @@ discard block |
||
80 | 86 | */ |
81 | 87 | protected function prepareFolder() |
82 | 88 | { |
83 | - if (!file_exists(Yii::$app->getModule('files')->cacheFullPath)) |
|
84 | - mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
89 | + if (!file_exists(Yii::$app->getModule('files')->cacheFullPath)) { |
|
90 | + mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
91 | + } |
|
85 | 92 | $lastFolder = '/'; |
86 | 93 | $explodes = explode('/', $this->fileName); |
87 | 94 | array_pop($explodes); |
88 | - if (empty($explodes)) |
|
89 | - return; |
|
95 | + if (empty($explodes)) { |
|
96 | + return; |
|
97 | + } |
|
90 | 98 | foreach ($explodes as $folder) { |
91 | - if (empty($folder)) |
|
92 | - continue; |
|
99 | + if (empty($folder)) { |
|
100 | + continue; |
|
101 | + } |
|
93 | 102 | $lastFolder = $lastFolder . $folder . '/'; |
94 | - if (!file_exists($lastFolder)) |
|
95 | - mkdir($lastFolder); |
|
103 | + if (!file_exists($lastFolder)) { |
|
104 | + mkdir($lastFolder); |
|
105 | + } |
|
96 | 106 | } |
97 | 107 | } |
98 | 108 | |
@@ -115,8 +125,9 @@ discard block |
||
115 | 125 | } |
116 | 126 | |
117 | 127 | $saveType = $img->image_type; |
118 | - if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) |
|
119 | - $saveType = IMG_JPEG; |
|
128 | + if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) { |
|
129 | + $saveType = IMG_JPEG; |
|
130 | + } |
|
120 | 131 | $img->save($this->fileName, $saveType); |
121 | 132 | } |
122 | 133 |
@@ -35,27 +35,31 @@ discard block |
||
35 | 35 | |
36 | 36 | $this->_onlyUploaded = $onlyUploaded; |
37 | 37 | |
38 | - if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) |
|
39 | - throw new BadRequestHttpException("Attribute or class name not set."); |
|
38 | + if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) { |
|
39 | + throw new BadRequestHttpException("Attribute or class name not set."); |
|
40 | + } |
|
40 | 41 | |
41 | 42 | // Загружаем полученные данные |
42 | 43 | $this->_instance = $file; |
43 | 44 | $this->_attribute = $data['attribute']; |
44 | 45 | |
45 | - if (!file_exists($this->_instance->tempName)) |
|
46 | - throw new ErrorException("Tmp file not found on disk."); |
|
46 | + if (!file_exists($this->_instance->tempName)) { |
|
47 | + throw new ErrorException("Tmp file not found on disk."); |
|
48 | + } |
|
47 | 49 | |
48 | 50 | // Инициализируем класс владельца файла для валидаций и ставим сценарий |
49 | 51 | $this->_owner = new $data['modelClass']; |
50 | 52 | |
51 | - if (isset($data['scenario'])) |
|
52 | - $this->_owner->setScenario($data['scenario']); |
|
53 | + if (isset($data['scenario'])) { |
|
54 | + $this->_owner->setScenario($data['scenario']); |
|
55 | + } |
|
53 | 56 | |
54 | 57 | |
55 | 58 | if (isset($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'])) { |
56 | 59 | foreach ($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'] as $validator) { |
57 | - if (!$validator->validate($this->_instance, $error)) |
|
58 | - throw new BadRequestHttpException($error); |
|
60 | + if (!$validator->validate($this->_instance, $error)) { |
|
61 | + throw new BadRequestHttpException($error); |
|
62 | + } |
|
59 | 63 | } |
60 | 64 | |
61 | 65 | } |
@@ -71,10 +75,12 @@ discard block |
||
71 | 75 | $this->_model->content_type = $this->_instance->type; |
72 | 76 | $this->_model->size = $this->_instance->size; |
73 | 77 | $this->_model->type = $this->detectType(); |
74 | - if ($identity) |
|
75 | - $this->_model->user_id = $identity->getId(); |
|
76 | - if ($this->_model->type == FileType::VIDEO) |
|
77 | - $this->_model->video_status = 0; |
|
78 | + if ($identity) { |
|
79 | + $this->_model->user_id = $identity->getId(); |
|
80 | + } |
|
81 | + if ($this->_model->type == FileType::VIDEO) { |
|
82 | + $this->_model->video_status = 0; |
|
83 | + } |
|
78 | 84 | |
79 | 85 | //Генерируем полный новый адрес сохранения файла |
80 | 86 | $this->_fullPath = Yii::$app->getModule('files')->storageFullPath . DIRECTORY_SEPARATOR . $this->_model->filename; |
@@ -86,10 +92,12 @@ discard block |
||
86 | 92 | public function detectType() |
87 | 93 | { |
88 | 94 | $contentTypeArray = explode('/', $this->_model->content_type); |
89 | - if ($contentTypeArray[0] == 'image') |
|
90 | - return FileType::IMAGE; |
|
91 | - if ($contentTypeArray[0] == 'video') |
|
92 | - return FileType::VIDEO; |
|
95 | + if ($contentTypeArray[0] == 'image') { |
|
96 | + return FileType::IMAGE; |
|
97 | + } |
|
98 | + if ($contentTypeArray[0] == 'video') { |
|
99 | + return FileType::VIDEO; |
|
100 | + } |
|
93 | 101 | return FileType::FILE; |
94 | 102 | } |
95 | 103 | |
@@ -102,10 +110,11 @@ discard block |
||
102 | 110 | $path = Yii::$app->getModule('files')->storageFullPath . $this->_model->filename; |
103 | 111 | |
104 | 112 | if ($this->_model->save()) { |
105 | - if (!$this->_onlyUploaded) |
|
106 | - copy($this->_instance->tempName, $this->_fullPath); |
|
107 | - else |
|
108 | - $this->_instance->saveAs($this->_fullPath, false); |
|
113 | + if (!$this->_onlyUploaded) { |
|
114 | + copy($this->_instance->tempName, $this->_fullPath); |
|
115 | + } else { |
|
116 | + $this->_instance->saveAs($this->_fullPath, false); |
|
117 | + } |
|
109 | 118 | } |
110 | 119 | |
111 | 120 | if ($this->_model->type == FileType::IMAGE) { |
@@ -19,14 +19,17 @@ discard block |
||
19 | 19 | { |
20 | 20 | $this->ffmpegBin = Yii::$app->getModule('files')->ffmpeg; |
21 | 21 | |
22 | - if (!file_exists($this->ffmpegBin)) |
|
23 | - throw new ErrorException("ffmpeg is not found: {$this->ffmpegBin}"); |
|
22 | + if (!file_exists($this->ffmpegBin)) { |
|
23 | + throw new ErrorException("ffmpeg is not found: {$this->ffmpegBin}"); |
|
24 | + } |
|
24 | 25 | |
25 | - if (!is_executable($this->ffmpegBin)) |
|
26 | - throw new ErrorException("ffmpeg is not executable: {$this->ffmpegBin}"); |
|
26 | + if (!is_executable($this->ffmpegBin)) { |
|
27 | + throw new ErrorException("ffmpeg is not executable: {$this->ffmpegBin}"); |
|
28 | + } |
|
27 | 29 | |
28 | - if (!is_file($videoSourceFilename)) |
|
29 | - throw new ErrorException('File not found on disk.'); |
|
30 | + if (!is_file($videoSourceFilename)) { |
|
31 | + throw new ErrorException('File not found on disk.'); |
|
32 | + } |
|
30 | 33 | |
31 | 34 | $this->videoSourceFilename = $videoSourceFilename; |
32 | 35 | $this->outputImageFilename = $outputImageFilename; |
@@ -39,8 +42,9 @@ discard block |
||
39 | 42 | { |
40 | 43 | $command = "{$this->ffmpegBin} -i {$this->videoSourceFilename} -ss 00:00:03.000 -vframes 1 {$this->outputImageFilename}"; |
41 | 44 | exec($command, $out, $result); |
42 | - if ($result !== 0) |
|
43 | - throw new ErrorException('FFmpeg frame extracting fail:' . print_r($out, true)); |
|
45 | + if ($result !== 0) { |
|
46 | + throw new ErrorException('FFmpeg frame extracting fail:' . print_r($out, true)); |
|
47 | + } |
|
44 | 48 | } |
45 | 49 | |
46 | 50 | } |
@@ -73,8 +73,9 @@ discard block |
||
73 | 73 | */ |
74 | 74 | private function checkFormToken() |
75 | 75 | { |
76 | - if (in_array($this->action->id, $this->actionsToCheck) && FileInputWidget::generateToken() != Yii::$app->request->post('_fileFormToken')) |
|
77 | - throw new BadRequestHttpException('File-form token is wrong or missing.'); |
|
76 | + if (in_array($this->action->id, $this->actionsToCheck) && FileInputWidget::generateToken() != Yii::$app->request->post('_fileFormToken')) { |
|
77 | + throw new BadRequestHttpException('File-form token is wrong or missing.'); |
|
78 | + } |
|
78 | 79 | } |
79 | 80 | |
80 | 81 | /** |
@@ -87,12 +88,14 @@ discard block |
||
87 | 88 | |
88 | 89 | $zip = new ZipArchive; |
89 | 90 | $filename = Yii::getAlias("@webroot/assets/files.zip"); |
90 | - if (file_exists($filename)) |
|
91 | - @unlink($filename); |
|
91 | + if (file_exists($filename)) { |
|
92 | + @unlink($filename); |
|
93 | + } |
|
92 | 94 | if (sizeof($files) && $zip->open($filename, ZipArchive::CREATE)) { |
93 | 95 | |
94 | - foreach ($files as $file) |
|
95 | - $zip->addFile($file->rootPath, $file->title); |
|
96 | + foreach ($files as $file) { |
|
97 | + $zip->addFile($file->rootPath, $file->title); |
|
98 | + } |
|
96 | 99 | |
97 | 100 | $zip->close(); |
98 | 101 | header("Pragma: public"); |
@@ -165,8 +168,9 @@ discard block |
||
165 | 168 | |
166 | 169 | $view = Yii::$app->request->post('mode') == 'single' ? "_single" : "_file"; |
167 | 170 | |
168 | - if ($ratio) |
|
169 | - $this->getView()->registerJs("initCropper({$model->id}, '{$model->href}', {$ratio}, true);"); |
|
171 | + if ($ratio) { |
|
172 | + $this->getView()->registerJs("initCropper({$model->id}, '{$model->href}', {$ratio}, true);"); |
|
173 | + } |
|
170 | 174 | |
171 | 175 | return $this->renderAjax($view, [ |
172 | 176 | 'model' => $model, |
@@ -192,8 +196,9 @@ discard block |
||
192 | 196 | { |
193 | 197 | $model = File::findOne(['hash' => $hash]); |
194 | 198 | |
195 | - if (!$model) |
|
196 | - throw new NotFoundHttpException("Запрашиваемый файл не найден в базе."); |
|
199 | + if (!$model) { |
|
200 | + throw new NotFoundHttpException("Запрашиваемый файл не найден в базе."); |
|
201 | + } |
|
197 | 202 | |
198 | 203 | $response = Yii::$app->response; |
199 | 204 | $response->format = Response::FORMAT_RAW; |
@@ -202,8 +207,9 @@ discard block |
||
202 | 207 | Yii::$app->response->headers->set('Last-Modified', date("c", $model->created)); |
203 | 208 | Yii::$app->response->headers->set('Cache-Control', 'public, max-age=' . (60 * 60 * 24 * 15)); |
204 | 209 | |
205 | - if (!file_exists($model->getRootPreviewPath())) |
|
206 | - throw new NotFoundHttpException('Preview not found.'); |
|
210 | + if (!file_exists($model->getRootPreviewPath())) { |
|
211 | + throw new NotFoundHttpException('Preview not found.'); |
|
212 | + } |
|
207 | 213 | |
208 | 214 | $response->sendFile($model->getRootPreviewPath(), 'preview.jpg'); |
209 | 215 |
@@ -28,11 +28,13 @@ |
||
28 | 28 | */ |
29 | 29 | public function run(): ?string |
30 | 30 | { |
31 | - if (empty($this->model) || !in_array($this->model->type, [FileType::IMAGE, FileType::VIDEO])) |
|
32 | - return null; |
|
31 | + if (empty($this->model) || !in_array($this->model->type, [FileType::IMAGE, FileType::VIDEO])) { |
|
32 | + return null; |
|
33 | + } |
|
33 | 34 | |
34 | - if (is_array($this->width)) |
|
35 | - $this->view = 'mediaPictureWidget'; |
|
35 | + if (is_array($this->width)) { |
|
36 | + $this->view = 'mediaPictureWidget'; |
|
37 | + } |
|
36 | 38 | |
37 | 39 | return $this->render($this->view, [ |
38 | 40 | 'model' => $this->model, |