mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Added wkhtmltopdf stuff and created preview generator service
This commit is contained in:
@@ -2,14 +2,16 @@
|
||||
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
|
||||
use Doctrine\Common\Cache\Cache;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use mikehaertl\wkhtmlto\Image;
|
||||
use Monolog\Logger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\Common\Factory\CacheFactory;
|
||||
use Shlinkio\Shlink\Common\Factory\EntityManagerFactory;
|
||||
use Shlinkio\Shlink\Common\Factory\LoggerFactory;
|
||||
use Shlinkio\Shlink\Common\Factory\TranslatorFactory;
|
||||
use Shlinkio\Shlink\Common\Image\ImageFactory;
|
||||
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware;
|
||||
use Shlinkio\Shlink\Common\Service\IpLocationResolver;
|
||||
use Shlinkio\Shlink\Common\Service;
|
||||
use Shlinkio\Shlink\Common\Twig\Extension\TranslatorExtension;
|
||||
use Zend\I18n\Translator\Translator;
|
||||
use Zend\ServiceManager\Factory\InvokableFactory;
|
||||
@@ -22,12 +24,14 @@ return [
|
||||
GuzzleHttp\Client::class => InvokableFactory::class,
|
||||
Cache::class => CacheFactory::class,
|
||||
'Logger_Shlink' => LoggerFactory::class,
|
||||
Image::class => ImageFactory::class,
|
||||
|
||||
Translator::class => TranslatorFactory::class,
|
||||
TranslatorExtension::class => AnnotatedFactory::class,
|
||||
LocaleMiddleware::class => AnnotatedFactory::class,
|
||||
|
||||
IpLocationResolver::class => AnnotatedFactory::class,
|
||||
Service\IpLocationResolver::class => AnnotatedFactory::class,
|
||||
Service\PreviewGenerator::class => AnnotatedFactory::class,
|
||||
],
|
||||
'aliases' => [
|
||||
'em' => EntityManager::class,
|
||||
|
||||
30
module/Common/src/Image/ImageFactory.php
Normal file
30
module/Common/src/Image/ImageFactory.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Image;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Interop\Container\Exception\ContainerException;
|
||||
use mikehaertl\wkhtmlto\Image;
|
||||
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||
|
||||
class ImageFactory implements FactoryInterface
|
||||
{
|
||||
/**
|
||||
* Create an object
|
||||
*
|
||||
* @param ContainerInterface $container
|
||||
* @param string $requestedName
|
||||
* @param null|array $options
|
||||
* @return object
|
||||
* @throws ServiceNotFoundException if unable to resolve the service.
|
||||
* @throws ServiceNotCreatedException if an exception is raised when
|
||||
* creating a service.
|
||||
* @throws ContainerException if any other error occurs
|
||||
*/
|
||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
||||
{
|
||||
$config = $container->get('config')['phpwkhtmltopdf'];
|
||||
return new Image(isset($config['images']) ? $config['images'] : null);
|
||||
}
|
||||
}
|
||||
58
module/Common/src/Service/PreviewGenerator.php
Normal file
58
module/Common/src/Service/PreviewGenerator.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Service;
|
||||
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Doctrine\Common\Cache\Cache;
|
||||
use mikehaertl\wkhtmlto\Image;
|
||||
|
||||
class PreviewGenerator implements PreviewGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* @var Image
|
||||
*/
|
||||
private $image;
|
||||
/**
|
||||
* @var Cache
|
||||
*/
|
||||
private $cache;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $location;
|
||||
|
||||
/**
|
||||
* PreviewGenerator constructor.
|
||||
* @param Image $image
|
||||
* @param Cache $cache
|
||||
* @param string $location
|
||||
*
|
||||
* @Inject({Image::class, Cache::class, "config.phpwkhtmltopdf.images.files_location"})
|
||||
*/
|
||||
public function __construct(Image $image, Cache $cache, $location)
|
||||
{
|
||||
$this->image = $image;
|
||||
$this->cache = $cache;
|
||||
$this->location = $location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates and stores preview for provided website and returns the path to the image file
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
public function generatePreview($url)
|
||||
{
|
||||
$cacheId = sprintf('preview_%s.png', urlencode($url));
|
||||
if ($this->cache->contains($cacheId)) {
|
||||
return $this->cache->fetch($cacheId);
|
||||
}
|
||||
|
||||
$path = $this->location . '/' . $cacheId;
|
||||
$this->image->setPage($url);
|
||||
$this->image->saveAs($path);
|
||||
$this->cache->save($cacheId, $path);
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
13
module/Common/src/Service/PreviewGeneratorInterface.php
Normal file
13
module/Common/src/Service/PreviewGeneratorInterface.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Service;
|
||||
|
||||
interface PreviewGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generates and stores preview for provided website and returns the path to the image file
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
public function generatePreview($url);
|
||||
}
|
||||
60
module/Common/test/Service/PreviewGeneratorTest.php
Normal file
60
module/Common/test/Service/PreviewGeneratorTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace ShlinkioTest\Shlink\Common\Service;
|
||||
|
||||
use Doctrine\Common\Cache\ArrayCache;
|
||||
use mikehaertl\wkhtmlto\Image;
|
||||
use PHPUnit_Framework_TestCase as TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
||||
|
||||
class PreviewGeneratorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var PreviewGenerator
|
||||
*/
|
||||
protected $generator;
|
||||
/**
|
||||
* @var ObjectProphecy
|
||||
*/
|
||||
protected $image;
|
||||
/**
|
||||
* @var ArrayCache
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->image = $this->prophesize(Image::class);
|
||||
$this->cache = new ArrayCache();
|
||||
$this->generator = new PreviewGenerator($this->image->reveal(), $this->cache, 'dir');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function alreadyCachedElementsAreNotProcessed()
|
||||
{
|
||||
$url = 'http://foo.com';
|
||||
$this->cache->save(sprintf('preview_%s.png', urlencode($url)), 'dir/foo.png');
|
||||
$this->image->saveAs(Argument::cetera())->shouldBeCalledTimes(0);
|
||||
$this->assertEquals('dir/foo.png', $this->generator->generatePreview($url));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function nonCachedElementsAreProcessedAndThenCached()
|
||||
{
|
||||
$url = 'http://foo.com';
|
||||
$cacheId = sprintf('preview_%s.png', urlencode($url));
|
||||
$expectedPath = 'dir/' . $cacheId;
|
||||
|
||||
$this->image->setPage($url)->shouldBeCalledTimes(1);
|
||||
$this->image->saveAs($expectedPath)->shouldBeCalledTimes(1);
|
||||
|
||||
$this->assertFalse($this->cache->contains($cacheId));
|
||||
$this->assertEquals($expectedPath, $this->generator->generatePreview($url));
|
||||
$this->assertTrue($this->cache->contains($cacheId));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user