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 // field content has been updated
56 if ($args->hasChangedField('content')) {
57 $html = $this->downloadImages($config, $entity);
59 if (false !== $html) {
60 $args->setNewValue('content', $html);
64 // field preview picture has been updated
65 if ($args->hasChangedField('previewPicture')) {
66 $previewPicture = $this->downloadPreviewImage($config, $entity);
68 if (false !== $previewPicture) {
69 $entity->setPreviewPicture($previewPicture);
75 * When a new entry is saved.
77 * @param LifecycleEventArgs $args
79 public function prePersist(LifecycleEventArgs
$args)
81 $entity = $args->getEntity();
83 if (!$entity instanceof Entry
) {
87 $config = new $this->configClass();
88 $config->setEntityManager($args->getEntityManager());
90 // update all images inside the html
91 $html = $this->downloadImages($config, $entity);
92 if (false !== $html) {
93 $entity->setContent($html);
96 // update preview picture
97 $previewPicture = $this->downloadPreviewImage($config, $entity);
98 if (false !== $previewPicture) {
99 $entity->setPreviewPicture($previewPicture);
104 * Download all images from the html.
106 * @param Config $config
107 * @param Entry $entry
109 * @return string|false False in case of async
111 public function downloadImages(Config
$config, Entry
$entry)
113 // if ($config->get('download_images_with_rabbitmq')) {
115 // } else if ($config->get('download_images_with_redis')) {
119 return $this->downloadImages
->processHtml(
120 $entry->getContent(),
126 * Download the preview picture.
128 * @param Config $config
129 * @param Entry $entry
131 * @return string|false False in case of async
133 public function downloadPreviewImage(Config
$config, Entry
$entry)
135 // if ($config->get('download_images_with_rabbitmq')) {
137 // } else if ($config->get('download_images_with_redis')) {
141 return $this->downloadImages
->processSingleImage(
142 $entry->getPreviewPicture(),