1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\Core\Action; |
3
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; |
5
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
7
|
|
|
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException; |
8
|
|
|
use Shlinkio\Shlink\Common\Service\PreviewGenerator; |
9
|
|
|
use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface; |
10
|
|
|
use Shlinkio\Shlink\Common\Util\ResponseUtilsTrait; |
11
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
12
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener; |
13
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; |
14
|
|
|
use Zend\Stratigility\MiddlewareInterface; |
15
|
|
|
|
16
|
|
|
class PreviewAction implements MiddlewareInterface |
17
|
|
|
{ |
18
|
|
|
use ResponseUtilsTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var PreviewGeneratorInterface |
22
|
|
|
*/ |
23
|
|
|
private $previewGenerator; |
24
|
|
|
/** |
25
|
|
|
* @var UrlShortenerInterface |
26
|
|
|
*/ |
27
|
|
|
private $urlShortener; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* PreviewAction constructor. |
31
|
|
|
* @param PreviewGeneratorInterface $previewGenerator |
32
|
|
|
* @param UrlShortenerInterface $urlShortener |
33
|
|
|
* |
34
|
|
|
* @Inject({PreviewGenerator::class, UrlShortener::class}) |
35
|
|
|
*/ |
36
|
|
|
public function __construct(PreviewGeneratorInterface $previewGenerator, UrlShortenerInterface $urlShortener) |
37
|
|
|
{ |
38
|
|
|
$this->previewGenerator = $previewGenerator; |
39
|
|
|
$this->urlShortener = $urlShortener; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Process an incoming request and/or response. |
44
|
|
|
* |
45
|
|
|
* Accepts a server-side request and a response instance, and does |
46
|
|
|
* something with them. |
47
|
|
|
* |
48
|
|
|
* If the response is not complete and/or further processing would not |
49
|
|
|
* interfere with the work done in the middleware, or if the middleware |
50
|
|
|
* wants to delegate to another process, it can use the `$out` callable |
51
|
|
|
* if present. |
52
|
|
|
* |
53
|
|
|
* If the middleware does not return a value, execution of the current |
54
|
|
|
* request is considered complete, and the response instance provided will |
55
|
|
|
* be considered the response to return. |
56
|
|
|
* |
57
|
|
|
* Alternately, the middleware may return a response instance. |
58
|
|
|
* |
59
|
|
|
* Often, middleware will `return $out();`, with the assumption that a |
60
|
|
|
* later middleware will return a response. |
61
|
|
|
* |
62
|
|
|
* @param Request $request |
63
|
|
|
* @param Response $response |
64
|
|
|
* @param null|callable $out |
65
|
|
|
* @return null|Response |
66
|
|
|
*/ |
67
|
|
|
public function __invoke(Request $request, Response $response, callable $out = null) |
68
|
|
|
{ |
69
|
|
|
$shortCode = $request->getAttribute('shortCode'); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$url = $this->urlShortener->shortCodeToUrl($shortCode); |
73
|
|
|
if (! isset($url)) { |
74
|
|
|
return $out($request, $response->withStatus(404), 'Not found'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$imagePath = $this->previewGenerator->generatePreview($url); |
78
|
|
|
return $this->generateImageResponse($imagePath); |
79
|
|
|
} catch (InvalidShortCodeException $e) { |
80
|
|
|
return $out($request, $response->withStatus(404), 'Not found'); |
81
|
|
|
} catch (PreviewGenerationException $e) { |
82
|
|
|
return $out($request, $response->withStatus(500), 'Preview generation error'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|