Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getRelativeMediaUrl example
$media
=
$this
->
findMediaById
(
$mediaId
,
$context
)
;
$resource
=
$this
->
getFileSystem
(
$media
)
->
readStream
(
$this
->
getFilePath
(
$media
)
)
;
return
$this
->streamFactory->
createStreamFromResource
(
$resource
)
;
}
private
function
getFilePath
(
MediaEntity
$media
)
: string
{
$this
->fileNameValidator->
validateFileName
(
$media
->
getFileName
(
)
?: ''
)
;
return
$this
->urlGenerator->
getRelativeMediaUrl
(
$media
)
;
}
private
function
getFileSystem
(
MediaEntity
$media
)
: FilesystemOperator
{
if
(
$media
->
isPrivate
(
)
)
{
return
$this
->filesystemPrivate;
}
return
$this
->filesystemPublic;
}
$privatePaths
=
[
]
;
$publicPaths
=
[
]
;
$thumbnails
=
[
]
;
foreach
(
$media
as
$mediaEntity
)
{
if
(
!
$mediaEntity
->
hasFile
(
)
)
{
continue
;
}
if
(
$mediaEntity
->
isPrivate
(
)
)
{
$privatePaths
[
]
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$mediaEntity
)
;
}
else
{
$publicPaths
[
]
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$mediaEntity
)
;
}
if
(
!
$mediaEntity
->
getThumbnails
(
)
)
{
continue
;
}
foreach
(
$mediaEntity
->
getThumbnails
(
)
->
getIds
(
)
as
$id
)
{
$thumbnails
[
]
=
[
'id' =>
$id
]
;
}
}
private
readonly MediaService
$mediaService
,
private
readonly string
$localPrivateDownloadStrategy
)
{
}
public
function
getResponse
(
MediaEntity
$media
,
SalesChannelContext
$context
,
string
$expiration
= '+120 minutes'
)
: Response
{
$fileSystem
=
$this
->
getFileSystem
(
$media
)
;
$path
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$media
)
;
try
{
$url
=
$fileSystem
->
temporaryUrl
(
$path
,
(
new
\
DateTime
(
)
)
->
modify
(
$expiration
)
)
;
return
new
RedirectResponse
(
$url
)
;
}
catch
(
UnableToGenerateTemporaryUrl
)
{
}
return
$this
->
getDefaultResponse
(
$media
,
$context
,
$fileSystem
)
;
}
$this
->
doRenameMedia
(
$currentMedia
,
$destination
,
$context
)
;
}
private
function
doRenameMedia
(
MediaEntity
$currentMedia
, string
$destination
, Context
$context
)
: void
{
$updatedMedia
=
clone
$currentMedia
;
$updatedMedia
->
setFileName
(
$destination
)
;
$updatedMedia
->
setUploadedAt
(
new
\
DateTime
(
)
)
;
try
{
$renamedFiles
=
$this
->
renameFile
(
$this
->urlGenerator->
getRelativeMediaUrl
(
$currentMedia
)
,
$this
->urlGenerator->
getRelativeMediaUrl
(
$updatedMedia
)
,
$this
->
getFileSystem
(
$currentMedia
)
)
;
}
catch
(
\Exception
)
{
throw
MediaException::
couldNotRenameFile
(
$currentMedia
->
getId
(
)
,
(string)
$currentMedia
->
getFileName
(
)
)
;
}
foreach
(
$currentMedia
->
getThumbnails
(
)
??
[
]
as
$thumbnail
)
{
try
{
$renamedFiles
=
[
...
$renamedFiles
, ...
$this
->
renameThumbnail
(
$thumbnail
,
$currentMedia
,
$updatedMedia
)
]
;
}
catch
(
\Exception
)
{
public
function
testCreateFileIsWrittenInFs
(
)
: void
{
/** @var FilesystemOperator $fileSystem */
$fileSystem
=
$this
->
getContainer
(
)
->
get
(
'shopware.filesystem.private'
)
;
$document
=
$this
->
createDocumentWithFile
(
)
;
/** @var UrlGenerator $urlGenerator */
$urlGenerator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
static
::
assertNotNull
(
$document
->
getDocumentMediaFile
(
)
)
;
$filePath
=
$urlGenerator
->
getRelativeMediaUrl
(
$document
->
getDocumentMediaFile
(
)
)
;
static
::
assertTrue
(
$fileSystem
->
has
(
$filePath
)
)
;
$fileSystem
->
delete
(
$filePath
)
;
static
::
assertFalse
(
$fileSystem
->
has
(
$filePath
)
)
;
}
public
function
testReadDocumentFileWithInvalidDocumentId
(
)
: void
{
$documentId
= Uuid::
randomHex
(
)
;
// Remove if branch and keep else branch in v6.6 release
public
function
testDeleteNotUsedMedia
(
)
: void
{
$this
->
setFixtureContext
(
$this
->context
)
;
$txt
=
$this
->
getTxt
(
)
;
$png
=
$this
->
getPng
(
)
;
$withProduct
=
$this
->
getMediaWithProduct
(
)
;
$withManufacturer
=
$this
->
getMediaWithManufacturer
(
)
;
$urlGenerator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
$firstPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$txt
)
;
$secondPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$png
)
;
$thirdPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$withProduct
)
;
$fourthPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$withManufacturer
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$firstPath
, \
fopen
(
self::FIXTURE_FILE, 'rb'
)
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$secondPath
, \
fopen
(
self::FIXTURE_FILE, 'rb'
)
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$thirdPath
, \
fopen
(
self::FIXTURE_FILE, 'rb'
)
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$fourthPath
, \
fopen
(
self::FIXTURE_FILE, 'rb'
)
)
;
$this
->unusedMediaPurger->
deleteNotUsedMedia
(
)
;
$this
->
runWorker
(
)
;
$this
->pathnameStrategy->
generatePathHash
(
$media
)
,
$this
->pathnameStrategy->
generatePathCacheBuster
(
$media
)
,
$this
->pathnameStrategy->
generatePhysicalFilename
(
$media
)
,
]
)
;
}
/** * @throws MediaException */
public
function
getAbsoluteMediaUrl
(
MediaEntity
$media
)
: string
{
return
$this
->filesystem->
publicUrl
(
$this
->
getRelativeMediaUrl
(
$media
)
)
;
}
/** * @throws MediaException */
public
function
getRelativeThumbnailUrl
(
MediaEntity
$media
, MediaThumbnailEntity
$thumbnail
)
: string
{
$this
->
validateMedia
(
$media
)
;
return
$this
->
toPathString
(
[
'thumbnail',
$criteria
=
new
Criteria
(
$data
[
'mediaIds'
]
)
;
$criteria
->
setTitle
(
'mail-service::resolve-media-ids'
)
;
$media
= null;
$mediaRepository
=
$this
->mediaRepository;
$context
->
scope
(
Context::SYSTEM_SCOPE,
static
function
DContext
$context
)
use
(
$criteria
,
$mediaRepository
, &
$media
)
: void
{
/** @var MediaEntity[] $media */
$media
=
$mediaRepository
->
search
(
$criteria
,
$context
)
->
getElements
(
)
;
}
)
;
$urls
=
[
]
;
foreach
(
$media
??
[
]
as
$mediaItem
)
{
$urls
[
]
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$mediaItem
)
;
}
return
$urls
;
}
private
function
getSalesChannelDomainCriteria
(
string
$salesChannelId
, Context
$context
)
: Criteria
{
$criteria
=
new
Criteria
(
[
$salesChannelId
]
)
;
$criteria
->
setTitle
(
'mail-service::resolve-sales-channel-domain'
)
;
$criteria
->
addAssociation
(
'mailHeaderFooter'
)
;
$criteria
->
getAssociation
(
'domains'
)
'mimeType' => 'image/png',
'fileExtension' => 'png',
'fileName' =>
$mediaId
. '-' .
(
new
\
DateTime
(
)
)
->
getTimestamp
(
)
,
'mediaFolderId' =>
$folderId
,
]
,
]
,
$this
->context
)
;
$media
=
$this
->mediaRepository->
search
(
new
Criteria
(
[
$mediaId
]
)
,
$this
->context
)
->
get
(
$mediaId
)
;
static
::
assertInstanceOf
(
MediaEntity::
class
,
$media
)
;
$mediaPath
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
->
getRelativeMediaUrl
(
$media
)
;
$file
=
fopen
(
self::FIXTURE_FILE, 'rb'
)
;
static
::
assertIsResource
(
$file
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$mediaPath
,
$file
)
;
$this
->folderRepository->
delete
(
[
[
'id' =>
$folderId
]
]
,
$this
->context
)
;
static
::
assertEquals
(
0,
$this
->folderRepository->
search
(
new
Criteria
(
[
$folderId
]
)
,
$this
->context
)
->
getTotal
(
)
)
;
static
::
assertEquals
(
0,
$this
->mediaRepository->
search
(
new
Criteria
(
[
$mediaId
]
)
,
$this
->context
)
->
getTotal
(
)
)
;
$this
->
runWorker
(
)
;
$this
->thumbnailRepository =
$this
->
getContainer
(
)
->
get
(
'media_thumbnail.repository'
)
;
$this
->context = Context::
createDefaultContext
(
)
;
$this
->thumbnailService =
$this
->
getContainer
(
)
->
get
(
ThumbnailService::
class
)
;
}
public
function
testThumbnailGeneration
(
)
: void
{
$this
->
setFixtureContext
(
$this
->context
)
;
$media
=
$this
->
getPngWithFolder
(
)
;
$filePath
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$media
)
;
$resource
=
fopen
(
__DIR__ . '/../fixtures/shopware-logo.png', 'rb'
)
;
\
assert
(
$resource
!== false
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$filePath
,
$resource
)
;
$this
->thumbnailService->
updateThumbnails
(
$media
,
$this
->context,
false
)
;
public
function
testDeleteNotUsedMedia
(
)
: void
{
$this
->
setFixtureContext
(
$this
->context
)
;
$txt
=
$this
->
getTxt
(
)
;
$png
=
$this
->
getPng
(
)
;
$withProduct
=
$this
->
getMediaWithProduct
(
)
;
$withManufacturer
=
$this
->
getMediaWithManufacturer
(
)
;
$urlGenerator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
$firstPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$txt
)
;
$secondPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$png
)
;
$thirdPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$withProduct
)
;
$fourthPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$withManufacturer
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$firstPath
,
fopen
(
self::FIXTURE_FILE, 'rb'
)
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$secondPath
,
fopen
(
self::FIXTURE_FILE, 'rb'
)
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$thirdPath
,
fopen
(
self::FIXTURE_FILE, 'rb'
)
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$fourthPath
,
fopen
(
self::FIXTURE_FILE, 'rb'
)
)
;
$this
->deleteMediaService->
deleteNotUsedMedia
(
$this
->context
)
;
$this
->
runWorker
(
)
;
'mediaId' =>
$media
->
getId
(
)
,
'width' => 150,
'height' => 150,
]
,
]
,
$this
->context
)
;
/** @var MediaEntity $media */
$media
=
$this
->mediaRepository->
search
(
new
Criteria
(
[
$media
->
getId
(
)
]
)
,
$this
->context
)
->
get
(
$media
->
getId
(
)
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$this
->urlGenerator->
getRelativeMediaUrl
(
$media
)
,
fopen
(
__DIR__ . '/../fixtures/shopware-logo.png', 'rb'
)
)
;
$msg
=
new
GenerateThumbnailsMessage
(
)
;
$msg
->
setMediaIds
(
[
$media
->
getId
(
)
]
)
;
if
(
Feature::
isActive
(
'v6.6.0.0'
)
)
{
$msg
->
setContext
(
$this
->context
)
;
}
else
{
$msg
->
withContext
(
$this
->context
)
;
}
$mediaId
,
$context
)
;
}
finally
{
if
(
file_exists
(
$tempFile
)
)
{
unlink
(
$tempFile
)
;
}
}
$media
=
$this
->mediaRepository->
search
(
new
Criteria
(
[
$mediaId
]
)
,
$context
)
->
get
(
$mediaId
)
;
static
::
assertInstanceOf
(
MediaEntity::
class
,
$media
)
;
$path
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$media
)
;
static
::
assertTrue
(
$this
->
getPublicFilesystem
(
)
->
has
(
$path
)
)
;
}
public
function
testPersistFileWithUpperCaseExtension
(
)
: void
{
$tempFile
=
tempnam
(
sys_get_temp_dir
(
)
, ''
)
;
static
::
assertIsString
(
$tempFile
)
;
copy
(
self::TEST_IMAGE,
$tempFile
)
;
$fileSize
=
filesize
(
$tempFile
)
;
static
::
assertIsInt
(
$fileSize
)
;
$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
(
'media/d0/b3/24/file.jpg',
$urlGenerator
->
getRelativeMediaUrl
(
$mediaEntity
)
)
;
}
public
function
testRelativeThumbnailUrl
(
)
: void
{
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
$thumbnail
)
;
static
::
assertTrue
(
$this
->
getPublicFilesystem
(
)
->
has
(
$thumbnailPath
)
)
;
}
protected
function
createValidMediaFiles
(
)
: void
{
$this
->
setFixtureContext
(
$this
->context
)
;
$mediaPng
=
$this
->
getPngWithFolder
(
)
;
$mediaJpg
=
$this
->
getJpgWithFolder
(
)
;
$filePath
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$mediaPng
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$filePath
,
fopen
(
__DIR__ . '/../fixtures/shopware-logo.png', 'rb'
)
)
;
$filePath
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$mediaJpg
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$filePath
,
fopen
(
__DIR__ . '/../fixtures/shopware.jpg', 'rb'
)
)
;
}