]>
Commit | Line | Data |
---|---|---|
7f559418 JB |
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 | ||
48656e0e JB |
52 | $config = new $this->configClass(); |
53 | $config->setEntityManager($args->getEntityManager()); | |
7f559418 | 54 | |
d1495dd0 JB |
55 | if (!$config->get('download_images_enabled')) { |
56 | return; | |
57 | } | |
58 | ||
7f559418 JB |
59 | // field content has been updated |
60 | if ($args->hasChangedField('content')) { | |
48656e0e | 61 | $html = $this->downloadImages($config, $entity); |
7f559418 | 62 | |
48656e0e | 63 | if (false !== $html) { |
7f559418 JB |
64 | $args->setNewValue('content', $html); |
65 | } | |
66 | } | |
67 | ||
68 | // field preview picture has been updated | |
69 | if ($args->hasChangedField('previewPicture')) { | |
48656e0e | 70 | $previewPicture = $this->downloadPreviewImage($config, $entity); |
7f559418 | 71 | |
48656e0e | 72 | if (false !== $previewPicture) { |
7f559418 JB |
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 | ||
d1495dd0 JB |
94 | if (!$config->get('download_images_enabled')) { |
95 | return; | |
96 | } | |
97 | ||
7f559418 JB |
98 | // update all images inside the html |
99 | $html = $this->downloadImages($config, $entity); | |
48656e0e | 100 | if (false !== $html) { |
7f559418 JB |
101 | $entity->setContent($html); |
102 | } | |
103 | ||
104 | // update preview picture | |
105 | $previewPicture = $this->downloadPreviewImage($config, $entity); | |
48656e0e | 106 | if (false !== $previewPicture) { |
7f559418 JB |
107 | $entity->setPreviewPicture($previewPicture); |
108 | } | |
109 | } | |
110 | ||
48656e0e JB |
111 | /** |
112 | * Download all images from the html. | |
113 | * | |
aedd6ca0 JB |
114 | * @todo If we want to add async download, it should be done in that method |
115 | * | |
48656e0e JB |
116 | * @param Config $config |
117 | * @param Entry $entry | |
118 | * | |
119 | * @return string|false False in case of async | |
120 | */ | |
7f559418 JB |
121 | public function downloadImages(Config $config, Entry $entry) |
122 | { | |
41ada277 JB |
123 | $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); |
124 | ||
7f559418 JB |
125 | return $this->downloadImages->processHtml( |
126 | $entry->getContent(), | |
127 | $entry->getUrl() | |
128 | ); | |
129 | } | |
130 | ||
48656e0e JB |
131 | /** |
132 | * Download the preview picture. | |
133 | * | |
aedd6ca0 JB |
134 | * @todo If we want to add async download, it should be done in that method |
135 | * | |
48656e0e JB |
136 | * @param Config $config |
137 | * @param Entry $entry | |
138 | * | |
139 | * @return string|false False in case of async | |
140 | */ | |
7f559418 JB |
141 | public function downloadPreviewImage(Config $config, Entry $entry) |
142 | { | |
41ada277 JB |
143 | $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); |
144 | ||
7f559418 JB |
145 | return $this->downloadImages->processSingleImage( |
146 | $entry->getPreviewPicture(), | |
147 | $entry->getUrl() | |
148 | ); | |
149 | } | |
150 | } |