mirror of
https://github.com/wallabag/wallabag.git
synced 2024-10-31 22:28:54 +00:00
Use custom event instead of Doctrine ones
This give us ability to use Entry ID to determine where to store images and it’s then more easy to remove them when we remove the entry.
This commit is contained in:
parent
aedd6ca0fd
commit
e0597476d1
8 changed files with 211 additions and 135 deletions
|
@ -13,6 +13,8 @@ use Wallabag\CoreBundle\Form\Type\EntryFilterType;
|
||||||
use Wallabag\CoreBundle\Form\Type\EditEntryType;
|
use Wallabag\CoreBundle\Form\Type\EditEntryType;
|
||||||
use Wallabag\CoreBundle\Form\Type\NewEntryType;
|
use Wallabag\CoreBundle\Form\Type\NewEntryType;
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
||||||
|
use Wallabag\CoreBundle\Event\EntrySavedEvent;
|
||||||
|
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
|
||||||
|
|
||||||
class EntryController extends Controller
|
class EntryController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -81,6 +83,9 @@ class EntryController extends Controller
|
||||||
$em->persist($entry);
|
$em->persist($entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
|
// entry saved, dispatch event about it!
|
||||||
|
$this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||||
|
|
||||||
return $this->redirect($this->generateUrl('homepage'));
|
return $this->redirect($this->generateUrl('homepage'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +112,9 @@ class EntryController extends Controller
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$em->persist($entry);
|
$em->persist($entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
|
// entry saved, dispatch event about it!
|
||||||
|
$this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->redirect($this->generateUrl('homepage'));
|
return $this->redirect($this->generateUrl('homepage'));
|
||||||
|
@ -343,6 +351,9 @@ class EntryController extends Controller
|
||||||
$em->persist($entry);
|
$em->persist($entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
|
// entry saved, dispatch event about it!
|
||||||
|
$this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||||
|
|
||||||
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
|
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -431,6 +442,9 @@ class EntryController extends Controller
|
||||||
UrlGeneratorInterface::ABSOLUTE_PATH
|
UrlGeneratorInterface::ABSOLUTE_PATH
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// entry deleted, dispatch event about it!
|
||||||
|
$this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
|
||||||
|
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$em->remove($entry);
|
$em->remove($entry);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
26
src/Wallabag/CoreBundle/Event/EntryDeletedEvent.php
Normal file
26
src/Wallabag/CoreBundle/Event/EntryDeletedEvent.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Wallabag\CoreBundle\Event;
|
||||||
|
|
||||||
|
use Symfony\Component\EventDispatcher\Event;
|
||||||
|
use Wallabag\CoreBundle\Entity\Entry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This event is fired as soon as an entry is deleted.
|
||||||
|
*/
|
||||||
|
class EntryDeletedEvent extends Event
|
||||||
|
{
|
||||||
|
const NAME = 'entry.deleted';
|
||||||
|
|
||||||
|
protected $entry;
|
||||||
|
|
||||||
|
public function __construct(Entry $entry)
|
||||||
|
{
|
||||||
|
$this->entry = $entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEntry()
|
||||||
|
{
|
||||||
|
return $this->entry;
|
||||||
|
}
|
||||||
|
}
|
26
src/Wallabag/CoreBundle/Event/EntrySavedEvent.php
Normal file
26
src/Wallabag/CoreBundle/Event/EntrySavedEvent.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Wallabag\CoreBundle\Event;
|
||||||
|
|
||||||
|
use Symfony\Component\EventDispatcher\Event;
|
||||||
|
use Wallabag\CoreBundle\Entity\Entry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This event is fired as soon as an entry was saved.
|
||||||
|
*/
|
||||||
|
class EntrySavedEvent extends Event
|
||||||
|
{
|
||||||
|
const NAME = 'entry.saved';
|
||||||
|
|
||||||
|
protected $entry;
|
||||||
|
|
||||||
|
public function __construct(Entry $entry)
|
||||||
|
{
|
||||||
|
$this->entry = $entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEntry()
|
||||||
|
{
|
||||||
|
return $this->entry;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,110 +2,85 @@
|
||||||
|
|
||||||
namespace Wallabag\CoreBundle\Event\Subscriber;
|
namespace Wallabag\CoreBundle\Event\Subscriber;
|
||||||
|
|
||||||
use Doctrine\Common\EventSubscriber;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Wallabag\CoreBundle\Helper\DownloadImages;
|
use Wallabag\CoreBundle\Helper\DownloadImages;
|
||||||
use Wallabag\CoreBundle\Entity\Entry;
|
use Wallabag\CoreBundle\Entity\Entry;
|
||||||
|
use Wallabag\CoreBundle\Event\EntrySavedEvent;
|
||||||
|
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
use Craue\ConfigBundle\Util\Config;
|
|
||||||
|
|
||||||
class DownloadImagesSubscriber implements EventSubscriber
|
class DownloadImagesSubscriber implements EventSubscriberInterface
|
||||||
{
|
{
|
||||||
private $configClass;
|
private $em;
|
||||||
private $downloadImages;
|
private $downloadImages;
|
||||||
|
private $enabled;
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
/**
|
public function __construct(EntityManager $em, DownloadImages $downloadImages, $enabled, LoggerInterface $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->em = $em;
|
||||||
$this->downloadImages = $downloadImages;
|
$this->downloadImages = $downloadImages;
|
||||||
$this->configClass = $configClass;
|
$this->enabled = $enabled;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubscribedEvents()
|
public static function getSubscribedEvents()
|
||||||
{
|
{
|
||||||
return array(
|
return [
|
||||||
'prePersist',
|
EntrySavedEvent::NAME => 'onEntrySaved',
|
||||||
'preUpdate',
|
EntryDeletedEvent::NAME => 'onEntryDeleted',
|
||||||
);
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In case of an entry has been updated.
|
* Download images and updated the data into the entry.
|
||||||
* We won't update the content field if it wasn't updated.
|
|
||||||
*
|
*
|
||||||
* @param LifecycleEventArgs $args
|
* @param EntrySavedEvent $event
|
||||||
*/
|
*/
|
||||||
public function preUpdate(LifecycleEventArgs $args)
|
public function onEntrySaved(EntrySavedEvent $event)
|
||||||
{
|
{
|
||||||
$entity = $args->getEntity();
|
if (!$this->enabled) {
|
||||||
|
$this->logger->debug('DownloadImagesSubscriber: disabled.');
|
||||||
|
|
||||||
if (!$entity instanceof Entry) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = new $this->configClass();
|
$entry = $event->getEntry();
|
||||||
$config->setEntityManager($args->getEntityManager());
|
|
||||||
|
|
||||||
if (!$config->get('download_images_enabled')) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// field content has been updated
|
|
||||||
if ($args->hasChangedField('content')) {
|
|
||||||
$html = $this->downloadImages($config, $entity);
|
|
||||||
|
|
||||||
|
$html = $this->downloadImages($entry);
|
||||||
if (false !== $html) {
|
if (false !== $html) {
|
||||||
$args->setNewValue('content', $html);
|
$this->logger->debug('DownloadImagesSubscriber: updated html.');
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// field preview picture has been updated
|
$entry->setContent($html);
|
||||||
if ($args->hasChangedField('previewPicture')) {
|
|
||||||
$previewPicture = $this->downloadPreviewImage($config, $entity);
|
|
||||||
|
|
||||||
if (false !== $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());
|
|
||||||
|
|
||||||
if (!$config->get('download_images_enabled')) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// update all images inside the html
|
|
||||||
$html = $this->downloadImages($config, $entity);
|
|
||||||
if (false !== $html) {
|
|
||||||
$entity->setContent($html);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// update preview picture
|
// update preview picture
|
||||||
$previewPicture = $this->downloadPreviewImage($config, $entity);
|
$previewPicture = $this->downloadPreviewImage($entry);
|
||||||
if (false !== $previewPicture) {
|
if (false !== $previewPicture) {
|
||||||
$entity->setPreviewPicture($previewPicture);
|
$this->logger->debug('DownloadImagesSubscriber: update preview picture.');
|
||||||
|
|
||||||
|
$entry->setPreviewPicture($previewPicture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->em->persist($entry);
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove images related to the entry.
|
||||||
|
*
|
||||||
|
* @param EntryDeletedEvent $event
|
||||||
|
*/
|
||||||
|
public function onEntryDeleted(EntryDeletedEvent $event)
|
||||||
|
{
|
||||||
|
if (!$this->enabled) {
|
||||||
|
$this->logger->debug('DownloadImagesSubscriber: disabled.');
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->downloadImages->removeImages($event->getEntry()->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,16 +88,14 @@ class DownloadImagesSubscriber implements EventSubscriber
|
||||||
*
|
*
|
||||||
* @todo If we want to add async download, it should be done in that method
|
* @todo If we want to add async download, it should be done in that method
|
||||||
*
|
*
|
||||||
* @param Config $config
|
|
||||||
* @param Entry $entry
|
* @param Entry $entry
|
||||||
*
|
*
|
||||||
* @return string|false False in case of async
|
* @return string|false False in case of async
|
||||||
*/
|
*/
|
||||||
public function downloadImages(Config $config, Entry $entry)
|
private function downloadImages(Entry $entry)
|
||||||
{
|
{
|
||||||
$this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
|
|
||||||
|
|
||||||
return $this->downloadImages->processHtml(
|
return $this->downloadImages->processHtml(
|
||||||
|
$entry->getId(),
|
||||||
$entry->getContent(),
|
$entry->getContent(),
|
||||||
$entry->getUrl()
|
$entry->getUrl()
|
||||||
);
|
);
|
||||||
|
@ -133,16 +106,14 @@ class DownloadImagesSubscriber implements EventSubscriber
|
||||||
*
|
*
|
||||||
* @todo If we want to add async download, it should be done in that method
|
* @todo If we want to add async download, it should be done in that method
|
||||||
*
|
*
|
||||||
* @param Config $config
|
|
||||||
* @param Entry $entry
|
* @param Entry $entry
|
||||||
*
|
*
|
||||||
* @return string|false False in case of async
|
* @return string|false False in case of async
|
||||||
*/
|
*/
|
||||||
public function downloadPreviewImage(Config $config, Entry $entry)
|
private function downloadPreviewImage(Entry $entry)
|
||||||
{
|
{
|
||||||
$this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
|
|
||||||
|
|
||||||
return $this->downloadImages->processSingleImage(
|
return $this->downloadImages->processSingleImage(
|
||||||
|
$entry->getId(),
|
||||||
$entry->getPreviewPicture(),
|
$entry->getPreviewPicture(),
|
||||||
$entry->getUrl()
|
$entry->getUrl()
|
||||||
);
|
);
|
||||||
|
|
|
@ -6,6 +6,7 @@ use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\DomCrawler\Crawler;
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
|
||||||
|
use Symfony\Component\Finder\Finder;
|
||||||
|
|
||||||
class DownloadImages
|
class DownloadImages
|
||||||
{
|
{
|
||||||
|
@ -17,27 +18,17 @@ class DownloadImages
|
||||||
private $mimeGuesser;
|
private $mimeGuesser;
|
||||||
private $wallabagUrl;
|
private $wallabagUrl;
|
||||||
|
|
||||||
public function __construct(Client $client, $baseFolder, LoggerInterface $logger)
|
public function __construct(Client $client, $baseFolder, $wallabagUrl, LoggerInterface $logger)
|
||||||
{
|
{
|
||||||
$this->client = $client;
|
$this->client = $client;
|
||||||
$this->baseFolder = $baseFolder;
|
$this->baseFolder = $baseFolder;
|
||||||
|
$this->wallabagUrl = rtrim($wallabagUrl, '/');
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->mimeGuesser = new MimeTypeExtensionGuesser();
|
$this->mimeGuesser = new MimeTypeExtensionGuesser();
|
||||||
|
|
||||||
$this->setFolder();
|
$this->setFolder();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Since we can't inject CraueConfig service because it'll generate a circular reference when injected in the subscriber
|
|
||||||
* we use a different way to inject the current wallabag url.
|
|
||||||
*
|
|
||||||
* @param string $url Usually from `$config->get('wallabag_url')`
|
|
||||||
*/
|
|
||||||
public function setWallabagUrl($url)
|
|
||||||
{
|
|
||||||
$this->wallabagUrl = rtrim($url, '/');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup base folder where all images are going to be saved.
|
* Setup base folder where all images are going to be saved.
|
||||||
*/
|
*/
|
||||||
|
@ -52,23 +43,24 @@ class DownloadImages
|
||||||
/**
|
/**
|
||||||
* Process the html and extract image from it, save them to local and return the updated html.
|
* Process the html and extract image from it, save them to local and return the updated html.
|
||||||
*
|
*
|
||||||
|
* @param int $entryId ID of the entry
|
||||||
* @param string $html
|
* @param string $html
|
||||||
* @param string $url Used as a base path for relative image and folder
|
* @param string $url Used as a base path for relative image and folder
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function processHtml($html, $url)
|
public function processHtml($entryId, $html, $url)
|
||||||
{
|
{
|
||||||
$crawler = new Crawler($html);
|
$crawler = new Crawler($html);
|
||||||
$result = $crawler
|
$result = $crawler
|
||||||
->filterXpath('//img')
|
->filterXpath('//img')
|
||||||
->extract(array('src'));
|
->extract(array('src'));
|
||||||
|
|
||||||
$relativePath = $this->getRelativePath($url);
|
$relativePath = $this->getRelativePath($entryId);
|
||||||
|
|
||||||
// download and save the image to the folder
|
// download and save the image to the folder
|
||||||
foreach ($result as $image) {
|
foreach ($result as $image) {
|
||||||
$imagePath = $this->processSingleImage($image, $url, $relativePath);
|
$imagePath = $this->processSingleImage($entryId, $image, $url, $relativePath);
|
||||||
|
|
||||||
if (false === $imagePath) {
|
if (false === $imagePath) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -86,24 +78,27 @@ class DownloadImages
|
||||||
* - re-saved it (for security reason)
|
* - re-saved it (for security reason)
|
||||||
* - return the new local path.
|
* - return the new local path.
|
||||||
*
|
*
|
||||||
|
* @param int $entryId ID of the entry
|
||||||
* @param string $imagePath Path to the image to retrieve
|
* @param string $imagePath Path to the image to retrieve
|
||||||
* @param string $url Url from where the image were found
|
* @param string $url Url from where the image were found
|
||||||
* @param string $relativePath Relative local path to saved the image
|
* @param string $relativePath Relative local path to saved the image
|
||||||
*
|
*
|
||||||
* @return string Relative url to access the image from the web
|
* @return string Relative url to access the image from the web
|
||||||
*/
|
*/
|
||||||
public function processSingleImage($imagePath, $url, $relativePath = null)
|
public function processSingleImage($entryId, $imagePath, $url, $relativePath = null)
|
||||||
{
|
{
|
||||||
if (null == $relativePath) {
|
if (null === $relativePath) {
|
||||||
$relativePath = $this->getRelativePath($url);
|
$relativePath = $this->getRelativePath($entryId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->logger->debug('DownloadImages: working on image: '.$imagePath);
|
||||||
|
|
||||||
$folderPath = $this->baseFolder.'/'.$relativePath;
|
$folderPath = $this->baseFolder.'/'.$relativePath;
|
||||||
|
|
||||||
// build image path
|
// build image path
|
||||||
$absolutePath = $this->getAbsoluteLink($url, $imagePath);
|
$absolutePath = $this->getAbsoluteLink($url, $imagePath);
|
||||||
if (false === $absolutePath) {
|
if (false === $absolutePath) {
|
||||||
$this->logger->log('error', 'Can not determine the absolute path for that image, skipping.');
|
$this->logger->error('DownloadImages: Can not determine the absolute path for that image, skipping.');
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -111,15 +106,15 @@ class DownloadImages
|
||||||
try {
|
try {
|
||||||
$res = $this->client->get($absolutePath);
|
$res = $this->client->get($absolutePath);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->logger->log('error', 'Can not retrieve image, skipping.', ['exception' => $e]);
|
$this->logger->error('DownloadImages: Can not retrieve image, skipping.', ['exception' => $e]);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$ext = $this->mimeGuesser->guess($res->getHeader('content-type'));
|
$ext = $this->mimeGuesser->guess($res->getHeader('content-type'));
|
||||||
$this->logger->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]);
|
$this->logger->debug('DownloadImages: Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]);
|
||||||
if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
|
if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
|
||||||
$this->logger->log('error', 'Processed image with not allowed extension. Skipping '.$imagePath);
|
$this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping '.$imagePath);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -133,7 +128,7 @@ class DownloadImages
|
||||||
}
|
}
|
||||||
|
|
||||||
if (false === $im) {
|
if (false === $im) {
|
||||||
$this->logger->log('error', 'Error while regenerating image', ['path' => $localPath]);
|
$this->logger->error('DownloadImages: Error while regenerating image', ['path' => $localPath]);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -141,16 +136,16 @@ class DownloadImages
|
||||||
switch ($ext) {
|
switch ($ext) {
|
||||||
case 'gif':
|
case 'gif':
|
||||||
$result = imagegif($im, $localPath);
|
$result = imagegif($im, $localPath);
|
||||||
$this->logger->log('debug', 'Re-creating gif');
|
$this->logger->debug('DownloadImages: Re-creating gif');
|
||||||
break;
|
break;
|
||||||
case 'jpeg':
|
case 'jpeg':
|
||||||
case 'jpg':
|
case 'jpg':
|
||||||
$result = imagejpeg($im, $localPath, self::REGENERATE_PICTURES_QUALITY);
|
$result = imagejpeg($im, $localPath, self::REGENERATE_PICTURES_QUALITY);
|
||||||
$this->logger->log('debug', 'Re-creating jpg');
|
$this->logger->debug('DownloadImages: Re-creating jpg');
|
||||||
break;
|
break;
|
||||||
case 'png':
|
case 'png':
|
||||||
$result = imagepng($im, $localPath, ceil(self::REGENERATE_PICTURES_QUALITY / 100 * 9));
|
$result = imagepng($im, $localPath, ceil(self::REGENERATE_PICTURES_QUALITY / 100 * 9));
|
||||||
$this->logger->log('debug', 'Re-creating png');
|
$this->logger->debug('DownloadImages: Re-creating png');
|
||||||
}
|
}
|
||||||
|
|
||||||
imagedestroy($im);
|
imagedestroy($im);
|
||||||
|
@ -158,24 +153,47 @@ class DownloadImages
|
||||||
return $this->wallabagUrl.'/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext;
|
return $this->wallabagUrl.'/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all images for the given entry id.
|
||||||
|
*
|
||||||
|
* @param int $entryId ID of the entry
|
||||||
|
*/
|
||||||
|
public function removeImages($entryId)
|
||||||
|
{
|
||||||
|
$relativePath = $this->getRelativePath($entryId);
|
||||||
|
$folderPath = $this->baseFolder.'/'.$relativePath;
|
||||||
|
|
||||||
|
$finder = new Finder();
|
||||||
|
$finder
|
||||||
|
->files()
|
||||||
|
->ignoreDotFiles(true)
|
||||||
|
->in($folderPath);
|
||||||
|
|
||||||
|
foreach ($finder as $file) {
|
||||||
|
@unlink($file->getRealPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
@rmdir($folderPath);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate the folder where we are going to save images based on the entry url.
|
* Generate the folder where we are going to save images based on the entry url.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param int $entryId ID of the entry
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function getRelativePath($url)
|
private function getRelativePath($entryId)
|
||||||
{
|
{
|
||||||
$hashUrl = hash('crc32', $url);
|
$hashId = hash('crc32', $entryId);
|
||||||
$relativePath = $hashUrl[0].'/'.$hashUrl[1].'/'.$hashUrl;
|
$relativePath = $hashId[0].'/'.$hashId[1].'/'.$hashId;
|
||||||
$folderPath = $this->baseFolder.'/'.$relativePath;
|
$folderPath = $this->baseFolder.'/'.$relativePath;
|
||||||
|
|
||||||
if (!file_exists($folderPath)) {
|
if (!file_exists($folderPath)) {
|
||||||
mkdir($folderPath, 0777, true);
|
mkdir($folderPath, 0777, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->logger->log('debug', 'Folder used for that url', ['folder' => $folderPath, 'url' => $url]);
|
$this->logger->debug('DownloadImages: Folder used for that Entry id', ['folder' => $folderPath, 'entryId' => $entryId]);
|
||||||
|
|
||||||
return $relativePath;
|
return $relativePath;
|
||||||
}
|
}
|
||||||
|
@ -208,7 +226,7 @@ class DownloadImages
|
||||||
return $absolute->get_uri();
|
return $absolute->get_uri();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->logger->log('error', 'Can not make an absolute link', ['base' => $base, 'url' => $url]);
|
$this->logger->error('DownloadImages: Can not make an absolute link', ['base' => $base, 'url' => $url]);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,17 +140,19 @@ services:
|
||||||
wallabag_core.subscriber.download_images:
|
wallabag_core.subscriber.download_images:
|
||||||
class: Wallabag\CoreBundle\Event\Subscriber\DownloadImagesSubscriber
|
class: Wallabag\CoreBundle\Event\Subscriber\DownloadImagesSubscriber
|
||||||
arguments:
|
arguments:
|
||||||
|
- "@doctrine.orm.default_entity_manager"
|
||||||
- "@wallabag_core.entry.download_images"
|
- "@wallabag_core.entry.download_images"
|
||||||
- "%craue_config.config.class%"
|
- '@=service(''craue_config'').get(''download_images_enabled'')'
|
||||||
- "@logger"
|
- "@logger"
|
||||||
tags:
|
tags:
|
||||||
- { name: doctrine.event_subscriber }
|
- { name: kernel.event_subscriber }
|
||||||
|
|
||||||
wallabag_core.entry.download_images:
|
wallabag_core.entry.download_images:
|
||||||
class: Wallabag\CoreBundle\Helper\DownloadImages
|
class: Wallabag\CoreBundle\Helper\DownloadImages
|
||||||
arguments:
|
arguments:
|
||||||
- "@wallabag_core.entry.download_images.client"
|
- "@wallabag_core.entry.download_images.client"
|
||||||
- "%kernel.root_dir%/../web/assets/images"
|
- "%kernel.root_dir%/../web/assets/images"
|
||||||
|
- '@=service(''craue_config'').get(''wallabag_url'')'
|
||||||
- "@logger"
|
- "@logger"
|
||||||
|
|
||||||
wallabag_core.entry.download_images.client:
|
wallabag_core.entry.download_images.client:
|
||||||
|
|
|
@ -869,10 +869,30 @@ class EntryControllerTest extends WallabagCoreTestCase
|
||||||
$this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $entry);
|
$this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $entry);
|
||||||
$this->assertEquals($url, $entry->getUrl());
|
$this->assertEquals($url, $entry->getUrl());
|
||||||
$this->assertContains('Perpignan', $entry->getTitle());
|
$this->assertContains('Perpignan', $entry->getTitle());
|
||||||
$this->assertContains('assets/images/8/e/8ec9229a/d9bc0fcd.jpeg', $entry->getContent());
|
$this->assertContains('/d9bc0fcd.jpeg', $entry->getContent());
|
||||||
|
|
||||||
$em->remove($entry);
|
$client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
|
||||||
$em->flush();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testNewEntryWithDownloadImagesEnabled
|
||||||
|
*/
|
||||||
|
public function testRemoveEntryWithDownloadImagesEnabled()
|
||||||
|
{
|
||||||
|
$this->logInAs('admin');
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
$url = 'http://www.20minutes.fr/montpellier/1952003-20161030-video-car-tombe-panne-rugbymen-perpignan-improvisent-melee-route';
|
||||||
|
$client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
|
||||||
|
|
||||||
|
$content = $client->getContainer()
|
||||||
|
->get('doctrine.orm.entity_manager')
|
||||||
|
->getRepository('WallabagCoreBundle:Entry')
|
||||||
|
->findByUrlAndUserId($url, $this->getLoggedInUserId());
|
||||||
|
|
||||||
|
$client->request('GET', '/delete/'.$content->getId());
|
||||||
|
|
||||||
|
$this->assertEquals(302, $client->getResponse()->getStatusCode());
|
||||||
|
|
||||||
$client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
|
$client->getContainer()->get('craue_config')->set('download_images_enabled', 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,12 +26,11 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
|
||||||
$logHandler = new TestHandler();
|
$logHandler = new TestHandler();
|
||||||
$logger = new Logger('test', array($logHandler));
|
$logger = new Logger('test', array($logHandler));
|
||||||
|
|
||||||
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger);
|
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger);
|
||||||
$download->setWallabagUrl('http://wallabag.io/');
|
|
||||||
|
|
||||||
$res = $download->processHtml('<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY');
|
$res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY');
|
||||||
|
|
||||||
$this->assertContains('http://wallabag.io/assets/images/4/2/4258f71e/c638b4c2.png', $res);
|
$this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/c638b4c2.png', $res);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testProcessHtmlWithBadImage()
|
public function testProcessHtmlWithBadImage()
|
||||||
|
@ -47,8 +46,8 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
|
||||||
$logHandler = new TestHandler();
|
$logHandler = new TestHandler();
|
||||||
$logger = new Logger('test', array($logHandler));
|
$logger = new Logger('test', array($logHandler));
|
||||||
|
|
||||||
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger);
|
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger);
|
||||||
$res = $download->processHtml('<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY');
|
$res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY');
|
||||||
|
|
||||||
$this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type');
|
$this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type');
|
||||||
}
|
}
|
||||||
|
@ -79,10 +78,10 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
|
||||||
$logHandler = new TestHandler();
|
$logHandler = new TestHandler();
|
||||||
$logger = new Logger('test', array($logHandler));
|
$logger = new Logger('test', array($logHandler));
|
||||||
|
|
||||||
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger);
|
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger);
|
||||||
$res = $download->processSingleImage('T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
$res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
||||||
|
|
||||||
$this->assertContains('/assets/images/4/2/4258f71e/ebe60399.'.$extension, $res);
|
$this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.'.$extension, $res);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testProcessSingleImageWithBadUrl()
|
public function testProcessSingleImageWithBadUrl()
|
||||||
|
@ -98,8 +97,8 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
|
||||||
$logHandler = new TestHandler();
|
$logHandler = new TestHandler();
|
||||||
$logger = new Logger('test', array($logHandler));
|
$logger = new Logger('test', array($logHandler));
|
||||||
|
|
||||||
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger);
|
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger);
|
||||||
$res = $download->processSingleImage('T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
$res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
||||||
|
|
||||||
$this->assertFalse($res, 'Image can not be found, so it will not be replaced');
|
$this->assertFalse($res, 'Image can not be found, so it will not be replaced');
|
||||||
}
|
}
|
||||||
|
@ -117,8 +116,8 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
|
||||||
$logHandler = new TestHandler();
|
$logHandler = new TestHandler();
|
||||||
$logger = new Logger('test', array($logHandler));
|
$logger = new Logger('test', array($logHandler));
|
||||||
|
|
||||||
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger);
|
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger);
|
||||||
$res = $download->processSingleImage('http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
$res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
||||||
|
|
||||||
$this->assertFalse($res, 'Image can not be loaded, so it will not be replaced');
|
$this->assertFalse($res, 'Image can not be loaded, so it will not be replaced');
|
||||||
}
|
}
|
||||||
|
@ -136,8 +135,8 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
|
||||||
$logHandler = new TestHandler();
|
$logHandler = new TestHandler();
|
||||||
$logger = new Logger('test', array($logHandler));
|
$logger = new Logger('test', array($logHandler));
|
||||||
|
|
||||||
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger);
|
$download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger);
|
||||||
$res = $download->processSingleImage('/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY');
|
$res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY');
|
||||||
|
|
||||||
$this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced');
|
$this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue