Skip to content

Commit

Permalink
Better integration of gaufrette and liip imagine
Browse files Browse the repository at this point in the history
fixing path prefix for gaufrette
  • Loading branch information
David-Crty committed Dec 13, 2022
1 parent 6858520 commit 42faada
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 26 deletions.
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
17 changes: 2 additions & 15 deletions src/Controller/ResourceDataProcessingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
}
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 = [];

public function __construct()
{
$this->initializeTranslationsCollection();
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:' %}
{% 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 @@ -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()));
Expand Down

0 comments on commit 42faada

Please sign in to comment.