1 | <?php |
||
2 | |||
3 | |||
4 | namespace carono\yii2file; |
||
5 | |||
6 | use yii\db\ActiveRecord; |
||
7 | use yii\helpers\FileHelper; |
||
8 | use carono\yii2file\Uploader; |
||
9 | |||
10 | /** |
||
11 | * Trait FileUploadTrait |
||
12 | * |
||
13 | * @package carono\yii2file |
||
14 | * @property integer $id |
||
15 | * @property string $uid |
||
16 | * @property integer $user_id |
||
17 | * @property string $name |
||
18 | * @property string $extension |
||
19 | * @property string $folder |
||
20 | * @property string $mime_type |
||
21 | * @property integer $size |
||
22 | * @property string $data |
||
23 | * @property string $session |
||
24 | * @property string $md5 |
||
25 | * @property string $slug |
||
26 | * @property integer $is_active |
||
27 | * @property integer $is_exist |
||
28 | * @property resource $binary |
||
29 | * @property string $created_at |
||
30 | * @property string $updated_at |
||
31 | * |
||
32 | * @property string $fileName |
||
33 | * @property string $realFileName |
||
34 | * @property string $realFilePath |
||
35 | * |
||
36 | * @mixin ActiveRecord |
||
37 | */ |
||
38 | trait FileUploadTrait |
||
39 | { |
||
40 | public $fileNameAsUid = true; |
||
41 | public $eraseOnDelete = true; |
||
42 | public $uploaderClass = Uploader::class; |
||
43 | public $fileUploadFolder = '@app/files'; |
||
44 | |||
45 | public function init() |
||
46 | { |
||
47 | $this->on(self::EVENT_BEFORE_DELETE, [$this, 'eraseOnDelete']); |
||
48 | parent::init(); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param $file |
||
53 | * @return Uploader|mixed |
||
54 | */ |
||
55 | public static function startUpload($file) |
||
56 | { |
||
57 | $model = new static(); |
||
58 | return \Yii::createObject([ |
||
59 | 'class' => $model->uploaderClass, |
||
60 | 'modelClass' => static::class, |
||
61 | 'file' => $file, |
||
62 | 'folder' => $model->fileUploadFolder, |
||
63 | ]); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function deleteFile() |
||
70 | { |
||
71 | if ($this->fileExist()) { |
||
72 | unlink($this->getRealFilePath()); |
||
73 | if ($f = !$this->fileExist()) { |
||
74 | $this->updateAttributes(['is_exist' => false]); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
75 | } |
||
76 | return $f; |
||
77 | } |
||
78 | |||
79 | return true; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getRealFileName() |
||
86 | { |
||
87 | if ($this->fileNameAsUid) { |
||
88 | return $this->uid . '.' . $this->extension; |
||
89 | } |
||
90 | |||
91 | return $this->name . '.' . $this->extension; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return mixed |
||
96 | */ |
||
97 | public function getRealFilePath() |
||
98 | { |
||
99 | $path = \Yii::getAlias($this->folder) . DIRECTORY_SEPARATOR . $this->getRealFileName(); |
||
100 | return str_replace('/', DIRECTORY_SEPARATOR, $path); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function fileExist() |
||
107 | { |
||
108 | return file_exists($this->getRealFilePath()); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return bool|null |
||
113 | */ |
||
114 | public function isImage() |
||
115 | { |
||
116 | if (($mime = $this->mime_type) || ($mime = FileHelper::getMimeType($this->getRealFilePath()))) { |
||
117 | return strpos($mime, 'image') === 0; |
||
118 | } |
||
119 | |||
120 | return null; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getFileName() |
||
127 | { |
||
128 | return implode('.', array_filter([$this->name, $this->extension])); |
||
129 | } |
||
130 | |||
131 | public function eraseOnDelete() |
||
132 | { |
||
133 | if ($this->eraseOnDelete && $this->fileExist()) { |
||
134 | $this->deleteFile(); |
||
135 | } |
||
136 | } |
||
137 | } |