X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FEvent%2FSubscriber%2FDownloadImagesSubscriber.php;h=1dd0a1a42ed56d7c510daa211c4c7cee4bc1c238;hb=f808b01692a835673f328d7221ba8c212caa9b61;hp=3f2d460c1ef7a3684f4e513336b881068070b77f;hpb=d1495dd0a456f0e35a09fb68679ee51f8d17bfe4;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php index 3f2d460c..1dd0a1a4 100644 --- a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php +++ b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php @@ -2,131 +2,100 @@ namespace Wallabag\CoreBundle\Event\Subscriber; -use Doctrine\Common\EventSubscriber; -use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\ORM\EntityManager; use Psr\Log\LoggerInterface; -use Wallabag\CoreBundle\Helper\DownloadImages; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Wallabag\CoreBundle\Entity\Entry; -use Doctrine\ORM\EntityManager; -use Craue\ConfigBundle\Util\Config; +use Wallabag\CoreBundle\Event\EntryDeletedEvent; +use Wallabag\CoreBundle\Event\EntrySavedEvent; +use Wallabag\CoreBundle\Helper\DownloadImages; -class DownloadImagesSubscriber implements EventSubscriber +class DownloadImagesSubscriber implements EventSubscriberInterface { - private $configClass; + private $em; private $downloadImages; + private $enabled; private $logger; - /** - * We inject the class instead of the service otherwise it generates a circular reference with the EntityManager. - * So we build the service ourself when we got the EntityManager (in downloadImages). - */ - public function __construct(DownloadImages $downloadImages, $configClass, LoggerInterface $logger) + public function __construct(EntityManager $em, DownloadImages $downloadImages, $enabled, LoggerInterface $logger) { + $this->em = $em; $this->downloadImages = $downloadImages; - $this->configClass = $configClass; + $this->enabled = $enabled; $this->logger = $logger; } - public function getSubscribedEvents() + public static function getSubscribedEvents() { - return array( - 'prePersist', - 'preUpdate', - ); + return [ + EntrySavedEvent::NAME => 'onEntrySaved', + EntryDeletedEvent::NAME => 'onEntryDeleted', + ]; } /** - * In case of an entry has been updated. - * We won't update the content field if it wasn't updated. + * Download images and updated the data into the entry. * - * @param LifecycleEventArgs $args + * @param EntrySavedEvent $event */ - public function preUpdate(LifecycleEventArgs $args) + public function onEntrySaved(EntrySavedEvent $event) { - $entity = $args->getEntity(); + if (!$this->enabled) { + $this->logger->debug('DownloadImagesSubscriber: disabled.'); - if (!$entity instanceof Entry) { return; } - $config = new $this->configClass(); - $config->setEntityManager($args->getEntityManager()); + $entry = $event->getEntry(); - if (!$config->get('download_images_enabled')) { - return; - } - - // field content has been updated - if ($args->hasChangedField('content')) { - $html = $this->downloadImages($config, $entity); + $html = $this->downloadImages($entry); + if (false !== $html) { + $this->logger->debug('DownloadImagesSubscriber: updated html.'); - if (false !== $html) { - $args->setNewValue('content', $html); - } + $entry->setContent($html); } - // field preview picture has been updated - if ($args->hasChangedField('previewPicture')) { - $previewPicture = $this->downloadPreviewImage($config, $entity); + // update preview picture + $previewPicture = $this->downloadPreviewImage($entry); + if (false !== $previewPicture) { + $this->logger->debug('DownloadImagesSubscriber: update preview picture.'); - if (false !== $previewPicture) { - $entity->setPreviewPicture($previewPicture); - } + $entry->setPreviewPicture($previewPicture); } + + $this->em->persist($entry); + $this->em->flush(); } /** - * When a new entry is saved. + * Remove images related to the entry. * - * @param LifecycleEventArgs $args + * @param EntryDeletedEvent $event */ - public function prePersist(LifecycleEventArgs $args) + public function onEntryDeleted(EntryDeletedEvent $event) { - $entity = $args->getEntity(); - - if (!$entity instanceof Entry) { - return; - } + if (!$this->enabled) { + $this->logger->debug('DownloadImagesSubscriber: disabled.'); - $config = new $this->configClass(); - $config->setEntityManager($args->getEntityManager()); - - if (!$config->get('download_images_enabled')) { return; } - // update all images inside the html - $html = $this->downloadImages($config, $entity); - if (false !== $html) { - $entity->setContent($html); - } - - // update preview picture - $previewPicture = $this->downloadPreviewImage($config, $entity); - if (false !== $previewPicture) { - $entity->setPreviewPicture($previewPicture); - } + $this->downloadImages->removeImages($event->getEntry()->getId()); } /** * Download all images from the html. * - * @param Config $config - * @param Entry $entry + * @todo If we want to add async download, it should be done in that method + * + * @param Entry $entry * * @return string|false False in case of async */ - public function downloadImages(Config $config, Entry $entry) + private function downloadImages(Entry $entry) { - $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); - - // if ($config->get('download_images_with_rabbitmq')) { - - // } else if ($config->get('download_images_with_redis')) { - - // } - return $this->downloadImages->processHtml( + $entry->getId(), $entry->getContent(), $entry->getUrl() ); @@ -135,22 +104,16 @@ class DownloadImagesSubscriber implements EventSubscriber /** * Download the preview picture. * - * @param Config $config - * @param Entry $entry + * @todo If we want to add async download, it should be done in that method + * + * @param Entry $entry * * @return string|false False in case of async */ - public function downloadPreviewImage(Config $config, Entry $entry) + private function downloadPreviewImage(Entry $entry) { - $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); - - // if ($config->get('download_images_with_rabbitmq')) { - - // } else if ($config->get('download_images_with_redis')) { - - // } - return $this->downloadImages->processSingleImage( + $entry->getId(), $entry->getPreviewPicture(), $entry->getUrl() );