-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.php
160 lines (138 loc) · 3.7 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace omnilight\files;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;
/**
* @property string $id
* @property string $original_name
* @property string $name
* @property int $file_size
* @property string $created_at
* @property string $updated_at
*
* Class File is the base class for all file uploads
*/
class File extends ActiveRecord
{
const SCENARIO_FILE_UPLOAD = 'fileUpload';
/**
* @var UploadedFile
*/
public $fileUpload;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%files}}';
}
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'value' => new Expression('NOW()'),
]
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
'fileRule' => [['fileUpload'], 'file', 'skipOnEmpty' => false, 'on' => self::SCENARIO_FILE_UPLOAD, 'when' => function () {
return $this->isNewRecord;
}],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('omnilight/files', 'ID'),
'original_name' => Yii::t('omnilight/files', 'Original Name'),
'name' => Yii::t('omnilight/files', 'Name'),
'file_size' => Yii::t('omnilight/files', 'File Size'),
'created_at' => Yii::t('omnilight/files', 'Created At'),
'updated_at' => Yii::t('omnilight/files', 'Updated At'),
'fileUpload' => Yii::t('omnilight/files', 'File'),
];
}
public function loadWithFile($data, $formName = null)
{
if ($this->load($data, $formName) == false) {
return false;
}
if (!($this->fileUpload instanceof UploadedFile)) {
$this->fileUpload = UploadedFile::getInstance($this, 'fileUpload');
}
return true;
}
public function afterSave($insert, $changedAttributes)
{
if ($this->fileUpload instanceof UploadedFile) {
$this->uploadFile($this->fileUpload);
}
parent::afterSave($insert, $changedAttributes);
}
public function uploadFile(UploadedFile $fileUpload)
{
if ($this->name) {
@unlink($this->getFileName());
}
$this->original_name = $fileUpload->name;
$this->file_size = filesize($fileUpload->tempName);
$this->name = $this->generateName();
$this->updateAttributes(['original_name', 'name', 'file_size']);
$fileName = $this->getFileName();
FileHelper::createDirectory(dirname($fileName));
$this->fileUpload->saveAs($fileName);
}
/**
* @return bool|string
*/
public function getFileName()
{
return Yii::getAlias('@webroot/uploads/' . $this->name);
}
/**
* @return string
*/
protected function generateName()
{
return $this->id . '.' . $this->getOriginalExtension();
}
/**
* @return string
*/
public function getOriginalExtension()
{
return strtolower(pathinfo($this->original_name, PATHINFO_EXTENSION));
}
public function afterDelete()
{
@unlink($this->getFileName());
parent::afterDelete();
}
/**
* @return bool|string
*/
public function getUrl()
{
return Yii::getAlias('@web/uploads/' . $this->name);
}
/**
* @return string
*/
public function getFileExtension()
{
return strtolower(pathinfo($this->name, PATHINFO_EXTENSION));
}
}