Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
teebbstudios
teebblog
提交
3f1fec8a
T
teebblog
项目概览
teebbstudios
/
teebblog
通知
0
Star
5
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
teebblog
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
3f1fec8a
编写于
8月 02, 2021
作者:
伟
伟伟权
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
119. handle file upload
上级
4ae1b6bd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
148 addition
and
1 deletion
+148
-1
src/Controller/ApiFileController.php
src/Controller/ApiFileController.php
+41
-0
src/Entity/FileManaged.php
src/Entity/FileManaged.php
+51
-1
src/Serializer/Normalizer/FileAwareNormalizer.php
src/Serializer/Normalizer/FileAwareNormalizer.php
+56
-0
未找到文件。
src/Controller/ApiFileController.php
0 → 100644
浏览文件 @
3f1fec8a
<?php
namespace
App\Controller
;
use
App\Entity\FileManaged
;
use
Doctrine\ORM\EntityManagerInterface
;
use
Symfony\Bundle\FrameworkBundle\Controller\AbstractController
;
use
Symfony\Component\HttpFoundation\File\UploadedFile
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpKernel\Exception\BadRequestHttpException
;
class
ApiFileController
extends
AbstractController
{
public
function
__invoke
(
Request
$request
,
EntityManagerInterface
$em
,
string
$projectDir
)
{
/**@var UploadedFile $uploadedFile * */
$uploadedFile
=
$request
->
files
->
get
(
'file'
);
if
(
!
$uploadedFile
)
{
throw
new
BadRequestHttpException
(
'"file" is required'
);
}
$newFileName
=
$uploadedFile
->
getClientOriginalName
()
.
'_'
.
substr
(
hash
(
'sha1'
,
$uploadedFile
->
getClientOriginalName
()),
0
,
8
)
.
'.'
.
$uploadedFile
->
getClientOriginalExtension
();
$file
=
new
FileManaged
();
$file
->
setMimeType
(
$uploadedFile
->
getMimeType
());
$file
->
setOriginName
(
$uploadedFile
->
getClientOriginalName
());
$file
->
setFileName
(
$newFileName
);
$file
->
setFileSize
(
$uploadedFile
->
getSize
());
$file
->
setPath
(
'/uploads/images/'
.
$newFileName
);
$em
->
persist
(
$file
);
$em
->
flush
();
$uploadedFile
->
move
(
$projectDir
.
'/public/uploads/images'
,
$newFileName
);
return
$file
;
}
}
\ No newline at end of file
src/Entity/FileManaged.php
浏览文件 @
3f1fec8a
...
...
@@ -3,6 +3,7 @@
namespace
App\Entity
;
use
ApiPlatform\Core\Annotation\ApiResource
;
use
App\Controller\ApiFileController
;
use
App\Repository\FileManagedRepository
;
use
Doctrine\ORM\Mapping
as
ORM
;
...
...
@@ -10,7 +11,36 @@ use Doctrine\ORM\Mapping as ORM;
* @ORM\Entity(repositoryClass=FileManagedRepository::class)
* @ORM\Table(name="file")
*/
#[ApiResource]
#[ApiResource(
collectionOperations
:
[
'get'
,
'post'
=>
[
'controller'
=>
ApiFileController
::
class
,
'method'
=>
'post'
,
'deserialize'
=>
false
,
'openapi_context'
=>
[
'requestBody'
=>
[
'content'
=>
[
'multipart/form-data'
=>
[
'schema'
=>
[
'type'
=>
'object'
,
'properties'
=>
[
'file'
=>
[
'type'
=>
'string'
,
'format'
=>
'binary'
,
],
],
],
],
],
],
],
]
],
itemOperations
:
[
'get'
]
)]
class
FileManaged
{
/**
...
...
@@ -45,6 +75,8 @@ class FileManaged
*/
private
$path
;
private
$fileUrl
;
public
function
getId
():
?int
{
return
$this
->
id
;
...
...
@@ -109,4 +141,22 @@ class FileManaged
return
$this
;
}
/**
* @return mixed
*/
public
function
getFileUrl
()
{
return
$this
->
fileUrl
;
}
/**
* @param mixed $fileUrl
*/
public
function
setFileUrl
(
$fileUrl
):
void
{
$this
->
fileUrl
=
$fileUrl
;
}
}
src/Serializer/Normalizer/FileAwareNormalizer.php
0 → 100644
浏览文件 @
3f1fec8a
<?php
namespace
App\Serializer\Normalizer
;
use
App\Entity\FileManaged
;
use
Symfony\Component\HttpFoundation\RequestStack
;
use
Symfony\Component\Serializer\Exception\CircularReferenceException
;
use
Symfony\Component\Serializer\Exception\ExceptionInterface
;
use
Symfony\Component\Serializer\Exception\InvalidArgumentException
;
use
Symfony\Component\Serializer\Exception\LogicException
;
use
Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface
;
use
Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface
;
use
Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait
;
class
FileAwareNormalizer
implements
NormalizerAwareInterface
,
ContextAwareNormalizerInterface
{
use
NormalizerAwareTrait
;
/**
* @var RequestStack
*/
private
RequestStack
$requestStack
;
public
function
__construct
(
RequestStack
$requestStack
)
{
$this
->
requestStack
=
$requestStack
;
}
private
const
FILE_NORMALIZER_ALREADY_CALLED
=
'file_normalizer_already_called'
;
public
function
supportsNormalization
(
$data
,
string
$format
=
null
,
array
$context
=
[])
{
if
(
isset
(
$context
[
self
::
FILE_NORMALIZER_ALREADY_CALLED
]))
{
return
false
;
}
return
$data
instanceof
FileManaged
;
}
/**
* @param FileManaged $object
* @param string|null $format
* @param array $context
* @return array|\ArrayObject|bool|float|int|string|null
* @throws ExceptionInterface
*/
public
function
normalize
(
$object
,
string
$format
=
null
,
array
$context
=
[])
{
$context
[
self
::
FILE_NORMALIZER_ALREADY_CALLED
]
=
true
;
$object
->
setFileUrl
(
$this
->
requestStack
->
getCurrentRequest
()
->
getSchemeAndHttpHost
()
.
$object
->
getPath
());
return
$this
->
normalizer
->
normalize
(
$object
,
$format
,
$context
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录