Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-rpc
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-rpc
Commits
640ca6ce
Commit
640ca6ce
authored
Mar 30, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加通用上传管理器
parent
e43b86d7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
129 additions
and
0 deletions
+129
-0
UploadManager.php
src/Manager/UploadManager.php
+129
-0
No files found.
src/Manager/UploadManager.php
0 → 100644
View file @
640ca6ce
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/3/30
* Time: 9:56
*/
declare
(
strict_types
=
1
);
namespace
Meibuyu\Micro\Manager
;
use
Hyperf\HttpMessage\Upload\UploadedFile
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
class
UploadManager
{
public
static
$pathPrefix
=
'public/upload/'
;
public
static
$options
=
[
'path'
=>
'default'
,
// 默认保存路径
'maxSize'
=>
100000000
,
// 文件大小
'temp'
=>
false
,
// 是否为临时文件
'mime'
=>
[
'jpeg'
,
'png'
,
'gif'
,
'jpg'
,
'svg'
,
'txt'
,
'pdf'
,
'xlsx'
,
'xls'
,
'doc'
,
'docx'
,
'rar'
,
'zip'
],
// 允许上传的文件类型
];
/**
* 图片上传方法
* @param $image
* @param array $options
* @return string
* @throws HttpResponseException
*/
public
static
function
uploadImage
(
$image
,
$options
=
[])
{
$imgOptions
=
[
'path'
=>
'images'
,
'mime'
=>
[
'jpeg'
,
'png'
,
'gif'
,
'jpg'
,
'svg'
]
];
$options
=
array_merge
(
$imgOptions
,
$options
);
return
self
::
uploadFile
(
$image
,
$options
);
}
/**
* 表格上传方法
* @param $excel
* @param array $options
* @return string
* @throws HttpResponseException
*/
public
static
function
uploadExcel
(
$excel
,
$options
=
[])
{
$excelOptions
=
[
'path'
=>
'excel'
,
'mime'
=>
[
'xlsx'
,
'xls'
]
];
$options
=
array_merge
(
$excelOptions
,
$options
);
return
self
::
uploadFile
(
$excel
,
$options
);
}
/**
* 文件上传方法
* @param UploadedFile $file 上传的文件
* @param array $options 配置参数
* @return string
* @throws HttpResponseException
*/
public
static
function
uploadFile
(
$file
,
$options
=
[])
{
$options
=
self
::
parseOptions
(
$options
);
if
(
$file
->
isValid
())
{
$extension
=
$file
->
getExtension
();
// 通过扩展名判断类型
if
(
!
in_array
(
$extension
,
$options
[
'mime'
]))
{
throw
new
HttpResponseException
(
'文件类型不支持,目前只支持'
.
implode
(
','
,
$options
[
'mime'
]));
}
// 判断文件大小
if
(
$file
->
getSize
()
>
$options
[
'maxSize'
])
{
throw
new
HttpResponseException
(
'文件超出系统规定的大小,最大不能超过'
.
$options
[
'maxSize'
]);
}
// 文件重命名,由当前日期时间 + 唯一ID + 扩展名
$fileName
=
date
(
'YmdHis'
)
.
uniqid
()
.
'.'
.
$extension
;
$savePath
=
self
::
parsePath
(
$options
)
.
$fileName
;
$file
->
moveTo
(
$savePath
);
if
(
$file
->
isMoved
())
{
return
str_replace
(
'public/'
,
''
,
$savePath
);
}
else
{
throw
new
HttpResponseException
(
'文件保存失败'
);
}
}
else
{
throw
new
HttpResponseException
(
'文件无效'
);
}
}
/**
* 处理保存路径
* @param $options
* @return string
*/
public
static
function
parsePath
(
$options
)
{
if
(
isset
(
$options
[
'temp'
])
&&
$options
[
'temp'
]
&&
!
isset
(
$options
[
'path'
]))
{
// 如果是临时文件,且没有指定保存路径,修改保存路径为临时路径
$options
[
'path'
]
=
'temp'
;
}
$path
=
self
::
$pathPrefix
.
$options
[
'path'
]
.
'/'
.
date
(
'Y-m-d'
);
if
(
!
is_dir
(
$path
))
{
// 判断路径是否存在,不存在,则创建
mkdir
(
$path
,
0777
,
true
);
}
return
$path
.
'/'
;
}
/**
* 处理配置参数
* @param array $options
* @return array
*/
public
static
function
parseOptions
(
$options
=
[])
{
if
(
$options
==
[])
{
return
self
::
$options
;
}
else
{
return
array_merge
(
self
::
$options
,
$options
);
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment