carono /
yii2-file-upload
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace carono\yii2file; |
||||||
| 4 | |||||||
| 5 | use Yii; |
||||||
| 6 | use yii\base\Component; |
||||||
| 7 | use yii\base\Security; |
||||||
| 8 | use yii\db\ActiveRecord; |
||||||
| 9 | use yii\db\Expression; |
||||||
| 10 | use yii\helpers\ArrayHelper; |
||||||
| 11 | use yii\helpers\FileHelper; |
||||||
| 12 | use yii\web\UploadedFile; |
||||||
| 13 | |||||||
| 14 | /** |
||||||
| 15 | * Class FileUpload |
||||||
| 16 | * |
||||||
| 17 | * @package carono\components |
||||||
| 18 | * @property string $fullName |
||||||
| 19 | */ |
||||||
| 20 | class Uploader extends Component |
||||||
| 21 | { |
||||||
| 22 | /** |
||||||
| 23 | * @var FileUploadTrait |
||||||
| 24 | */ |
||||||
| 25 | public $modelClass; |
||||||
| 26 | public $data; |
||||||
| 27 | public $file; |
||||||
| 28 | public $name; |
||||||
| 29 | public $slug; |
||||||
| 30 | public $uid; |
||||||
| 31 | public $delete = true; |
||||||
| 32 | public $folder; |
||||||
| 33 | |||||||
| 34 | protected $fileName; |
||||||
| 35 | protected $filePath; |
||||||
| 36 | |||||||
| 37 | /** |
||||||
| 38 | * @param $name |
||||||
| 39 | * @return $this |
||||||
| 40 | */ |
||||||
| 41 | public function name($name) |
||||||
| 42 | { |
||||||
| 43 | $this->name = $name; |
||||||
| 44 | return $this; |
||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | /** |
||||||
| 48 | * @param $data |
||||||
| 49 | * @return $this |
||||||
| 50 | */ |
||||||
| 51 | public function data($data) |
||||||
| 52 | { |
||||||
| 53 | $this->data = $data; |
||||||
| 54 | return $this; |
||||||
| 55 | } |
||||||
| 56 | |||||||
| 57 | /** |
||||||
| 58 | * @param $path |
||||||
| 59 | * @return $this |
||||||
| 60 | */ |
||||||
| 61 | public function folder($path) |
||||||
| 62 | { |
||||||
| 63 | $this->folder = $path; |
||||||
| 64 | return $this; |
||||||
| 65 | } |
||||||
| 66 | |||||||
| 67 | /** |
||||||
| 68 | * @param bool $deleteOnFinish |
||||||
| 69 | * @return $this |
||||||
| 70 | */ |
||||||
| 71 | public function delete($deleteOnFinish = true) |
||||||
| 72 | { |
||||||
| 73 | $this->delete = $deleteOnFinish; |
||||||
| 74 | return $this; |
||||||
| 75 | } |
||||||
| 76 | |||||||
| 77 | /** |
||||||
| 78 | * @return null|string |
||||||
| 79 | */ |
||||||
| 80 | protected function getSession() |
||||||
| 81 | { |
||||||
| 82 | if (isset(Yii::$app->session)) { |
||||||
| 83 | $session = Yii::$app->session->getIsActive() ? Yii::$app->session->getId() : null; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 84 | } else { |
||||||
| 85 | $session = null; |
||||||
| 86 | } |
||||||
| 87 | return $session; |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | /** |
||||||
| 91 | * @return mixed |
||||||
| 92 | */ |
||||||
| 93 | public function getFileName() |
||||||
| 94 | { |
||||||
| 95 | return $this->name ?: $this->fileName; |
||||||
| 96 | } |
||||||
| 97 | |||||||
| 98 | /** |
||||||
| 99 | * @return string |
||||||
| 100 | */ |
||||||
| 101 | protected function getFileNameWithOutExtension() |
||||||
| 102 | { |
||||||
| 103 | return basename($this->getFileName(), '.' . $this->getFileExtension()); |
||||||
| 104 | } |
||||||
| 105 | |||||||
| 106 | /** |
||||||
| 107 | * @return string |
||||||
| 108 | */ |
||||||
| 109 | protected function getMimeType() |
||||||
| 110 | { |
||||||
| 111 | return FileHelper::getMimeType($this->filePath); |
||||||
| 112 | } |
||||||
| 113 | |||||||
| 114 | /** |
||||||
| 115 | * @return mixed|string |
||||||
| 116 | */ |
||||||
| 117 | protected function getFileExtension() |
||||||
| 118 | { |
||||||
| 119 | if (!$extension = strtolower(pathinfo($this->fileName, PATHINFO_EXTENSION))) { |
||||||
| 120 | $extension = ArrayHelper::getValue(FileHelper::getExtensionsByMimeType($this->getMimeType()), 0); |
||||||
| 121 | } |
||||||
| 122 | return $extension; |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | /** |
||||||
| 126 | * @return int|null|string |
||||||
| 127 | */ |
||||||
| 128 | public function getUserId() |
||||||
| 129 | { |
||||||
| 130 | if (isset(Yii::$app->user)) { |
||||||
| 131 | $userId = Yii::$app->user->getId(); |
||||||
|
0 ignored issues
–
show
The method
getId() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 132 | } else { |
||||||
| 133 | $userId = null; |
||||||
| 134 | } |
||||||
| 135 | return $userId; |
||||||
| 136 | } |
||||||
| 137 | |||||||
| 138 | /** |
||||||
| 139 | * @param $source |
||||||
| 140 | * @param $destination |
||||||
| 141 | * @throws \Exception |
||||||
| 142 | */ |
||||||
| 143 | protected function copyUploadedFile($source, $destination) |
||||||
| 144 | { |
||||||
| 145 | if (!move_uploaded_file($source, $destination)) { |
||||||
| 146 | throw new \Exception('Unknown upload error'); |
||||||
| 147 | } |
||||||
| 148 | } |
||||||
| 149 | |||||||
| 150 | /** |
||||||
| 151 | * @param $source |
||||||
| 152 | * @param $destination |
||||||
| 153 | * @throws \Exception |
||||||
| 154 | */ |
||||||
| 155 | protected function renameFile($source, $destination) |
||||||
| 156 | { |
||||||
| 157 | if (!rename($source, $destination)) { |
||||||
| 158 | throw new \Exception('Failed rename file'); |
||||||
| 159 | } |
||||||
| 160 | } |
||||||
| 161 | |||||||
| 162 | /** |
||||||
| 163 | * @param $source |
||||||
| 164 | * @param $destination |
||||||
| 165 | * @throws \Exception |
||||||
| 166 | */ |
||||||
| 167 | protected function copyFile($source, $destination) |
||||||
| 168 | { |
||||||
| 169 | if (!copy($source, $destination)) { |
||||||
| 170 | throw new \Exception('Failed copy file'); |
||||||
| 171 | } |
||||||
| 172 | } |
||||||
| 173 | |||||||
| 174 | /** |
||||||
| 175 | * @param $source |
||||||
| 176 | * @param $destination |
||||||
| 177 | * @throws \Exception |
||||||
| 178 | */ |
||||||
| 179 | public function copy($source, $destination) |
||||||
| 180 | { |
||||||
| 181 | if (is_uploaded_file($source)) { |
||||||
| 182 | $this->copyUploadedFile($source, $destination); |
||||||
| 183 | } elseif ($this->delete) { |
||||||
| 184 | $this->renameFile($source, $destination); |
||||||
| 185 | } else { |
||||||
| 186 | $this->copyFile($source, $destination); |
||||||
| 187 | } |
||||||
| 188 | } |
||||||
| 189 | |||||||
| 190 | /** |
||||||
| 191 | * @return string |
||||||
| 192 | */ |
||||||
| 193 | public function getUid() |
||||||
| 194 | { |
||||||
| 195 | return $this->uid = $this->uid ?: $this->formUid(); |
||||||
| 196 | } |
||||||
| 197 | |||||||
| 198 | /** |
||||||
| 199 | * @return string |
||||||
| 200 | */ |
||||||
| 201 | public function getNewFileAlias() |
||||||
| 202 | { |
||||||
| 203 | return static::formAliasPath($this->getUid(), $this->folder); |
||||||
| 204 | } |
||||||
| 205 | |||||||
| 206 | /** |
||||||
| 207 | * @param ActiveRecord $model |
||||||
| 208 | * @param $attributes |
||||||
| 209 | */ |
||||||
| 210 | public function loadAttributes($model, $attributes) |
||||||
| 211 | { |
||||||
| 212 | $model->setAttributes($attributes); |
||||||
| 213 | } |
||||||
| 214 | |||||||
| 215 | public function formAttributes() |
||||||
| 216 | { |
||||||
| 217 | $this->processFilePath($this->file); |
||||||
| 218 | return [ |
||||||
| 219 | 'session' => $this->getSession(), |
||||||
| 220 | 'name' => $this->getFileNameWithOutExtension(), |
||||||
| 221 | 'extension' => $this->getFileExtension(), |
||||||
| 222 | 'user_id' => $this->getUserId(), |
||||||
| 223 | 'uid' => $this->getUid(), |
||||||
| 224 | 'data' => !is_null($this->data) ? json_encode($this->data) : null, |
||||||
| 225 | 'mime_type' => $this->getMimeType(), |
||||||
| 226 | 'md5' => md5_file($this->filePath), |
||||||
| 227 | 'folder' => static::formAliasPath($this->getUid(), $this->folder), |
||||||
| 228 | 'slug' => $this->slug, |
||||||
| 229 | 'size' => filesize($this->filePath) |
||||||
| 230 | ]; |
||||||
| 231 | } |
||||||
| 232 | |||||||
| 233 | /** |
||||||
| 234 | * @return FileUploadTrait|null |
||||||
| 235 | * @throws \Exception |
||||||
| 236 | */ |
||||||
| 237 | public function process() |
||||||
| 238 | { |
||||||
| 239 | /** |
||||||
| 240 | * @var FileUploadTrait $model |
||||||
| 241 | */ |
||||||
| 242 | $model = new $this->modelClass(); |
||||||
| 243 | $this->loadAttributes($model, $this->formAttributes()); |
||||||
| 244 | $newFilePath = $model->getRealFilePath(); |
||||||
| 245 | if (!is_dir($realFolder = dirname($newFilePath))) { |
||||||
| 246 | mkdir($realFolder, 0777, true); |
||||||
| 247 | } |
||||||
| 248 | if (!file_exists($this->filePath)) { |
||||||
| 249 | throw new \Exception('File not loaded or not exist'); |
||||||
| 250 | } |
||||||
| 251 | $this->copy($this->filePath, $newFilePath); |
||||||
| 252 | if ($model->save()) { |
||||||
| 253 | return $model; |
||||||
| 254 | } else { |
||||||
| 255 | $model->deleteFile(); |
||||||
| 256 | return null; |
||||||
| 257 | } |
||||||
| 258 | } |
||||||
| 259 | |||||||
| 260 | /** |
||||||
| 261 | * @param $file |
||||||
| 262 | */ |
||||||
| 263 | protected function processFilePath($file) |
||||||
| 264 | { |
||||||
| 265 | if (strpos($file, 'http') === 0) { |
||||||
| 266 | $tmp = Yii::getAlias('@runtime') . DIRECTORY_SEPARATOR . uniqid("fu"); |
||||||
| 267 | file_put_contents($tmp, file_get_contents($file)); |
||||||
| 268 | $this->filePath = $tmp; |
||||||
| 269 | $this->fileName = explode('?', basename($file))[0]; |
||||||
| 270 | } elseif (is_string($file)) { |
||||||
| 271 | $this->filePath = Yii::getAlias($file); |
||||||
| 272 | $this->fileName = basename($this->filePath); |
||||||
| 273 | } elseif ($file instanceof UploadedFile) { |
||||||
| 274 | $this->filePath = $file->tempName; |
||||||
| 275 | $this->fileName = $file->name; |
||||||
| 276 | } |
||||||
| 277 | } |
||||||
| 278 | |||||||
| 279 | /** |
||||||
| 280 | * @param $slug |
||||||
| 281 | * @return $this |
||||||
| 282 | */ |
||||||
| 283 | public function slug($slug) |
||||||
| 284 | { |
||||||
| 285 | $this->slug = $slug; |
||||||
| 286 | return $this; |
||||||
| 287 | } |
||||||
| 288 | |||||||
| 289 | /** |
||||||
| 290 | * @return string |
||||||
| 291 | */ |
||||||
| 292 | protected function generateUid() |
||||||
| 293 | { |
||||||
| 294 | $sec = new Security(); |
||||||
| 295 | return md5($sec->generateRandomString(64)); |
||||||
| 296 | } |
||||||
| 297 | |||||||
| 298 | /** |
||||||
| 299 | * @return string |
||||||
| 300 | */ |
||||||
| 301 | protected function formUid() |
||||||
| 302 | { |
||||||
| 303 | $class = $this->modelClass; |
||||||
| 304 | do { |
||||||
| 305 | $uPath = $this->generateUid(); |
||||||
| 306 | } while ($class::find()->where(["uid" => $uPath])->exists()); |
||||||
| 307 | return $uPath; |
||||||
| 308 | } |
||||||
| 309 | |||||||
| 310 | /** |
||||||
| 311 | * @param $path |
||||||
| 312 | * @param null|string $aliasPrefix |
||||||
| 313 | * @return string |
||||||
| 314 | */ |
||||||
| 315 | public static function formAliasPath($path, $aliasPrefix = null) |
||||||
| 316 | { |
||||||
| 317 | $p = $aliasPrefix ? [$aliasPrefix] : []; |
||||||
| 318 | for ($i = 0; $i < 3; $i++) { |
||||||
| 319 | $p[] = $path[$i]; |
||||||
| 320 | } |
||||||
| 321 | return join('/', $p); |
||||||
| 322 | } |
||||||
| 323 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.