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