diff --git a/src/Controller/MediaController.php b/src/Controller/MediaController.php index 1044d75cc..22cd768d5 100755 --- a/src/Controller/MediaController.php +++ b/src/Controller/MediaController.php @@ -17,7 +17,6 @@ use Sylius\Bundle\ResourceBundle\Controller\ResourceController; use Sylius\Component\Resource\ResourceActions; use Symfony\Component\HttpFoundation\BinaryFileResponse; -use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; @@ -59,15 +58,13 @@ public function downloadMediaAction(Request $request): Response /** @var MediaInterface|null $media */ $media = $this->getMediaForRequestCode($configuration, $request); Assert::notNull($media); + Assert::notNull($media->getPath()); $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $media); - $mediaPath = $this->getMediaPathIfNotNull($media); - $mediaFile = new File($mediaPath); - $mediaName = $media->getDownloadName() . '.' . $mediaFile->guessExtension(); - $response = new BinaryFileResponse($mediaPath); + $response = new BinaryFileResponse('gaufrette://sylius_image/' . $media->getPath()); $response->setContentDisposition( $request->get('disposition', ResponseHeaderBag::DISPOSITION_ATTACHMENT), - $mediaName + $media->getDownloadName() . '.' . pathinfo($media->getPath(), \PATHINFO_EXTENSION), ); $response->headers->set('Content-Type', $media->getMimeType()); diff --git a/src/Controller/ResourceDataProcessingTrait.php b/src/Controller/ResourceDataProcessingTrait.php index f57bfd7c7..df2b07e2c 100644 --- a/src/Controller/ResourceDataProcessingTrait.php +++ b/src/Controller/ResourceDataProcessingTrait.php @@ -8,7 +8,6 @@ use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; use Sylius\Component\Resource\Model\ResourceInterface; use Symfony\Component\Form\FormInterface; -use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\Request; use Webmozart\Assert\Assert; @@ -51,17 +50,13 @@ private function setPathForImageMediaType(MediaInterface $media): void if (!$this->cacheManager->isStored($media->getPath(), $this::FILTER)) { $this->cacheManager->store($this->dataManager->find($this::FILTER, $media->getPath()), $media->getPath(), $this::FILTER); } - $resolvedPath = $this->cacheManager->resolve($media->getPath(), $this::FILTER); - $fileContents = file_get_contents($resolvedPath); - Assert::string($fileContents); + $fileContents = $this->dataManager->find($this::FILTER, $media->getPath())->getContent(); $this->setFileContentsAsMediaPath($media, $fileContents); } private function setPathForNonImageMediaType(MediaInterface $media): void { - $mediaPath = $this->getMediaPathIfNotNull($media); - $file = new File($mediaPath); - $fileContents = file_get_contents($file->getPathname()); + $fileContents = $this->dataManager->getLoader($this::FILTER)->find($media->getPath()); Assert::string($fileContents); $this->setFileContentsAsMediaPath($media, $fileContents); } @@ -72,12 +67,4 @@ private function setFileContentsAsMediaPath(MediaInterface $media, string $fileC $path = 'data:' . $media->getMimeType() . ';base64, ' . $base64Content; $media->setPath($path); } - - private function getMediaPathIfNotNull(MediaInterface $media): string - { - Assert::string($media->getPath()); - Assert::string($this->getParameter('sylius_core.public_dir')); - - return $this->getParameter('sylius_core.public_dir') . $media->getPath(); - } } diff --git a/src/Entity/Media.php b/src/Entity/Media.php index be2ab4a36..31ada089d 100644 --- a/src/Entity/Media.php +++ b/src/Entity/Media.php @@ -58,6 +58,9 @@ class Media implements MediaInterface /** @var int|null */ protected $height; + /** @var array */ + public $imaginePaths = []; + public function __construct() { $this->initializeTranslationsCollection(); diff --git a/src/EventListener/CmsMediaListener.php b/src/EventListener/CmsMediaListener.php new file mode 100644 index 000000000..cb5412fa4 --- /dev/null +++ b/src/EventListener/CmsMediaListener.php @@ -0,0 +1,27 @@ +imagineCacheManager = $imagineCacheManager; + } + + public function postLoad(Media $media, LifecycleEventArgs $event): void + { + Assert::notNull($media->getPath()); + $media->imaginePaths['tiny'] = $this->imagineCacheManager->resolve($media->getPath(), 'sylius_admin_product_tiny_thumbnail'); + } +} diff --git a/src/Resources/assets/admin/js/bitbag/bitbag-media-autocomplete.js b/src/Resources/assets/admin/js/bitbag/bitbag-media-autocomplete.js index 1d54148b4..0a016fa80 100644 --- a/src/Resources/assets/admin/js/bitbag/bitbag-media-autocomplete.js +++ b/src/Resources/assets/admin/js/bitbag/bitbag-media-autocomplete.js @@ -147,7 +147,7 @@ export class HandleAutoComplete { const selectMenu = mediaContainer.querySelector(this.selectMenu); selectMenu.innerHTML = ''; arr?.forEach((item) => { - selectMenu.insertAdjacentHTML('beforeend', this._itemTemplate(item.path, item.code.trim())); + selectMenu.insertAdjacentHTML('beforeend', this._itemTemplate(item.imaginePaths.tiny, item.code.trim())); }); triggerCustomEvent(mediaContainer, 'cms.media.display.update.end'); } diff --git a/src/Resources/config/serializer/Entity.Media.yml b/src/Resources/config/serializer/Entity.Media.yml index 8cf3defd2..26f632060 100755 --- a/src/Resources/config/serializer/Entity.Media.yml +++ b/src/Resources/config/serializer/Entity.Media.yml @@ -15,6 +15,10 @@ BitBag\SyliusCmsPlugin\Entity\Media: expose: true type: string groups: [Autocomplete] + imaginePaths: + expose: true + type: array + groups: [Autocomplete] relations: - rel: self diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index fbc0ca5fd..7dc75804a 100755 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -54,3 +54,10 @@ services: bitbag_sylius_cms_plugin.sorter.sections: class: BitBag\SyliusCmsPlugin\Sorter\SectionsSorter + + bitbag_sylius_cms_plugin.media_listener: + class: BitBag\SyliusCmsPlugin\EventListener\CmsMediaListener + tags: + - { name: 'doctrine.orm.entity_listener', event: 'postLoad', entity: 'BitBag\SyliusCmsPlugin\Entity\Media' } + arguments: + - "@liip_imagine.cache.manager" \ No newline at end of file diff --git a/src/Resources/views/Shop/Media/Show/image.html.twig b/src/Resources/views/Shop/Media/Show/image.html.twig index ec8b13b71..c884c59db 100755 --- a/src/Resources/views/Shop/Media/Show/image.html.twig +++ b/src/Resources/views/Shop/Media/Show/image.html.twig @@ -1,6 +1,12 @@

