From 41ada277f066ea57947bce05bcda63962b7fea55 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 30 Oct 2016 19:50:00 +0100 Subject: [PATCH] Add instance url to the downloaded images --- .../Event/Subscriber/DownloadImagesSubscriber.php | 4 ++++ src/Wallabag/CoreBundle/Helper/DownloadImages.php | 14 +++++++++++++- .../CoreBundle/Helper/DownloadImagesTest.php | 4 +++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php index 09f8e9119..0792653e0 100644 --- a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php +++ b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php @@ -110,6 +110,8 @@ class DownloadImagesSubscriber implements EventSubscriber */ public function downloadImages(Config $config, Entry $entry) { + $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); + // if ($config->get('download_images_with_rabbitmq')) { // } else if ($config->get('download_images_with_redis')) { @@ -132,6 +134,8 @@ class DownloadImagesSubscriber implements EventSubscriber */ public function downloadPreviewImage(Config $config, Entry $entry) { + $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); + // if ($config->get('download_images_with_rabbitmq')) { // } else if ($config->get('download_images_with_redis')) { diff --git a/src/Wallabag/CoreBundle/Helper/DownloadImages.php b/src/Wallabag/CoreBundle/Helper/DownloadImages.php index 004bb2775..e7982c560 100644 --- a/src/Wallabag/CoreBundle/Helper/DownloadImages.php +++ b/src/Wallabag/CoreBundle/Helper/DownloadImages.php @@ -15,6 +15,7 @@ class DownloadImages private $baseFolder; private $logger; private $mimeGuesser; + private $wallabagUrl; public function __construct(Client $client, $baseFolder, LoggerInterface $logger) { @@ -26,6 +27,17 @@ class DownloadImages $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. */ @@ -143,7 +155,7 @@ class DownloadImages imagedestroy($im); - return '/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext; + return $this->wallabagUrl.'/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext; } /** diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php index e000d6817..33d2e389c 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php @@ -27,9 +27,11 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase $logger = new Logger('test', array($logHandler)); $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger); + $download->setWallabagUrl('http://wallabag.io/'); + $res = $download->processHtml('
', 'http://imgur.com/gallery/WxtWY'); - $this->assertContains('/assets/images/4/2/4258f71e/c638b4c2.png', $res); + $this->assertContains('http://wallabag.io/assets/images/4/2/4258f71e/c638b4c2.png', $res); } public function testProcessHtmlWithBadImage()