Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-common
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-common
Commits
75b8b392
Commit
75b8b392
authored
Feb 13, 2023
by
chentianyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
设置是否自动保存记录到数据库
parent
5d7d5af3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
28 deletions
+54
-28
UploadOssService.php
src/UploadOss/UploadOssService.php
+54
-28
No files found.
src/UploadOss/UploadOssService.php
View file @
75b8b392
...
...
@@ -38,6 +38,12 @@ class UploadOssService
*/
private
$appName
;
/**
* 是否自动保存数据库
* @var bool
*/
private
$autoSaveDataBase
=
true
;
public
static
$options
=
[
'maxSize'
=>
10
*
1024
*
1024
,
// 文件大小,10M
'mime'
=>
[
'jpeg'
,
'png'
,
'gif'
,
'jpg'
,
'svg'
,
'txt'
,
'pdf'
,
'xlsx'
,
'xls'
,
'doc'
,
'docx'
,
'rar'
,
'zip'
,
'csv'
],
// 允许上传的文件类型
...
...
@@ -53,11 +59,21 @@ class UploadOssService
$this
->
appName
=
$this
->
config
->
get
(
'app_name'
);
}
/**
* 设置是否自动保存记录到数据库
* @param bool $bool
* @author chentianyu
*/
public
function
setAutoSaveDataBase
(
bool
$bool
)
{
$this
->
autoSaveDataBase
=
$bool
;
}
/**
* 上传图片
* @param \Hyperf\HttpMessage\Upload\UploadedFile $image
* @param string $module
* @return array
* @return array
|string
* @throws \Meibuyu\Common\Exceptions\HttpResponseException
* @author chentianyu
*/
...
...
@@ -69,26 +85,31 @@ class UploadOssService
$options
=
[
'mime'
=>
[
'jpeg'
,
'png'
,
'gif'
,
'jpg'
,
'svg'
]];
$filePath
=
'oss2/'
.
$this
->
appDev
.
"/"
.
$this
->
appName
.
"/
$module
/images/"
.
today
()
.
"/
$userId
/"
.
$clientFilename
;
$this
->
upload
(
$image
,
$filePath
,
$options
);
$model
=
new
FileModel
();
$model
=
$model
->
newInstance
([
'type'
=>
'image'
,
'module'
=>
$module
,
'user_id'
=>
$userId
,
'source_name'
=>
$clientFilename
,
'name'
=>
$fileName
,
'path'
=>
$this
->
urlPrefix
.
$filePath
,
'ext'
=>
$ext
,
'size'
=>
num_2_file_size
(
$image
->
getSize
()),
]);
$model
->
save
();
return
$model
->
toArray
();
if
(
$this
->
autoSaveDataBase
){
$model
=
new
FileModel
();
$model
=
$model
->
newInstance
([
'type'
=>
'image'
,
'module'
=>
$module
,
'user_id'
=>
$userId
,
'source_name'
=>
$clientFilename
,
'name'
=>
$fileName
,
'path'
=>
$this
->
urlPrefix
.
$filePath
,
'ext'
=>
$ext
,
'size'
=>
num_2_file_size
(
$image
->
getSize
()),
]);
$model
->
save
();
return
$model
->
toArray
();
}
else
{
return
$this
->
urlPrefix
.
$filePath
;
}
}
/**
* 上传文件
* @param \Hyperf\HttpMessage\Upload\UploadedFile $image
* @param string $module
* @return array
* @return array
|string
* @throws \Meibuyu\Common\Exceptions\HttpResponseException
* @author chentianyu
*/
...
...
@@ -99,19 +120,24 @@ class UploadOssService
$fileName
=
$uniqueFileName
?
(
$this
->
genUniqueFileName
()
.
'.'
.
$ext
)
:
$clientFilename
;
$filePath
=
'oss2/'
.
$this
->
appDev
.
"/"
.
$this
->
appName
.
"/
$module
/files/"
.
today
()
.
"/
$userId
/"
.
$clientFilename
;
$this
->
upload
(
$file
,
$filePath
,
$options
);
$model
=
new
FileModel
();
$model
=
$model
->
newInstance
([
'type'
=>
'file'
,
'module'
=>
$module
,
'user_id'
=>
$userId
,
'source_name'
=>
$clientFilename
,
'name'
=>
$fileName
,
'path'
=>
$this
->
urlPrefix
.
$filePath
,
'ext'
=>
$ext
,
'size'
=>
num_2_file_size
(
$file
->
getSize
()),
]);
$model
->
save
();
return
$model
->
toArray
();
if
(
$this
->
autoSaveDataBase
){
$model
=
new
FileModel
();
$model
=
$model
->
newInstance
([
'type'
=>
'file'
,
'module'
=>
$module
,
'user_id'
=>
$userId
,
'source_name'
=>
$clientFilename
,
'name'
=>
$fileName
,
'path'
=>
$this
->
urlPrefix
.
$filePath
,
'ext'
=>
$ext
,
'size'
=>
num_2_file_size
(
$file
->
getSize
()),
]);
$model
->
save
();
return
$model
->
toArray
();
}
else
{
return
$this
->
urlPrefix
.
$filePath
;
}
}
/**
...
...
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