{{ media.name|raw }}

-{{ media.name }} diff --git a/src/Resources/views/Shop/Page/show.html.twig b/src/Resources/views/Shop/Page/show.html.twig index d36002715..8e957cb88 100755 --- a/src/Resources/views/Shop/Page/show.html.twig +++ b/src/Resources/views/Shop/Page/show.html.twig @@ -33,7 +33,12 @@
{% if page.image %} - + {% if page.image.path starts with 'data:' %} + {% set src = page.image.path %} + {% else %} + {% set src = page.image.path|imagine_filter('sylius_large') %} + {% endif %} + {% endif %}
diff --git a/src/Resources/views/Shop/Product/_pagesBySection.html.twig b/src/Resources/views/Shop/Product/_pagesBySection.html.twig index 8a695a5bd..b10bc139d 100644 --- a/src/Resources/views/Shop/Product/_pagesBySection.html.twig +++ b/src/Resources/views/Shop/Product/_pagesBySection.html.twig @@ -12,7 +12,12 @@
{% if page.image %} - {{ page.nameWhenLinked }} + {% if page.image.path starts with 'data:' %} + {% set src = page.image.path %} + {% else %} + {% set src = page.image.path|imagine_filter('sylius_small') %} + {% endif %} + {{ page.nameWhenLinked }} {% endif %}

diff --git a/src/Uploader/MediaUploader.php b/src/Uploader/MediaUploader.php index ee1b0e547..eb47396b2 100644 --- a/src/Uploader/MediaUploader.php +++ b/src/Uploader/MediaUploader.php @@ -41,7 +41,7 @@ public function upload(MediaInterface $media, string $pathPrefix): void $path = $this->expandPath($hash . '.' . $file->guessExtension(), $pathPrefix); } while ($this->filesystem->has($path)); - $media->setPath('/' . $path); + $media->setPath($path); $media->setMimeType($file->getMimeType()); $file = $media->getFile(); Assert::notNull($file, sprintf('File for media identified by id: "%s" is null', $media->getId()));