aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
blob: 654edf31078184e1e13e46dba4b4de85a966e596 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php

namespace Wallabag\CoreBundle\Event\Subscriber;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Psr\Log\LoggerInterface;
use Wallabag\CoreBundle\Helper\DownloadImages;
use Wallabag\CoreBundle\Entity\Entry;
use Doctrine\ORM\EntityManager;
use Craue\ConfigBundle\Util\Config;

class DownloadImagesSubscriber implements EventSubscriber
{
    private $configClass;
    private $downloadImages;
    private $logger;

    /**
     * We inject the class instead of the service otherwise it generates a circular reference with the EntityManager.
     * So we build the service ourself when we got the EntityManager (in downloadImages).
     */
    public function __construct(DownloadImages $downloadImages, $configClass, LoggerInterface $logger)
    {
        $this->downloadImages = $downloadImages;
        $this->configClass = $configClass;
        $this->logger = $logger;
    }

    public function getSubscribedEvents()
    {
        return array(
            'prePersist',
            'preUpdate',
        );
    }

    /**
     * In case of an entry has been updated.
     * We won't update the content field if it wasn't updated.
     *
     * @param LifecycleEventArgs $args
     */
    public function preUpdate(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if (!$entity instanceof Entry) {
            return;
        }

        $em = $args->getEntityManager();

        // field content has been updated
        if ($args->hasChangedField('content')) {
            $html = $this->downloadImages($em, $entity);

            if (null !== $html) {
                $args->setNewValue('content', $html);
            }
        }

        // field preview picture has been updated
        if ($args->hasChangedField('previewPicture')) {
            $previewPicture = $this->downloadPreviewImage($em, $entity);

            if (null !== $previewPicture) {
                $entity->setPreviewPicture($previewPicture);
            }
        }
    }

    /**
     * When a new entry is saved.
     *
     * @param LifecycleEventArgs $args
     */
    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if (!$entity instanceof Entry) {
            return;
        }

        $config = new $this->configClass();
        $config->setEntityManager($args->getEntityManager());

        // update all images inside the html
        $html = $this->downloadImages($config, $entity);
        if (null !== $html) {
            $entity->setContent($html);
        }

        // update preview picture
        $previewPicture = $this->downloadPreviewImage($config, $entity);
        if (null !== $previewPicture) {
            $entity->setPreviewPicture($previewPicture);
        }
    }

    public function downloadImages(Config $config, Entry $entry)
    {
        // if ($config->get('download_images_with_rabbitmq')) {

        // } else if ($config->get('download_images_with_redis')) {

        // }

        return $this->downloadImages->processHtml(
            $entry->getContent(),
            $entry->getUrl()
        );
    }

    public function downloadPreviewImage(Config $config, Entry $entry)
    {
        // if ($config->get('download_images_with_rabbitmq')) {

        // } else if ($config->get('download_images_with_redis')) {

        // }

        return $this->downloadImages->processSingleImage(
            $entry->getPreviewPicture(),
            $entry->getUrl()
        );
    }
}