]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
Ability to enable/disable downloading images
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / DownloadImagesSubscriber.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Event\Subscriber;
4
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;
12
13 class DownloadImagesSubscriber implements EventSubscriber
14 {
15 private $configClass;
16 private $downloadImages;
17 private $logger;
18
19 /**
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).
22 */
23 public function __construct(DownloadImages $downloadImages, $configClass, LoggerInterface $logger)
24 {
25 $this->downloadImages = $downloadImages;
26 $this->configClass = $configClass;
27 $this->logger = $logger;
28 }
29
30 public function getSubscribedEvents()
31 {
32 return array(
33 'prePersist',
34 'preUpdate',
35 );
36 }
37
38 /**
39 * In case of an entry has been updated.
40 * We won't update the content field if it wasn't updated.
41 *
42 * @param LifecycleEventArgs $args
43 */
44 public function preUpdate(LifecycleEventArgs $args)
45 {
46 $entity = $args->getEntity();
47
48 if (!$entity instanceof Entry) {
49 return;
50 }
51
52 $config = new $this->configClass();
53 $config->setEntityManager($args->getEntityManager());
54
55 if (!$config->get('download_images_enabled')) {
56 return;
57 }
58
59 // field content has been updated
60 if ($args->hasChangedField('content')) {
61 $html = $this->downloadImages($config, $entity);
62
63 if (false !== $html) {
64 $args->setNewValue('content', $html);
65 }
66 }
67
68 // field preview picture has been updated
69 if ($args->hasChangedField('previewPicture')) {
70 $previewPicture = $this->downloadPreviewImage($config, $entity);
71
72 if (false !== $previewPicture) {
73 $entity->setPreviewPicture($previewPicture);
74 }
75 }
76 }
77
78 /**
79 * When a new entry is saved.
80 *
81 * @param LifecycleEventArgs $args
82 */
83 public function prePersist(LifecycleEventArgs $args)
84 {
85 $entity = $args->getEntity();
86
87 if (!$entity instanceof Entry) {
88 return;
89 }
90
91 $config = new $this->configClass();
92 $config->setEntityManager($args->getEntityManager());
93
94 if (!$config->get('download_images_enabled')) {
95 return;
96 }
97
98 // update all images inside the html
99 $html = $this->downloadImages($config, $entity);
100 if (false !== $html) {
101 $entity->setContent($html);
102 }
103
104 // update preview picture
105 $previewPicture = $this->downloadPreviewImage($config, $entity);
106 if (false !== $previewPicture) {
107 $entity->setPreviewPicture($previewPicture);
108 }
109 }
110
111 /**
112 * Download all images from the html.
113 *
114 * @param Config $config
115 * @param Entry $entry
116 *
117 * @return string|false False in case of async
118 */
119 public function downloadImages(Config $config, Entry $entry)
120 {
121 $this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
122
123 // if ($config->get('download_images_with_rabbitmq')) {
124
125 // } else if ($config->get('download_images_with_redis')) {
126
127 // }
128
129 return $this->downloadImages->processHtml(
130 $entry->getContent(),
131 $entry->getUrl()
132 );
133 }
134
135 /**
136 * Download the preview picture.
137 *
138 * @param Config $config
139 * @param Entry $entry
140 *
141 * @return string|false False in case of async
142 */
143 public function downloadPreviewImage(Config $config, Entry $entry)
144 {
145 $this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
146
147 // if ($config->get('download_images_with_rabbitmq')) {
148
149 // } else if ($config->get('download_images_with_redis')) {
150
151 // }
152
153 return $this->downloadImages->processSingleImage(
154 $entry->getPreviewPicture(),
155 $entry->getUrl()
156 );
157 }
158 }