-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttachmentTrait.php
140 lines (129 loc) · 3.65 KB
/
AttachmentTrait.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
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\attachment;
use Yii;
/**
* Trait ModuleTrait
* @property-read Module $module
* @package yuncms\attachment
*/
trait AttachmentTrait
{
/**
* 获取附件模块配置
* @param string $key
* @param null $default
* @return bool|mixed|string
*/
public function getSetting($key, $default = null)
{
$value = Yii::$app->settings->get($key, 'attachment', $default);
if ($key == 'storePath' || $key == 'storeUrl') {
return Yii::getAlias($value);
}
return $value;
}
/**
* 获取允许上传的最大图像大小
* @return int
*/
public function getImageMaxSizeByte()
{
$imageMaxSize = $this->getSetting('imageMaxSize');
return $this->getMaxUploadByte($imageMaxSize);
}
/**
* 获取允许上传的最大视频大小
* @return int
*/
public function getVideoMaxSizeByte()
{
$videoMaxSize = $this->getSetting('videoMaxSize');
return $this->getMaxUploadByte($videoMaxSize);
}
/**
* 获取允许上传的最大文件大小
* @return int
*/
public function getFileMaxSizeByte()
{
$fileMaxSize = $this->getSetting('fileMaxSize');
return $this->getMaxUploadByte($fileMaxSize);
}
/**
* 获取允许上传的图像 mimeTypes 列表
* @return array ['image/jpg','image/png']
*/
public function getAcceptImageMimeTypes()
{
$imageAllowFiles = $this->getSetting('imageAllowFiles');
$extensions = explode(',', $imageAllowFiles);
array_walk($extensions, function (&$value) {
$value = 'image/' . $value;
});
return $extensions;
}
/**
* 格式化后缀
*
* @param string $extensions 后缀数组 jpg,png,gif,bmp
* @return mixed ['.jpg','.png']
*/
public function normalizeExtension($extensions)
{
$extensions = explode(',', $extensions);
array_walk($extensions, function (&$value) {
$value = '.' . $value;
});
return $extensions;
}
/**
* 获取一个暂未使用的路径用来存放临时文件
* @param string $path
* @return string
*/
protected function getUnusedPath($path)
{
$newPath = $path;
$info = pathinfo($path);
$suffix = 1;
while (file_exists($newPath)) {
$newPath = $info['dirname'] . DIRECTORY_SEPARATOR . "{$info['filename']}_{$suffix}";
if (isset($info['extension'])) {
$newPath .= ".{$info['extension']}";
}
$suffix++;
}
return $newPath;
}
/**
* 返回允许上传的最大大小单位 Byte
* @param string $maxSize 最大上传大小MB
* @return int the max upload size in Byte
*/
public function getMaxUploadByte($maxSize = null)
{
return $this->getMaxUploadSize($maxSize) * 1024 * 1024;
}
/**
* 返回允许上传的最大大小单位 MB
* @param string $maxSize 最大上传大小MB
* @return int the max upload size in MB
*/
public function getMaxUploadSize($maxSize = null)
{
$maxUpload = (int)(ini_get('upload_max_filesize'));
$maxPost = (int)(ini_get('post_max_size'));
$memoryLimit = (int)(ini_get('memory_limit'));
$min = min($maxUpload, $maxPost, $memoryLimit);
if ($maxSize) {
$maxSize = (int)$maxSize;
return min($maxSize, $min);
}
return $min;
}
}