1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-06 17:41:01 +00:00
wallabag/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php

151 lines
4.1 KiB
PHP
Raw Normal View History

2016-10-30 10:48:29 +01:00
<?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;
}
2016-10-30 11:27:09 +01:00
$config = new $this->configClass();
$config->setEntityManager($args->getEntityManager());
2016-10-30 10:48:29 +01:00
if (!$config->get('download_images_enabled')) {
return;
}
2016-10-30 10:48:29 +01:00
// field content has been updated
if ($args->hasChangedField('content')) {
2016-10-30 11:27:09 +01:00
$html = $this->downloadImages($config, $entity);
2016-10-30 10:48:29 +01:00
2016-10-30 11:27:09 +01:00
if (false !== $html) {
2016-10-30 10:48:29 +01:00
$args->setNewValue('content', $html);
}
}
// field preview picture has been updated
if ($args->hasChangedField('previewPicture')) {
2016-10-30 11:27:09 +01:00
$previewPicture = $this->downloadPreviewImage($config, $entity);
2016-10-30 10:48:29 +01:00
2016-10-30 11:27:09 +01:00
if (false !== $previewPicture) {
2016-10-30 10:48:29 +01:00
$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());
if (!$config->get('download_images_enabled')) {
return;
}
2016-10-30 10:48:29 +01:00
// update all images inside the html
$html = $this->downloadImages($config, $entity);
2016-10-30 11:27:09 +01:00
if (false !== $html) {
2016-10-30 10:48:29 +01:00
$entity->setContent($html);
}
// update preview picture
$previewPicture = $this->downloadPreviewImage($config, $entity);
2016-10-30 11:27:09 +01:00
if (false !== $previewPicture) {
2016-10-30 10:48:29 +01:00
$entity->setPreviewPicture($previewPicture);
}
}
2016-10-30 11:27:09 +01:00
/**
* Download all images from the html.
*
2016-10-31 13:29:33 +01:00
* @todo If we want to add async download, it should be done in that method
*
2016-10-30 11:27:09 +01:00
* @param Config $config
* @param Entry $entry
*
* @return string|false False in case of async
*/
2016-10-30 10:48:29 +01:00
public function downloadImages(Config $config, Entry $entry)
{
$this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
2016-10-30 10:48:29 +01:00
return $this->downloadImages->processHtml(
$entry->getContent(),
$entry->getUrl()
);
}
2016-10-30 11:27:09 +01:00
/**
* Download the preview picture.
*
2016-10-31 13:29:33 +01:00
* @todo If we want to add async download, it should be done in that method
*
2016-10-30 11:27:09 +01:00
* @param Config $config
* @param Entry $entry
*
* @return string|false False in case of async
*/
2016-10-30 10:48:29 +01:00
public function downloadPreviewImage(Config $config, Entry $entry)
{
$this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
2016-10-30 10:48:29 +01:00
return $this->downloadImages->processSingleImage(
$entry->getPreviewPicture(),
$entry->getUrl()
);
}
}