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
ac49e396
Commit
ac49e396
authored
Feb 08, 2023
by
chentianyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
oss
parent
21acd8b5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
7 deletions
+36
-7
UploadOssService.php
src/UploadOss/UploadOssService.php
+36
-7
No files found.
src/UploadOss/UploadOssService.php
View file @
ac49e396
...
@@ -8,13 +8,13 @@
...
@@ -8,13 +8,13 @@
namespace
App\Services
;
namespace
App\Services
;
use
App\Model\File
;
use
Meibuyu\Rpc\UploadOss\FileModel
;
use
Hyperf\Contract\ConfigInterface
;
use
Hyperf\Contract\ConfigInterface
;
use
League\Flysystem\Filesystem
;
use
Hyperf\Filesystem\FilesystemFactory
;
use
Hyperf\Filesystem\FilesystemFactory
;
use
Hyperf\HttpMessage\Upload\UploadedFile
;
use
Hyperf\HttpMessage\Upload\UploadedFile
;
use
League\Flysystem\FileExistsException
;
use
League\Flysystem\FileExistsException
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Model\Auth
;
use
Psr\Container\ContainerInterface
;
use
Psr\Container\ContainerInterface
;
class
UploadOssService
class
UploadOssService
...
@@ -55,20 +55,22 @@ class UploadOssService
...
@@ -55,20 +55,22 @@ class UploadOssService
}
}
/**
/**
* 上传图片
* @param \Hyperf\HttpMessage\Upload\UploadedFile $image
* @param \Hyperf\HttpMessage\Upload\UploadedFile $image
* @param string $path
* @param string $path
* @return array
* @return array
* @throws \Meibuyu\Micro\Exceptions\HttpResponseException
* @throws \Meibuyu\Micro\Exceptions\HttpResponseException
* @author Zero
* @author Zero
*/
*/
public
function
uploadImage
(
UploadedFile
$image
,
string
$path
=
'default'
)
public
function
uploadImage
(
UploadedFile
$image
,
string
$path
,
string
$userId
)
{
{
$options
=
[
'mime'
=>
[
'jpeg'
,
'png'
,
'gif'
,
'jpg'
,
'svg'
]];
$options
=
[
'mime'
=>
[
'jpeg'
,
'png'
,
'gif'
,
'jpg'
,
'svg'
]];
$userId
=
Auth
::
id
();
$filePath
=
'meibuyu/'
.
$this
->
appDev
.
"/
$path
/images/"
.
today
()
.
"/
$userId
/"
.
$image
->
getClientFilename
();
$filePath
=
$this
->
appDev
.
"/upload/
$path
/images/"
.
today
()
.
"/
$userId
/"
.
$image
->
getClientFilename
();
$this
->
upload
(
$image
,
$filePath
,
$options
);
$this
->
upload
(
$image
,
$filePath
,
$options
);
$model
=
new
File
();
$model
=
new
File
Model
();
$model
=
$model
->
newInstance
([
$model
=
$model
->
newInstance
([
'type'
=>
'image'
,
'module'
=>
''
,
'user_id'
=>
$userId
,
'user_id'
=>
$userId
,
'size'
=>
num_2_file_size
(
$image
->
getSize
()),
'size'
=>
num_2_file_size
(
$image
->
getSize
()),
'name'
=>
$image
->
getClientFilename
(),
'name'
=>
$image
->
getClientFilename
(),
...
@@ -79,6 +81,33 @@ class UploadOssService
...
@@ -79,6 +81,33 @@ class UploadOssService
return
$model
->
toArray
();
return
$model
->
toArray
();
}
}
/**
* 上传文件
* @param \Hyperf\HttpMessage\Upload\UploadedFile $image
* @param string $path
* @return array
* @throws \Meibuyu\Micro\Exceptions\HttpResponseException
* @author Zero
*/
public
function
uploadFile
(
UploadedFile
$file
,
string
$path
,
string
$userId
,
array
$options
=
[])
{
$filePath
=
'meibuyu/'
.
$this
->
appDev
.
"/
$path
/images/"
.
today
()
.
"/
$userId
/"
.
$file
->
getClientFilename
();
$this
->
upload
(
$file
,
$filePath
,
$options
);
$model
=
new
FileModel
();
$model
=
$model
->
newInstance
([
'type'
=>
'file'
,
'module'
=>
''
,
'user_id'
=>
$userId
,
'size'
=>
num_2_file_size
(
$file
->
getSize
()),
'name'
=>
$file
->
getClientFilename
(),
'path'
=>
$this
->
urlPrefix
.
$filePath
,
'ext'
=>
$file
->
getExtension
(),
]);
$model
->
save
();
return
$model
->
toArray
();
}
/**
/**
* @param \Hyperf\HttpMessage\Upload\UploadedFile $file
* @param \Hyperf\HttpMessage\Upload\UploadedFile $file
* @param $path
* @param $path
...
@@ -87,7 +116,7 @@ class UploadOssService
...
@@ -87,7 +116,7 @@ class UploadOssService
* @throws \Meibuyu\Micro\Exceptions\HttpResponseException
* @throws \Meibuyu\Micro\Exceptions\HttpResponseException
* @author Zero
* @author Zero
*/
*/
public
function
upload
(
UploadedFile
$file
,
$path
,
array
$options
=
[])
public
function
upload
(
UploadedFile
$file
,
string
$path
,
array
$options
=
[])
{
{
if
(
$file
->
isValid
())
{
if
(
$file
->
isValid
())
{
$options
=
array_merge
(
self
::
$options
,
$options
);
$options
=
array_merge
(
self
::
$options
,
$options
);
...
...
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