blob: ef8d7d3bcf9bd9361947d2ac745969e2e11d37da (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
<?php
namespace Wallabag\CoreBundle\Event\Subscriber;
use Doctrine\ORM\EntityManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
use Wallabag\CoreBundle\Event\EntrySavedEvent;
use Wallabag\CoreBundle\Helper\DownloadImages;
class DownloadImagesSubscriber implements EventSubscriberInterface
{
private $em;
private $downloadImages;
private $enabled;
private $logger;
public function __construct(EntityManager $em, DownloadImages $downloadImages, $enabled, LoggerInterface $logger)
{
$this->em = $em;
$this->downloadImages = $downloadImages;
$this->enabled = $enabled;
$this->logger = $logger;
}
public static function getSubscribedEvents()
{
return [
EntrySavedEvent::NAME => 'onEntrySaved',
EntryDeletedEvent::NAME => 'onEntryDeleted',
];
}
/**
* Download images and updated the data into the entry.
*/
public function onEntrySaved(EntrySavedEvent $event)
{
if (!$this->enabled) {
$this->logger->debug('DownloadImagesSubscriber: disabled.');
return;
}
$entry = $event->getEntry();
$html = $this->downloadImages($entry);
if (false !== $html) {
$this->logger->debug('DownloadImagesSubscriber: updated html.');
$entry->setContent($html);
}
// update preview picture
$previewPicture = $this->downloadPreviewImage($entry);
if (false !== $previewPicture) {
$this->logger->debug('DownloadImagesSubscriber: update preview picture.');
$entry->setPreviewPicture($previewPicture);
}
$this->em->persist($entry);
$this->em->flush();
}
/**
* Remove images related to the entry.
*/
public function onEntryDeleted(EntryDeletedEvent $event)
{
if (!$this->enabled) {
$this->logger->debug('DownloadImagesSubscriber: disabled.');
return;
}
$this->downloadImages->removeImages($event->getEntry()->getId());
}
/**
* Download all images from the html.
*
* @todo If we want to add async download, it should be done in that method
*
* @return string|false False in case of async
*/
private function downloadImages(Entry $entry)
{
return $this->downloadImages->processHtml(
$entry->getId(),
$entry->getContent(),
$entry->getUrl()
);
}
/**
* Download the preview picture.
*
* @todo If we want to add async download, it should be done in that method
*
* @return string|false False in case of async
*/
private function downloadPreviewImage(Entry $entry)
{
return $this->downloadImages->processSingleImage(
$entry->getId(),
$entry->getPreviewPicture(),
$entry->getUrl()
);
}
}
|