Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Liip-Imagine/Gaufrette media integration #457

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/Controller/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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://' . $this->getParameter('bitbag_sylius_cms_plugin.uploader.filesystem') . '/' . $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());

Expand Down
8 changes: 3 additions & 5 deletions src/Controller/ResourceDataProcessingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ 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);
}

Expand All @@ -76,8 +74,8 @@ private function setFileContentsAsMediaPath(MediaInterface $media, string $fileC
private function getMediaPathIfNotNull(MediaInterface $media): string
{
Assert::string($media->getPath());
Assert::string($this->getParameter('sylius_core.public_dir'));
Assert::string($this->getParameter('bitbag_sylius_cms_plugin.uploader.filesystem'));

return $this->getParameter('sylius_core.public_dir') . $media->getPath();
return 'gaufrette://' . $this->getParameter('bitbag_sylius_cms_plugin.uploader.filesystem') . '/' . $media->getPath();
}
}
3 changes: 3 additions & 0 deletions src/Entity/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class Media implements MediaInterface
/** @var int|null */
protected $height;

/** @var array */
public $imaginePaths = [];

/** @var bool */
protected $saveWithOriginalName = false;

Expand Down
27 changes: 27 additions & 0 deletions src/EventListener/CmsMediaListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\EventListener;

use BitBag\SyliusCmsPlugin\Entity\Media;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Webmozart\Assert\Assert;

class CmsMediaListener
{
private CacheManager $imagineCacheManager;

public function __construct(
CacheManager $imagineCacheManager
) {
$this->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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/serializer/Entity.Media.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ BitBag\SyliusCmsPlugin\Entity\Media:
expose: true
type: string
groups: [Autocomplete]
imaginePaths:
expose: true
type: array
groups: [Autocomplete]
relations:
-
rel: self
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 7 additions & 1 deletion src/Resources/views/Shop/Media/Show/image.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<h2>{{ media.name|raw }}</h2>

<img class="ui fluid image" src="{{ media.path }}" alt="{{ media.name }}"
{% if page.image.path starts with 'data:' %}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change throws 500 error on main page.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show me the 500?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @David-Crty. I also noticed this 500 error on fresh install with default fixtures.
2024-05-22_09-18

{% set src = page.image.path %}
{% else %}
{% set src = page.image.path|imagine_filter('sylius_large') %}
{% endif %}

<img class="ui fluid image" src="{{ src }}" alt="{{ media.name }}"
{% if media.width is not null %} width="{{ media.width }}" {% endif %}
{% if media.height is not null %} height="{{ media.height }}" {% endif %}
/>
Expand Down
7 changes: 6 additions & 1 deletion src/Resources/views/Shop/Page/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
<div class="ui grid center aligned">
<div class="eight wide column">
{% if page.image %}
<img class="page-image ui fluid image" src="{{ page.image.path }}" alt="">
{% if page.image.path starts with 'data:' %}
{% set src = page.image.path %}
{% else %}
{% set src = page.image.path|imagine_filter('sylius_large') %}
{% endif %}
<img class="page-image ui fluid image" src="{{ src }}" alt="">
{% endif %}
</div>
</div>
Expand Down
7 changes: 6 additions & 1 deletion src/Resources/views/Shop/Product/_pagesBySection.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
<div class="column">
<div class="ui fluid segment column">
{% if page.image %}
<img class="ui fluid image" src="{{ page.image.path }}" alt="{{ 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 %}
<img class="ui fluid image" src="{{ src }}" alt="{{ page.nameWhenLinked }}">
{% endif %}

<h3>
Expand Down
2 changes: 1 addition & 1 deletion src/Uploader/MediaUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function upload(MediaInterface $media, string $pathPrefix): void
$path = $this->expandPath($hash . '.' . $file->guessExtension(), $pathPrefix, $originalName);
} 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()));
Expand Down