bluzphp /
module-media
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @copyright Bluz PHP Team |
||
| 4 | * @link https://github.com/bluzphp/skeleton |
||
| 5 | */ |
||
| 6 | |||
| 7 | declare(strict_types=1); |
||
| 8 | |||
| 9 | namespace Application\Media; |
||
| 10 | |||
| 11 | use Application\Users; |
||
|
0 ignored issues
–
show
|
|||
| 12 | use Bluz\Config\ConfigException; |
||
| 13 | use Bluz\Http\Exception\BadRequestException; |
||
| 14 | use Bluz\Proxy\Auth; |
||
| 15 | use Bluz\Proxy\Config; |
||
| 16 | use Image\Thumbnail; |
||
|
0 ignored issues
–
show
The type
Image\Thumbnail was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 17 | use Zend\Diactoros\UploadedFile; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Manager |
||
| 21 | * |
||
| 22 | * @category Application |
||
| 23 | * @package Media |
||
| 24 | * @author Anton Shevchuk |
||
| 25 | */ |
||
| 26 | class Manager |
||
| 27 | { |
||
| 28 | public const THUMB_HEIGHT = 196; |
||
| 29 | public const THUMB_WIDTH = 196; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Row |
||
| 33 | */ |
||
| 34 | protected $media; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var UploadedFile |
||
| 38 | */ |
||
| 39 | protected $file; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $name; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $publicPath; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $uploadPath; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param Row $media |
||
| 58 | * @param UploadedFile $file |
||
| 59 | * |
||
| 60 | * @throws BadRequestException |
||
| 61 | */ |
||
| 62 | public function __construct($media, $file) |
||
| 63 | { |
||
| 64 | $this->media = $media; |
||
| 65 | $this->media->module = $this->media->module ?: 'users'; |
||
| 66 | $this->media->userId = $this->media->userId ?: Auth::getIdentity()->getId() ?: Users\Table::SYSTEM_USER; |
||
|
0 ignored issues
–
show
The type
Application\Users\Table was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 67 | |||
| 68 | $this->file = $file; |
||
| 69 | $this->name = $media->title ?? pathinfo($file->getClientFilename(), PATHINFO_FILENAME); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Move file to directory |
||
| 74 | * |
||
| 75 | * @param string $directory |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | * @throws ConfigException |
||
| 79 | */ |
||
| 80 | public function moveToDir($directory) |
||
| 81 | { |
||
| 82 | $uploadPath = Config::get('module.media', 'upload_path'); |
||
| 83 | |||
| 84 | if (empty($uploadPath)) { |
||
| 85 | throw new ConfigException('Upload path is not configured'); |
||
| 86 | } |
||
| 87 | |||
| 88 | $fullPath = PATH_PUBLIC.'/'.$uploadPath.'/'.$directory; |
||
|
0 ignored issues
–
show
|
|||
| 89 | |||
| 90 | if (!@mkdir($fullPath, 0755, true) && !is_dir($fullPath)) { |
||
| 91 | throw new ConfigException('Upload folder is not exists, please create it'); |
||
| 92 | } |
||
| 93 | |||
| 94 | if (!is_writable($fullPath)) { |
||
| 95 | throw new ConfigException('Upload folder is not writable'); |
||
| 96 | } |
||
| 97 | |||
| 98 | $fileName = $this->getFileName($fullPath); |
||
| 99 | |||
| 100 | $this->publicPath = $uploadPath.'/'.$directory.'/'.$fileName; |
||
| 101 | $this->uploadPath = $fullPath.'/'.$fileName; |
||
| 102 | |||
| 103 | $this->file->moveTo($this->uploadPath); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Create thumbnail |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | * @throws \Image\Exception |
||
| 111 | * @throws \ImagickException |
||
| 112 | */ |
||
| 113 | public function createThumbnail() |
||
| 114 | { |
||
| 115 | // set full path |
||
| 116 | $image = new Thumbnail($this->getUploadPath()); |
||
| 117 | $image->setHeight(self::THUMB_HEIGHT); |
||
| 118 | $image->setWidth(self::THUMB_WIDTH); |
||
| 119 | $thumb = $image->generate(); |
||
| 120 | // crop full path |
||
| 121 | $thumb = substr($thumb, strlen(PATH_PUBLIC) + 1); |
||
|
0 ignored issues
–
show
|
|||
| 122 | return $thumb; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Prepare File name for path |
||
| 127 | * |
||
| 128 | * @param string $path |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | protected function getFileName($path) : string |
||
| 133 | { |
||
| 134 | /** |
||
| 135 | * Generate image name |
||
| 136 | */ |
||
| 137 | $pathInfo = pathinfo($this->file->getClientFilename()); |
||
| 138 | |||
| 139 | $fileName = strtolower($this->name ?? $pathInfo['filename']); |
||
| 140 | $fileExt = strtolower($pathInfo['extension']); |
||
| 141 | |||
| 142 | // Prepare filename |
||
| 143 | $fileName = preg_replace('/[ _;:]+/', '-', $fileName); |
||
| 144 | $fileName = preg_replace('/[^a-z0-9.-]+/i', '', $fileName); |
||
| 145 | |||
| 146 | // If name is empty, generate it with current time |
||
| 147 | if (empty($fileName)) { |
||
| 148 | $fileName = date('Y-m-d-His'); |
||
| 149 | } |
||
| 150 | |||
| 151 | // If file already exists, increment name |
||
| 152 | $originFileName = $fileName; |
||
| 153 | $counter = 0; |
||
| 154 | while (file_exists($path .'/'.$fileName.'.'.$fileExt)) { |
||
| 155 | $counter++; |
||
| 156 | $fileName = $originFileName.'-'.$counter; |
||
| 157 | } |
||
| 158 | return $fileName.'.'.$fileExt; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function getPublicPath(): string |
||
| 165 | { |
||
| 166 | return $this->publicPath; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getUploadPath(): string |
||
| 173 | { |
||
| 174 | return $this->uploadPath; |
||
| 175 | } |
||
| 176 | } |
||
| 177 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths