feat: 同步下新环境
This commit is contained in:
@@ -16,64 +16,61 @@ use SplFileObject;
|
||||
class File extends SplFileObject
|
||||
{
|
||||
/**
|
||||
* 错误信息
|
||||
* @var string
|
||||
* @var string 错误信息
|
||||
*/
|
||||
private $error = '';
|
||||
|
||||
/**
|
||||
* 当前完整文件名
|
||||
* @var string
|
||||
* @var string 当前完整文件名
|
||||
*/
|
||||
protected $filename;
|
||||
|
||||
/**
|
||||
* 上传文件名
|
||||
* @var string
|
||||
* @var string 上传文件名
|
||||
*/
|
||||
protected $saveName;
|
||||
|
||||
/**
|
||||
* 上传文件命名规则
|
||||
* @var string
|
||||
* @var string 文件上传命名规则
|
||||
*/
|
||||
protected $rule = 'date';
|
||||
|
||||
/**
|
||||
* 上传文件验证规则
|
||||
* @var array
|
||||
* @var array 文件上传验证规则
|
||||
*/
|
||||
protected $validate = [];
|
||||
|
||||
/**
|
||||
* 是否单元测试
|
||||
* @var bool
|
||||
* @var bool 单元测试
|
||||
*/
|
||||
protected $isTest;
|
||||
|
||||
/**
|
||||
* 上传文件信息
|
||||
* @var array
|
||||
* @var array 上传文件信息
|
||||
*/
|
||||
protected $info = [];
|
||||
protected $info;
|
||||
|
||||
/**
|
||||
* 文件hash规则
|
||||
* @var array
|
||||
* @var array 文件 hash 信息
|
||||
*/
|
||||
protected $hash = [];
|
||||
|
||||
/**
|
||||
* File constructor.
|
||||
* @access public
|
||||
* @param string $filename 文件名称
|
||||
* @param string $mode 访问模式
|
||||
*/
|
||||
public function __construct($filename, $mode = 'r')
|
||||
{
|
||||
parent::__construct($filename, $mode);
|
||||
|
||||
$this->filename = $this->getRealPath() ?: $this->getPathname();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否测试
|
||||
* 设置是否是单元测试
|
||||
* @access public
|
||||
* @param bool $test 是否测试
|
||||
* @param bool $test 是否是测试
|
||||
* @return $this
|
||||
*/
|
||||
public function isTest($test = false)
|
||||
@@ -86,7 +83,7 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 设置上传信息
|
||||
* @access public
|
||||
* @param array $info 上传文件信息
|
||||
* @param array $info 上传文件信息
|
||||
* @return $this
|
||||
*/
|
||||
public function setUploadInfo($info)
|
||||
@@ -99,7 +96,7 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 获取上传文件的信息
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @param string $name 信息名称
|
||||
* @return array|string
|
||||
*/
|
||||
public function getInfo($name = '')
|
||||
@@ -120,7 +117,7 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 设置上传文件的保存文件名
|
||||
* @access public
|
||||
* @param string $saveName
|
||||
* @param string $saveName 保存名称
|
||||
* @return $this
|
||||
*/
|
||||
public function setSaveName($saveName)
|
||||
@@ -133,7 +130,7 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 获取文件的哈希散列值
|
||||
* @access public
|
||||
* @param string $type
|
||||
* @param string $type 类型
|
||||
* @return string
|
||||
*/
|
||||
public function hash($type = 'sha1')
|
||||
@@ -148,20 +145,17 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 检查目录是否可写
|
||||
* @access protected
|
||||
* @param string $path 目录
|
||||
* @param string $path 目录
|
||||
* @return boolean
|
||||
*/
|
||||
protected function checkPath($path)
|
||||
{
|
||||
if (is_dir($path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (mkdir($path, 0755, true)) {
|
||||
if (is_dir($path) || mkdir($path, 0755, true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->error = ['directory {:path} creation failed', ['path' => $path]];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -180,7 +174,7 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 设置文件的命名规则
|
||||
* @access public
|
||||
* @param string $rule 文件命名规则
|
||||
* @param string $rule 文件命名规则
|
||||
* @return $this
|
||||
*/
|
||||
public function rule($rule)
|
||||
@@ -193,10 +187,10 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 设置上传文件的验证规则
|
||||
* @access public
|
||||
* @param array $rule 验证规则
|
||||
* @param array $rule 验证规则
|
||||
* @return $this
|
||||
*/
|
||||
public function validate($rule = [])
|
||||
public function validate(array $rule = [])
|
||||
{
|
||||
$this->validate = $rule;
|
||||
|
||||
@@ -210,27 +204,40 @@ class File extends SplFileObject
|
||||
*/
|
||||
public function isValid()
|
||||
{
|
||||
if ($this->isTest) {
|
||||
return is_file($this->filename);
|
||||
}
|
||||
|
||||
return is_uploaded_file($this->filename);
|
||||
return $this->isTest ? is_file($this->filename) : is_uploaded_file($this->filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测上传文件
|
||||
* @access public
|
||||
* @param array $rule 验证规则
|
||||
* @param array $rule 验证规则
|
||||
* @return bool
|
||||
*/
|
||||
public function check($rule = [])
|
||||
{
|
||||
$rule = $rule ?: $this->validate;
|
||||
|
||||
if ((isset($rule['size']) && !$this->checkSize($rule['size']))
|
||||
|| (isset($rule['type']) && !$this->checkMime($rule['type']))
|
||||
|| (isset($rule['ext']) && !$this->checkExt($rule['ext']))
|
||||
|| !$this->checkImg()) {
|
||||
/* 检查文件大小 */
|
||||
if (isset($rule['size']) && !$this->checkSize($rule['size'])) {
|
||||
$this->error = 'filesize not match';
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 检查文件 Mime 类型 */
|
||||
if (isset($rule['type']) && !$this->checkMime($rule['type'])) {
|
||||
$this->error = 'mimetype to upload is not allowed';
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 检查文件后缀 */
|
||||
if (isset($rule['ext']) && !$this->checkExt($rule['ext'])) {
|
||||
$this->error = 'extensions to upload is not allowed';
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 检查图像文件 */
|
||||
if (!$this->checkImg()) {
|
||||
$this->error = 'illegal image files';
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -240,7 +247,7 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 检测上传文件后缀
|
||||
* @access public
|
||||
* @param array|string $ext 允许后缀
|
||||
* @param array|string $ext 允许后缀
|
||||
* @return bool
|
||||
*/
|
||||
public function checkExt($ext)
|
||||
@@ -251,12 +258,7 @@ class File extends SplFileObject
|
||||
|
||||
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION));
|
||||
|
||||
if (!in_array($extension, $ext)) {
|
||||
$this->error = 'extensions to upload is not allowed';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return in_array($extension, $ext);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,16 +270,16 @@ class File extends SplFileObject
|
||||
{
|
||||
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION));
|
||||
|
||||
/* 对图像文件进行严格检测 */
|
||||
if (in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && !in_array($this->getImageType($this->filename), [1, 2, 3, 4, 6, 13])) {
|
||||
$this->error = 'illegal image files';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
// 如果上传的不是图片,或者是图片而且后缀确实符合图片类型则返回 true
|
||||
return !in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) || in_array($this->getImageType($this->filename), [1, 2, 3, 4, 6, 13]);
|
||||
}
|
||||
|
||||
// 判断图像类型
|
||||
/**
|
||||
* 判断图像类型
|
||||
* @access protected
|
||||
* @param string $image 图片名称
|
||||
* @return bool|int
|
||||
*/
|
||||
protected function getImageType($image)
|
||||
{
|
||||
if (function_exists('exif_imagetype')) {
|
||||
@@ -295,49 +297,36 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 检测上传文件大小
|
||||
* @access public
|
||||
* @param integer $size 最大大小
|
||||
* @param integer $size 最大大小
|
||||
* @return bool
|
||||
*/
|
||||
public function checkSize($size)
|
||||
{
|
||||
if ($this->getSize() > (int) $size) {
|
||||
$this->error = 'filesize not match';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return $this->getSize() <= $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测上传文件类型
|
||||
* @access public
|
||||
* @param array|string $mime 允许类型
|
||||
* @param array|string $mime 允许类型
|
||||
* @return bool
|
||||
*/
|
||||
public function checkMime($mime)
|
||||
{
|
||||
if (is_string($mime)) {
|
||||
$mime = explode(',', $mime);
|
||||
}
|
||||
$mime = is_string($mime) ? explode(',', $mime) : $mime;
|
||||
|
||||
if (!in_array(strtolower($this->getMime()), $mime)) {
|
||||
$this->error = 'mimetype to upload is not allowed';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return in_array(strtolower($this->getMime()), $mime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动文件
|
||||
* @access public
|
||||
* @param string $path 保存路径
|
||||
* @param string|bool $savename 保存的文件名 默认自动生成
|
||||
* @param boolean $replace 同名文件是否覆盖
|
||||
* @param bool $autoAppendExt 自动补充扩展名
|
||||
* @return false|File false-失败 否则返回File实例
|
||||
* @param string $path 保存路径
|
||||
* @param string|bool $savename 保存的文件名 默认自动生成
|
||||
* @param boolean $replace 同名文件是否覆盖
|
||||
* @return false|File
|
||||
*/
|
||||
public function move($path, $savename = true, $replace = true, $autoAppendExt = true)
|
||||
public function move($path, $savename = true, $replace = true)
|
||||
{
|
||||
// 文件上传失败,捕获错误代码
|
||||
if (!empty($this->info['error'])) {
|
||||
@@ -356,9 +345,9 @@ class File extends SplFileObject
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||
$path = rtrim($path, DS) . DS;
|
||||
// 文件保存命名规则
|
||||
$saveName = $this->buildSaveName($savename, $autoAppendExt);
|
||||
$saveName = $this->buildSaveName($savename);
|
||||
$filename = $path . $saveName;
|
||||
|
||||
// 检测目录
|
||||
@@ -366,7 +355,7 @@ class File extends SplFileObject
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 不覆盖同名文件 */
|
||||
// 不覆盖同名文件
|
||||
if (!$replace && is_file($filename)) {
|
||||
$this->error = ['has the same filename: {:filename}', ['filename' => $filename]];
|
||||
return false;
|
||||
@@ -380,10 +369,9 @@ class File extends SplFileObject
|
||||
return false;
|
||||
}
|
||||
|
||||
// 返回 File对象实例
|
||||
// 返回 File 对象实例
|
||||
$file = new self($filename);
|
||||
$file->setSaveName($saveName);
|
||||
$file->setUploadInfo($this->info);
|
||||
$file->setSaveName($saveName)->setUploadInfo($this->info);
|
||||
|
||||
return $file;
|
||||
}
|
||||
@@ -391,60 +379,47 @@ class File extends SplFileObject
|
||||
/**
|
||||
* 获取保存文件名
|
||||
* @access protected
|
||||
* @param string|bool $savename 保存的文件名 默认自动生成
|
||||
* @param bool $autoAppendExt 自动补充扩展名
|
||||
* @param string|bool $savename 保存的文件名 默认自动生成
|
||||
* @return string
|
||||
*/
|
||||
protected function buildSaveName($savename, $autoAppendExt = true)
|
||||
protected function buildSaveName($savename)
|
||||
{
|
||||
// 自动生成文件名
|
||||
if (true === $savename) {
|
||||
// 自动生成文件名
|
||||
$savename = $this->autoBuildName();
|
||||
if ($this->rule instanceof \Closure) {
|
||||
$savename = call_user_func_array($this->rule, [$this]);
|
||||
} else {
|
||||
switch ($this->rule) {
|
||||
case 'date':
|
||||
$savename = date('Ymd') . DS . md5(microtime(true));
|
||||
break;
|
||||
default:
|
||||
if (in_array($this->rule, hash_algos())) {
|
||||
$hash = $this->hash($this->rule);
|
||||
$savename = substr($hash, 0, 2) . DS . substr($hash, 2);
|
||||
} elseif (is_callable($this->rule)) {
|
||||
$savename = call_user_func($this->rule);
|
||||
} else {
|
||||
$savename = date('Ymd') . DS . md5(microtime(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ('' === $savename || false === $savename) {
|
||||
// 保留原文件名
|
||||
$savename = $this->getInfo('name');
|
||||
}
|
||||
|
||||
if ($autoAppendExt && false === strpos($savename, '.')) {
|
||||
if (!strpos($savename, '.')) {
|
||||
$savename .= '.' . pathinfo($this->getInfo('name'), PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
return $savename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动生成文件名
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function autoBuildName()
|
||||
{
|
||||
if ($this->rule instanceof \Closure) {
|
||||
$savename = call_user_func_array($this->rule, [$this]);
|
||||
} else {
|
||||
switch ($this->rule) {
|
||||
case 'date':
|
||||
$savename = date('Ymd') . DIRECTORY_SEPARATOR . md5(microtime(true));
|
||||
break;
|
||||
default:
|
||||
if (in_array($this->rule, hash_algos())) {
|
||||
$hash = $this->hash($this->rule);
|
||||
$savename = substr($hash, 0, 2) . DIRECTORY_SEPARATOR . substr($hash, 2);
|
||||
} elseif (is_callable($this->rule)) {
|
||||
$savename = call_user_func($this->rule);
|
||||
} else {
|
||||
$savename = date('Ymd') . DIRECTORY_SEPARATOR . md5(microtime(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $savename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误代码信息
|
||||
* @access private
|
||||
* @param int $errorNo 错误号
|
||||
* @param int $errorNo 错误号
|
||||
* @return $this
|
||||
*/
|
||||
private function error($errorNo)
|
||||
{
|
||||
@@ -468,6 +443,8 @@ class File extends SplFileObject
|
||||
default:
|
||||
$this->error = 'unknown upload error';
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -477,8 +454,6 @@ class File extends SplFileObject
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
$lang = Container::get('lang');
|
||||
|
||||
if (is_array($this->error)) {
|
||||
list($msg, $vars) = $this->error;
|
||||
} else {
|
||||
@@ -486,9 +461,16 @@ class File extends SplFileObject
|
||||
$vars = [];
|
||||
}
|
||||
|
||||
return $lang->has($msg) ? $lang->get($msg, $vars) : $msg;
|
||||
return Lang::has($msg) ? Lang::get($msg, $vars) : $msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 魔法方法,获取文件的 hash 值
|
||||
* @access public
|
||||
* @param string $method 方法名
|
||||
* @param mixed $args 调用参数
|
||||
* @return string
|
||||
*/
|
||||
public function __call($method, $args)
|
||||
{
|
||||
return $this->hash($method);
|
||||
|
||||
Reference in New Issue
Block a user