@@ -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; |
@@ -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 | |
@@ -25,10 +25,10 @@ |
||
| 25 | 25 | $this->image = imagecreatefromjpeg($filename); |
| 26 | 26 | } elseif ($this->image_type == IMAGETYPE_GIF) { |
| 27 | 27 | $this->image = imagecreatefromgif($filename); |
| 28 | - imageSaveAlpha($this->image,true); |
|
| 28 | + imageSaveAlpha($this->image, true); |
|
| 29 | 29 | } elseif ($this->image_type == IMAGETYPE_PNG) { |
| 30 | 30 | $this->image = imagecreatefrompng($filename); |
| 31 | - imageSaveAlpha($this->image,true); |
|
| 31 | + imageSaveAlpha($this->image, true); |
|
| 32 | 32 | } elseif ($this->image_type == IMAGETYPE_WEBP) { |
| 33 | 33 | $this->image = imagecreatefromwebp($filename); |
| 34 | 34 | } |
@@ -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) |
| 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) . |
| 374 | 386 | "?hash={$this->hash}&width={$width}&webp=" . intval($webp); |
| 387 | + } |
|
| 375 | 388 | |
| 376 | 389 | return Url::toRoute(['/files/default/image', 'hash' => $this->hash, 'width' => $width, 'webp' => $webp]); |
| 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 | |