@@ -31,8 +31,9 @@ discard block |
||
| 31 | 31 | $this->webp = $webp; |
| 32 | 32 | $this->quality = $quality; |
| 33 | 33 | |
| 34 | - if (!$this->model->isImage() && !$this->model->isVideo()) |
|
| 35 | - throw new ErrorException('File is not an image or video.'); |
|
| 34 | + if (!$this->model->isImage() && !$this->model->isVideo()) { |
|
| 35 | + throw new ErrorException('File is not an image or video.'); |
|
| 36 | + } |
|
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | /** |
@@ -42,8 +43,9 @@ discard block |
||
| 42 | 43 | */ |
| 43 | 44 | public function getUrl() |
| 44 | 45 | { |
| 45 | - if ($this->model->isSvg()) |
|
| 46 | - return $this->model->getRootPath(); |
|
| 46 | + if ($this->model->isSvg()) { |
|
| 47 | + return $this->model->getRootPath(); |
|
| 48 | + } |
|
| 47 | 49 | |
| 48 | 50 | $cachePath = Yii::$app->getModule('files')->cacheFullPath; |
| 49 | 51 | $jpegName = $this->model->makeNameWithSize($this->model->filename, $this->width, false, $this->quality); |
@@ -57,21 +59,25 @@ discard block |
||
| 57 | 59 | $sourceImagePath = $this->model->rootPath; |
| 58 | 60 | if ($this->model->isVideo()) { |
| 59 | 61 | $sourceImagePath = $this->fileName . '.jpeg'; |
| 60 | - if (!is_file($sourceImagePath)) |
|
| 61 | - Yii::createObject(VideoFrameExtractor::class, [ |
|
| 62 | + if (!is_file($sourceImagePath)) { |
|
| 63 | + Yii::createObject(VideoFrameExtractor::class, [ |
|
| 62 | 64 | $this->model->rootPath, |
| 63 | 65 | $sourceImagePath |
| 64 | 66 | ])->extract(); |
| 67 | + } |
|
| 65 | 68 | } |
| 66 | 69 | |
| 67 | - if (!is_file($this->fileName) || filesize($this->fileName) == 0) |
|
| 68 | - $this->createPreview($sourceImagePath); |
|
| 70 | + if (!is_file($this->fileName) || filesize($this->fileName) == 0) { |
|
| 71 | + $this->createPreview($sourceImagePath); |
|
| 72 | + } |
|
| 69 | 73 | |
| 70 | - if ($this->webp && !file_exists($this->fileNameWebp)) |
|
| 71 | - $this->createPreviewWebp(); |
|
| 74 | + if ($this->webp && !file_exists($this->fileNameWebp)) { |
|
| 75 | + $this->createPreviewWebp(); |
|
| 76 | + } |
|
| 72 | 77 | |
| 73 | - if ($this->webp) |
|
| 74 | - return $this->fileNameWebp; |
|
| 78 | + if ($this->webp) { |
|
| 79 | + return $this->fileNameWebp; |
|
| 80 | + } |
|
| 75 | 81 | |
| 76 | 82 | return $this->fileName; |
| 77 | 83 | } |
@@ -81,19 +87,23 @@ discard block |
||
| 81 | 87 | */ |
| 82 | 88 | protected function prepareFolder() |
| 83 | 89 | { |
| 84 | - if (!file_exists(Yii::$app->getModule('files')->cacheFullPath)) |
|
| 85 | - mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
| 90 | + if (!file_exists(Yii::$app->getModule('files')->cacheFullPath)) { |
|
| 91 | + mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
| 92 | + } |
|
| 86 | 93 | $lastFolder = '/'; |
| 87 | 94 | $explodes = explode('/', $this->fileName); |
| 88 | 95 | array_pop($explodes); |
| 89 | - if (empty($explodes)) |
|
| 90 | - return; |
|
| 96 | + if (empty($explodes)) { |
|
| 97 | + return; |
|
| 98 | + } |
|
| 91 | 99 | foreach ($explodes as $folder) { |
| 92 | - if (empty($folder)) |
|
| 93 | - continue; |
|
| 100 | + if (empty($folder)) { |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 94 | 103 | $lastFolder = $lastFolder . $folder . '/'; |
| 95 | - if (!file_exists($lastFolder)) |
|
| 96 | - mkdir($lastFolder); |
|
| 104 | + if (!file_exists($lastFolder)) { |
|
| 105 | + mkdir($lastFolder); |
|
| 106 | + } |
|
| 97 | 107 | } |
| 98 | 108 | } |
| 99 | 109 | |
@@ -116,8 +126,9 @@ discard block |
||
| 116 | 126 | } |
| 117 | 127 | |
| 118 | 128 | $saveType = $img->image_type; |
| 119 | - if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) |
|
| 120 | - $saveType = IMG_JPEG; |
|
| 129 | + if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) { |
|
| 130 | + $saveType = IMG_JPEG; |
|
| 131 | + } |
|
| 121 | 132 | $img->save($this->fileName, $saveType, $this->quality); |
| 122 | 133 | } |
| 123 | 134 | |
@@ -54,14 +54,17 @@ discard block |
||
| 54 | 54 | private function loadAndCheckModel(string $hash): void |
| 55 | 55 | { |
| 56 | 56 | $this->model = File::findOne(['hash' => $hash]); |
| 57 | - if (!$this->model) |
|
| 58 | - throw new NotFoundHttpException("Запрашиваемый файл не найден"); |
|
| 57 | + if (!$this->model) { |
|
| 58 | + throw new NotFoundHttpException("Запрашиваемый файл не найден"); |
|
| 59 | + } |
|
| 59 | 60 | |
| 60 | - if (!$this->model->isImage() && !$this->model->isVideo()) |
|
| 61 | - throw new NotFoundHttpException("Запрашиваемый файл не является изображением"); |
|
| 61 | + if (!$this->model->isImage() && !$this->model->isVideo()) { |
|
| 62 | + throw new NotFoundHttpException("Запрашиваемый файл не является изображением"); |
|
| 63 | + } |
|
| 62 | 64 | |
| 63 | - if (!file_exists($this->model->rootPath)) |
|
| 64 | - throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
| 65 | + if (!file_exists($this->model->rootPath)) { |
|
| 66 | + throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
| 67 | + } |
|
| 65 | 68 | } |
| 66 | 69 | |
| 67 | 70 | /** |
@@ -73,8 +76,9 @@ discard block |
||
| 73 | 76 | protected function sendPreview($width, $webp, $quality) |
| 74 | 77 | { |
| 75 | 78 | $filename = Yii::createObject(ImagePreviewer::class, [$this->model, $width, $webp, $quality])->getUrl(); |
| 76 | - if (!file_exists($filename)) |
|
| 77 | - throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
| 79 | + if (!file_exists($filename)) { |
|
| 80 | + throw new NotFoundHttpException('Запрашиваемый файл не найден на диске.'); |
|
| 81 | + } |
|
| 78 | 82 | |
| 79 | 83 | $response = Yii::$app->response; |
| 80 | 84 | $response->format = Response::FORMAT_RAW; |
@@ -87,29 +87,37 @@ discard block |
||
| 87 | 87 | { |
| 88 | 88 | $icon = IconHelper::FILE; |
| 89 | 89 | |
| 90 | - if ($this->content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') |
|
| 91 | - $icon = IconHelper::FILE_WORD; |
|
| 90 | + if ($this->content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') { |
|
| 91 | + $icon = IconHelper::FILE_WORD; |
|
| 92 | + } |
|
| 92 | 93 | |
| 93 | - if ($this->content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') |
|
| 94 | - $icon = IconHelper::FILE_EXCEL; |
|
| 94 | + if ($this->content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') { |
|
| 95 | + $icon = IconHelper::FILE_EXCEL; |
|
| 96 | + } |
|
| 95 | 97 | |
| 96 | - if ($this->content_type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation') |
|
| 97 | - $icon = IconHelper::FILE_POWERPOINT; |
|
| 98 | + if ($this->content_type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation') { |
|
| 99 | + $icon = IconHelper::FILE_POWERPOINT; |
|
| 100 | + } |
|
| 98 | 101 | |
| 99 | - if ($this->content_type == 'application/x-zip-compressed') |
|
| 100 | - $icon = IconHelper::FILE_ARCHIVE; |
|
| 102 | + if ($this->content_type == 'application/x-zip-compressed') { |
|
| 103 | + $icon = IconHelper::FILE_ARCHIVE; |
|
| 104 | + } |
|
| 101 | 105 | |
| 102 | - if ($this->content_type == 'application/octet-stream') |
|
| 103 | - $icon = IconHelper::FILE_ARCHIVE; |
|
| 106 | + if ($this->content_type == 'application/octet-stream') { |
|
| 107 | + $icon = IconHelper::FILE_ARCHIVE; |
|
| 108 | + } |
|
| 104 | 109 | |
| 105 | - if (preg_match('/audio/', $this->content_type)) |
|
| 106 | - $icon = IconHelper::FILE_AUDIO; |
|
| 110 | + if (preg_match('/audio/', $this->content_type)) { |
|
| 111 | + $icon = IconHelper::FILE_AUDIO; |
|
| 112 | + } |
|
| 107 | 113 | |
| 108 | - if (preg_match('/pdf/', $this->content_type)) |
|
| 109 | - $icon = IconHelper::FILE_PDF; |
|
| 114 | + if (preg_match('/pdf/', $this->content_type)) { |
|
| 115 | + $icon = IconHelper::FILE_PDF; |
|
| 116 | + } |
|
| 110 | 117 | |
| 111 | - if ($this->type == FileType::VIDEO) |
|
| 112 | - $icon = IconHelper::FILE_VIDEO; |
|
| 118 | + if ($this->type == FileType::VIDEO) { |
|
| 119 | + $icon = IconHelper::FILE_VIDEO; |
|
| 120 | + } |
|
| 113 | 121 | |
| 114 | 122 | return $icon; |
| 115 | 123 | } |
@@ -225,8 +233,9 @@ discard block |
||
| 225 | 233 | |
| 226 | 234 | public function getRootPreviewPath() |
| 227 | 235 | { |
| 228 | - if ($this->isSvg()) |
|
| 229 | - return $this->getRootPath(); |
|
| 236 | + if ($this->isSvg()) { |
|
| 237 | + return $this->getRootPath(); |
|
| 238 | + } |
|
| 230 | 239 | |
| 231 | 240 | return Yii::$app->getModule('files')->storageFullPath . $this->filename . '.jpg'; |
| 232 | 241 | } |
@@ -340,8 +349,9 @@ discard block |
||
| 340 | 349 | isset($this->behaviors['files']) && |
| 341 | 350 | isset($this->behaviors['files']->attributes[$this->field]) && |
| 342 | 351 | isset($this->behaviors['files']->attributes[$this->field]['watermark']) |
| 343 | - ) |
|
| 344 | - return $this->behaviors['files']->attributes[$this->field]['watermark']; |
|
| 352 | + ) { |
|
| 353 | + return $this->behaviors['files']->attributes[$this->field]['watermark']; |
|
| 354 | + } |
|
| 345 | 355 | } |
| 346 | 356 | |
| 347 | 357 | /** |
@@ -361,17 +371,20 @@ discard block |
||
| 361 | 371 | */ |
| 362 | 372 | public function getPreviewWebPath(int $width = 0, bool $webp = false, $quality = 75) |
| 363 | 373 | { |
| 364 | - if (!file_exists($this->getRootPath())) |
|
| 365 | - return null; |
|
| 374 | + if (!file_exists($this->getRootPath())) { |
|
| 375 | + return null; |
|
| 376 | + } |
|
| 366 | 377 | |
| 367 | - if (!$this->isVideo() && !$this->isImage()) |
|
| 368 | - throw new ErrorException('Requiested file is not an image and its implsible to resize it.'); |
|
| 378 | + if (!$this->isVideo() && !$this->isImage()) { |
|
| 379 | + throw new ErrorException('Requiested file is not an image and its implsible to resize it.'); |
|
| 380 | + } |
|
| 369 | 381 | |
| 370 | - if (Yii::$app->getModule('files')->hostStatic) |
|
| 371 | - return |
|
| 382 | + if (Yii::$app->getModule('files')->hostStatic) { |
|
| 383 | + return |
|
| 372 | 384 | Yii::$app->getModule('files')->hostStatic . |
| 373 | 385 | $this->makeNameWithSize($this->filename, $width, $webp, $quality) . |
| 374 | 386 | "?hash={$this->hash}&width={$width}&webp=" . intval($webp) . '&quality=' . $quality; |
| 387 | + } |
|
| 375 | 388 | |
| 376 | 389 | return Url::toRoute(['/files/default/image', 'hash' => $this->hash, 'width' => $width, 'webp' => $webp, 'quality' => $quality]); |
| 377 | 390 | } |
@@ -407,8 +420,9 @@ discard block |
||
| 407 | 420 | */ |
| 408 | 421 | public function getPreviewRootPath($width = 0, $webp = false) |
| 409 | 422 | { |
| 410 | - if (!$this->isVideo() && !$this->isImage()) |
|
| 411 | - throw new ErrorException('Requiested file is not an image and its implsible to resize it.'); |
|
| 423 | + if (!$this->isVideo() && !$this->isImage()) { |
|
| 424 | + throw new ErrorException('Requiested file is not an image and its implsible to resize it.'); |
|
| 425 | + } |
|
| 412 | 426 | return $this->makeNameWithSize($this->rootPath, $width, $webp); |
| 413 | 427 | } |
| 414 | 428 | |