From e5bc4a7e417b406da05893cc5993097fd3209b49 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 16 Feb 2024 16:55:51 +0100 Subject: [PATCH] full file url for media objects --- src/Mapper/ContactEntityToApiMapper.php | 7 ++- src/Mapper/PartnerEntityToApiMapper.php | 5 +- src/Serializer/MediaObjectNormalizer.php | 66 ++++++++++++++++++++++++ src/Service/FileUrlService.php | 35 +++++++++++++ 4 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 src/Serializer/MediaObjectNormalizer.php create mode 100644 src/Service/FileUrlService.php diff --git a/src/Mapper/ContactEntityToApiMapper.php b/src/Mapper/ContactEntityToApiMapper.php index 151dd3d..3647aa5 100644 --- a/src/Mapper/ContactEntityToApiMapper.php +++ b/src/Mapper/ContactEntityToApiMapper.php @@ -6,19 +6,18 @@ use App\ApiResource\ContactApi; use App\ApiResource\PartnerApi; use App\ApiResource\PostingApi; use App\Entity\Contact; -use App\Entity\Partner; use App\Entity\Posting; +use App\Service\FileUrlService; use Symfonycasts\MicroMapper\AsMapper; use Symfonycasts\MicroMapper\MapperInterface; use Symfonycasts\MicroMapper\MicroMapperInterface; -use Vich\UploaderBundle\Storage\StorageInterface; #[AsMapper(from: Contact::class, to: ContactApi::class)] class ContactEntityToApiMapper implements MapperInterface { public function __construct( private MicroMapperInterface $microMapper, - private StorageInterface $storage + private FileUrlService $fileUrlService ) { } @@ -50,7 +49,7 @@ class ContactEntityToApiMapper implements MapperInterface ); $dto->birthday = $entity->getBirthday(); $dto->image = $entity->getImage(); - $dto->imageUrl = $entity->getImage() !== null ? $this->storage->resolveUri($entity->getImage(), 'file') : null; + $dto->imageUrl = $this->fileUrlService->getFileUrl($entity->getImage()); $dto->position = $entity->getPosition(); $dto->phone = $entity->getPhone(); $dto->email = $entity->getEmail(); diff --git a/src/Mapper/PartnerEntityToApiMapper.php b/src/Mapper/PartnerEntityToApiMapper.php index 1b8b655..a6d4e70 100644 --- a/src/Mapper/PartnerEntityToApiMapper.php +++ b/src/Mapper/PartnerEntityToApiMapper.php @@ -6,6 +6,7 @@ use App\ApiResource\ContactApi; use App\ApiResource\PartnerApi; use App\Entity\Contact; use App\Entity\Partner; +use App\Service\FileUrlService; use Symfonycasts\MicroMapper\AsMapper; use Symfonycasts\MicroMapper\MapperInterface; use Symfonycasts\MicroMapper\MicroMapperInterface; @@ -16,7 +17,7 @@ class PartnerEntityToApiMapper implements MapperInterface { public function __construct( private MicroMapperInterface $microMapper, - private StorageInterface $storage + private FileUrlService $fileUrlService ) { } @@ -47,7 +48,7 @@ class PartnerEntityToApiMapper implements MapperInterface $dto->country = $entity->getCountry(); $dto->website = $entity->getWebsite(); $dto->logo = $entity->getLogo(); - $dto->logoUrl = $entity->getLogo() !== null ? $this->storage->resolveUri($entity->getLogo(), 'file') : null; + $dto->logoUrl = $this->fileUrlService->getFileUrl($entity->getLogo()); $dto->createdAt = $entity->getCreatedAt(); $dto->contacts = array_map(function(Contact $contact) { return $this->microMapper->map($contact, ContactApi::class, [ diff --git a/src/Serializer/MediaObjectNormalizer.php b/src/Serializer/MediaObjectNormalizer.php new file mode 100644 index 0000000..612401a --- /dev/null +++ b/src/Serializer/MediaObjectNormalizer.php @@ -0,0 +1,66 @@ + + * @date 25.01.24 + */ + + +namespace App\Serializer; + + +use App\Entity\MediaObject; +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Symfony\Component\Serializer\SerializerAwareInterface; +use Symfony\Component\Serializer\SerializerInterface; +use Vich\UploaderBundle\Storage\StorageInterface; + +#[AsDecorator('api_platform.jsonld.normalizer.item')] +final class MediaObjectNormalizer implements NormalizerInterface, SerializerAwareInterface +{ + private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED'; + + public function __construct( + private StorageInterface $storage, + private NormalizerInterface $normalizer + ) + { + } + + public function normalize($object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null + { + $context[self::ALREADY_CALLED] = true; + + if ($object instanceof MediaObject) { + // Nur für MediaObject die URI generieren + $object->contentUrl = $this->storage->resolveUri($object, 'file'); + } + + return $this->normalizer->normalize($object, $format, $context); + } + + public function supportsNormalization($data, ?string $format = null, array $context = []): bool + { + if (isset($context[self::ALREADY_CALLED])) { + return false; + } + + return $this->normalizer->supportsNormalization($data, $format, $context); + } + + public function setSerializer(SerializerInterface $serializer): void + { + if ($this->normalizer instanceof SerializerAwareInterface) { + $this->normalizer->setSerializer($serializer); + } + } + + public function getSupportedTypes(?string $format): array + { + if (method_exists($this->normalizer, 'getSupportedTypes')) { + return $this->normalizer->getSupportedTypes($format); + } + + return 'jsonld' === $format ? ['*' => true] : []; + } +} \ No newline at end of file diff --git a/src/Service/FileUrlService.php b/src/Service/FileUrlService.php new file mode 100644 index 0000000..37c39f1 --- /dev/null +++ b/src/Service/FileUrlService.php @@ -0,0 +1,35 @@ + + * @date 16.02.24 + */ + + +namespace App\Service; + + +use App\Entity\MediaObject; +use Symfony\Component\HttpFoundation\RequestStack; +use Vich\UploaderBundle\Storage\StorageInterface; + +class FileUrlService +{ + + public function __construct( + private RequestStack $requestStack, + private StorageInterface $storage, + ){} + + public function getFileUrl(?MediaObject $mediaObject) + { + if ($mediaObject !== null) { + $currentRequest = $this->requestStack->getCurrentRequest(); + if ($currentRequest !== null) { + $apiUrl = $currentRequest->getSchemeAndHttpHost(); + $imageUri = $this->storage->resolveUri($mediaObject, 'file'); + return $apiUrl . $imageUri; + } + } + return null; + } +} \ No newline at end of file