Add instance url to the downloaded images

This commit is contained in:
Jeremy Benoist 2016-10-30 19:50:00 +01:00
parent 48656e0eaa
commit 41ada277f0
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
3 changed files with 20 additions and 2 deletions

View file

@ -110,6 +110,8 @@ class DownloadImagesSubscriber implements EventSubscriber
*/ */
public function downloadImages(Config $config, Entry $entry) public function downloadImages(Config $config, Entry $entry)
{ {
$this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
// if ($config->get('download_images_with_rabbitmq')) { // if ($config->get('download_images_with_rabbitmq')) {
// } else if ($config->get('download_images_with_redis')) { // } else if ($config->get('download_images_with_redis')) {
@ -132,6 +134,8 @@ class DownloadImagesSubscriber implements EventSubscriber
*/ */
public function downloadPreviewImage(Config $config, Entry $entry) public function downloadPreviewImage(Config $config, Entry $entry)
{ {
$this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
// if ($config->get('download_images_with_rabbitmq')) { // if ($config->get('download_images_with_rabbitmq')) {
// } else if ($config->get('download_images_with_redis')) { // } else if ($config->get('download_images_with_redis')) {

View file

@ -15,6 +15,7 @@ class DownloadImages
private $baseFolder; private $baseFolder;
private $logger; private $logger;
private $mimeGuesser; private $mimeGuesser;
private $wallabagUrl;
public function __construct(Client $client, $baseFolder, LoggerInterface $logger) public function __construct(Client $client, $baseFolder, LoggerInterface $logger)
{ {
@ -26,6 +27,17 @@ class DownloadImages
$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.
*/ */
@ -143,7 +155,7 @@ class DownloadImages
imagedestroy($im); imagedestroy($im);
return '/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext; return $this->wallabagUrl.'/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext;
} }
/** /**

View file

@ -27,9 +27,11 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase
$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', $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('<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', '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() public function testProcessHtmlWithBadImage()