Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getAbsoluteMediaUrl example
}
public
function
addUrls
(
EntityLoadedEvent
$event
)
: void
{
/** @var MediaEntity $media */
foreach
(
$event
->
getEntities
(
)
as
$media
)
{
if
(
!
$media
->
hasFile
(
)
||
$media
->
isPrivate
(
)
)
{
continue
;
}
$media
->
setUrl
(
$this
->urlGenerator->
getAbsoluteMediaUrl
(
$media
)
)
;
foreach
(
$media
->
getThumbnails
(
)
??
[
]
as
$thumbnail
)
{
$this
->
addThumbnailUrl
(
$thumbnail
,
$media
)
;
}
}
}
public
function
unserialize
(
EntityLoadedEvent
$event
)
: void
{
/** @var MediaEntity $media */
foreach
(
$event
->
getEntities
(
)
as
$media
)
{
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
$urlGenerator
=
new
UrlGenerator
(
new
FilenamePathnameStrategy
(
)
,
new
Filesystem
(
new
InMemoryFilesystemAdapter
(
)
,
[
'public_url' => 'http://localhost:8000'
]
)
)
;
static
::
assertSame
(
'http://localhost:8000/media/d0/b3/24/file.jpg',
$urlGenerator
->
getAbsoluteMediaUrl
(
$mediaEntity
)
)
;
}
public
function
testMediaUrlWithEmptyRequest
(
)
: void
{
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
$urlGenerator
=
new
UrlGenerator
(
new
FilenamePathnameStrategy
(
)
,
new
Filesystem
(
new
InMemoryFilesystemAdapter
(
)
,
[
'public_url' => 'http://localhost:8000'
]
)
)
;
static
::
assertSame
(
'http://localhost:8000/media/d0/b3/24/file.jpg',
$urlGenerator
->
getAbsoluteMediaUrl
(
$mediaEntity
)
)
;
}
public
function
testMediaUrlWithEmptyRequest
(
)
: void
{
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
$filter
=
new
UrlEncodingTwigFilter
(
)
;
$urlGenerator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
$uploadTime
=
new
\
DateTime
(
)
;
$utc
=
$uploadTime
->
getTimestamp
(
)
;
$media
=
new
MediaEntity
(
)
;
$media
->
setId
(
Uuid::
randomHex
(
)
)
;
$media
->
setMimeType
(
'image/png'
)
;
$media
->
setFileExtension
(
'png'
)
;
$media
->
setUploadedAt
(
$uploadTime
)
;
$media
->
setFileName
(
'(image with spaces and brackets)'
)
;
$media
->
setUrl
(
$urlGenerator
->
getAbsoluteMediaUrl
(
$media
)
)
;
static
::
assertStringEndsWith
(
"{
$utc
}
/%28image%20with%20spaces%20and%20brackets%29.png",
(string)
$filter
->
encodeMediaUrl
(
$media
)
)
;
}
}
return
new
RedirectResponse
(
$url
)
;
}
catch
(
UnableToGenerateTemporaryUrl
)
{
}
return
$this
->
getDefaultResponse
(
$media
,
$context
,
$fileSystem
)
;
}
private
function
getDefaultResponse
(
MediaEntity
$media
, SalesChannelContext
$context
, FilesystemOperator
$fileSystem
)
: Response
{
if
(
!
$media
->
isPrivate
(
)
)
{
return
new
RedirectResponse
(
$this
->urlGenerator->
getAbsoluteMediaUrl
(
$media
)
)
;
}
switch
(
$this
->localPrivateDownloadStrategy
)
{
case
self::X_SENDFILE_DOWNLOAD_STRATEGRY:
$location
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$media
)
;
$stream
=
$fileSystem
->
readStream
(
$location
)
;
$location
= \
is_resource
(
$stream
)
?
stream_get_meta_data
(
$stream
)
[
'uri'
]
:
$location
;
$response
=
new
Response
(
null, 200,
$this
->
getStreamHeaders
(
$media
)
)
;
$response
->headers->
set
(
'X-Sendfile',
$location
)
;