Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
emptyMediaFilename example
'#',
'{',
'}',
]
;
/** * @throws MediaException */
public
function
validateFileName
(
string
$fileName
)
: void
{
if
(
empty
(
$fileName
)
)
{
throw
MediaException::
emptyMediaFilename
(
)
;
}
$this
->
validateFileNameDoesNotEndWithSpaces
(
$fileName
)
;
$this
->
validateFileNameDoesNotEndOrStartWithDot
(
$fileName
)
;
$this
->
validateFileNameDoesNotContainForbiddenCharacter
(
$fileName
)
;
$this
->
validateFileNameDoesNotContainC0Character
(
$fileName
)
;
}
private
function
validateFileNameDoesNotEndOrStartWithDot
(
string
$fileName
)
: void
{
if
(
mb_substr
(
$fileName
, 0, 1
)
=== '.'
)
{
return
$responseFactory
->
createRedirectResponse
(
$this
->mediaDefinition,
$mediaId
,
$request
,
$context
)
;
}
#[Route(path: '/api/_action/media/{mediaId}/rename', name: 'api.action.media.rename', methods: ['POST'])]
public
function
renameMediaFile
(
Request
$request
, string
$mediaId
, Context
$context
, ResponseFactoryInterface
$responseFactory
)
: Response
{
$fileName
=
$request
->request->
getString
(
'fileName'
)
;
$destination
=
preg_replace
(
'/[\x00-\x1F\x7F-\xFF]/', '',
$fileName
)
;
if
(
$destination
=== ''
)
{
throw
MediaException::
emptyMediaFilename
(
)
;
}
if
(
!\
is_string
(
$destination
)
)
{
throw
MediaException::
illegalFileName
(
$fileName
, 'Filename must be a string'
)
;
}
$this
->fileSaver->
renameMedia
(
$mediaId
,
$destination
,
$context
)
;
return
$responseFactory
->
createRedirectResponse
(
$this
->mediaDefinition,
$mediaId
,
$request
,
$context
)
;
}
/** * @throws MediaException */
private
function
validateMedia
(
MediaEntity
$media
)
: void
{
if
(
empty
(
$media
->
getId
(
)
)
)
{
throw
MediaException::
emptyMediaId
(
)
;
}
if
(
empty
(
$media
->
getFileName
(
)
)
)
{
throw
MediaException::
emptyMediaFilename
(
)
;
}
}
}
$exception
= MediaException::
invalidFile
(
$cause
)
;
static
::
assertEquals
(
Response::HTTP_BAD_REQUEST,
$exception
->
getStatusCode
(
)
)
;
static
::
assertEquals
(
MediaException::MEDIA_INVALID_FILE,
$exception
->
getErrorCode
(
)
)
;
static
::
assertEquals
(
'Provided file is invalid: cause.',
$exception
->
getMessage
(
)
)
;
static
::
assertEquals
(
[
'cause' =>
$cause
]
,
$exception
->
getParameters
(
)
)
;
}
public
function
testEmptyMediaFilename
(
)
: void
{
$exception
= MediaException::
emptyMediaFilename
(
)
;
static
::
assertEquals
(
Response::HTTP_BAD_REQUEST,
$exception
->
getStatusCode
(
)
)
;
static
::
assertEquals
(
MediaException::MEDIA_EMPTY_FILE_NAME,
$exception
->
getErrorCode
(
)
)
;
static
::
assertEquals
(
'A valid filename must be provided.',
$exception
->
getMessage
(
)
)
;
}
public
function
testDuplicatedMediaFileName
(
)
: void
{
$fileName
= 'file-name';
$fileExtension
= 'file-extension';