-
Notifications
You must be signed in to change notification settings - Fork 34
/
FileModel.php
177 lines (165 loc) · 5.12 KB
/
FileModel.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace mdm\upload;
use Yii;
use yii\web\UploadedFile;
use yii\helpers\FileHelper;
use yii\imagine\Image;
/**
* This is the model class for table "uploaded_file".
*
* @property integer $id
* @property string $name
* @property string $filename
* @property integer $size
* @property string $type
*/
class FileModel extends \yii\db\ActiveRecord
{
/**
* @var string
*/
public static $defaultUploadPath = '@runtime/upload';
/**
* @var integer
*/
public static $defaultDirectoryLevel = 1;
/**
* @var UploadedFile
*/
public $file;
/**
* @var string Upload path
*/
public $uploadPath;
/**
* @var integer the level of sub-directories to store uploaded files. Defaults to 1.
* If the system has huge number of uploaded files (e.g. one million), you may use a bigger value
* (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
* is not over burdened with a single directory having too many files.
*/
public $directoryLevel;
/**
* @var \Closure
*/
public $saveCallback;
public $cropParam;
public $resizeParam;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%uploaded_file}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['file'], 'required'],
[['file'], 'file', 'skipOnEmpty' => false],
[['uploadPath'], 'default', 'value' => static::$defaultUploadPath],
[['name', 'size'], 'default', 'value' => function($obj, $attribute) {
return $obj->file->$attribute;
}],
[['type'], 'default', 'value' => function() {
return FileHelper::getMimeType($this->file->tempName);
}],
[['filename'], 'default', 'value' => function() {
$level = $this->directoryLevel === null ? static::$defaultDirectoryLevel : $this->directoryLevel;
$key = md5(microtime() . $this->file->name);
$base = Yii::getAlias($this->uploadPath);
if ($level > 0) {
for ($i = 0; $i < $level; ++$i) {
if (($prefix = substr($key, 0, 2)) !== false) {
$base .= DIRECTORY_SEPARATOR . $prefix;
$key = substr($key, 2);
}
}
}
return $base . DIRECTORY_SEPARATOR . "{$key}_{$this->file->name}";
}],
[['size'], 'integer'],
[['name'], 'string', 'max' => 256],
[['type'], 'string', 'max' => 64],
[['filename'], 'string', 'max' => 256]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Basename',
'filename' => 'Filename',
'size' => 'Filesize',
'type' => 'Content Type',
];
}
/**
* @inherited
*/
public function beforeSave($insert)
{
if ($this->file && $this->file instanceof UploadedFile && parent::beforeSave($insert)) {
FileHelper::createDirectory(dirname($this->filename));
if ($this->saveCallback !== null) {
return call_user_func($this->saveCallback, $this);
} elseif ($this->cropParam && ($crop = Yii::$app->getRequest()->post($this->cropParam))) {
return $this->cropImage($crop);
} elseif ($this->resizeParam && ($resize = Yii::$app->getRequest()->post($this->resizeParam))) {
return $this->resizeImage($resize);
} else {
return $this->file->saveAs($this->filename, false);
}
}
return false;
}
protected function cropImage($crop)
{
$image = Image::crop($this->file->tempName, $crop['w'], $crop['h'], [$crop['x'], $crop['y']]);
$image->save($this->filename);
$this->size = filesize($this->filename);
return true;
}
protected function resizeImage($resize)
{
$image = Image::thumbnail($this->file->tempName, $resize['width'], $resize['height']);
$image->save($this->filename);
$this->size = filesize($this->filename);
return true;
}
/**
* @inherited
*/
public function beforeDelete()
{
if (parent::beforeDelete()) {
@unlink($this->filename);
return true;
}
return false;
}
/**
* Save file
* @param UploadedFile|string $file
* @param array $options
* @return boolean|static
*/
public static function saveAs($file, $options = [])
{
if (!($file instanceof UploadedFile)) {
$file = UploadedFile::getInstanceByName($file);
}
$options['file'] = $file;
$model = new static($options);
return $model->save() ? $model : false;
}
public function getContent()
{
return file_get_contents($this->filename);
}
}