3 namespace Wallabag\CoreBundle\Event\Subscriber
;
5 use Doctrine\Common\EventSubscriber
;
6 use Doctrine\ORM\Event\LifecycleEventArgs
;
7 use Psr\Log\LoggerInterface
;
8 use Wallabag\CoreBundle\Helper\DownloadImages
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Doctrine\ORM\EntityManager
;
11 use Craue\ConfigBundle\Util\Config
;
13 class DownloadImagesSubscriber
implements EventSubscriber
16 private $downloadImages;
20 * We inject the class instead of the service otherwise it generates a circular reference with the EntityManager.
21 * So we build the service ourself when we got the EntityManager (in downloadImages).
23 public function __construct(DownloadImages
$downloadImages, $configClass, LoggerInterface
$logger)
25 $this->downloadImages
= $downloadImages;
26 $this->configClass
= $configClass;
27 $this->logger
= $logger;
30 public function getSubscribedEvents()
39 * In case of an entry has been updated.
40 * We won't update the content field if it wasn't updated.
42 * @param LifecycleEventArgs $args
44 public function preUpdate(LifecycleEventArgs
$args)
46 $entity = $args->getEntity();
48 if (!$entity instanceof Entry
) {
52 $config = new $this->configClass();
53 $config->setEntityManager($args->getEntityManager());
55 if (!$config->get('download_images_enabled')) {
59 // field content has been updated
60 if ($args->hasChangedField('content')) {
61 $html = $this->downloadImages($config, $entity);
63 if (false !== $html) {
64 $args->setNewValue('content', $html);
68 // field preview picture has been updated
69 if ($args->hasChangedField('previewPicture')) {
70 $previewPicture = $this->downloadPreviewImage($config, $entity);
72 if (false !== $previewPicture) {
73 $entity->setPreviewPicture($previewPicture);
79 * When a new entry is saved.
81 * @param LifecycleEventArgs $args
83 public function prePersist(LifecycleEventArgs
$args)
85 $entity = $args->getEntity();
87 if (!$entity instanceof Entry
) {
91 $config = new $this->configClass();
92 $config->setEntityManager($args->getEntityManager());
94 if (!$config->get('download_images_enabled')) {
98 // update all images inside the html
99 $html = $this->downloadImages($config, $entity);
100 if (false !== $html) {
101 $entity->setContent($html);
104 // update preview picture
105 $previewPicture = $this->downloadPreviewImage($config, $entity);
106 if (false !== $previewPicture) {
107 $entity->setPreviewPicture($previewPicture);
112 * Download all images from the html.
114 * @param Config $config
115 * @param Entry $entry
117 * @return string|false False in case of async
119 public function downloadImages(Config
$config, Entry
$entry)
121 $this->downloadImages
->setWallabagUrl($config->get('wallabag_url'));
123 // if ($config->get('download_images_with_rabbitmq')) {
125 // } else if ($config->get('download_images_with_redis')) {
129 return $this->downloadImages
->processHtml(
130 $entry->getContent(),
136 * Download the preview picture.
138 * @param Config $config
139 * @param Entry $entry
141 * @return string|false False in case of async
143 public function downloadPreviewImage(Config
$config, Entry
$entry)
145 $this->downloadImages
->setWallabagUrl($config->get('wallabag_url'));
147 // if ($config->get('download_images_with_rabbitmq')) {
149 // } else if ($config->get('download_images_with_redis')) {
153 return $this->downloadImages
->processSingleImage(
154 $entry->getPreviewPicture(),