-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalImageServer.php
More file actions
157 lines (128 loc) 路 5.16 KB
/
Copy pathLocalImageServer.php
File metadata and controls
157 lines (128 loc) 路 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace SixtyEightPublishers\ImageStorage\ImageServer;
use SixtyEightPublishers\FileStorage\Config\ConfigInterface;
use SixtyEightPublishers\FileStorage\Exception\FileNotFoundException;
use SixtyEightPublishers\ImageStorage\Config\Config;
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
use SixtyEightPublishers\ImageStorage\Exception\ResponseException;
use SixtyEightPublishers\ImageStorage\Exception\SignatureException;
use SixtyEightPublishers\ImageStorage\ImageStorageInterface;
use SixtyEightPublishers\ImageStorage\PathInfoInterface;
use SixtyEightPublishers\ImageStorage\Persistence\ImagePersisterInterface;
use Throwable;
use function assert;
use function count;
use function explode;
use function implode;
use function is_string;
use function ltrim;
use function strlen;
use function strncmp;
use function substr;
final class LocalImageServer implements ImageServerInterface
{
public function __construct(
private readonly ImageStorageInterface $imageStorage,
private readonly ResponseFactoryInterface $responseFactory,
) {}
public function getImageResponse(RequestInterface $request): object
{
try {
return $this->processRequest($request);
} catch (FileNotFoundException $e) {
$e = new ResponseException('Source file not found.', 404, $e);
} catch (SignatureException $e) {
$e = new ResponseException($e->getMessage(), 403, $e);
} catch (Throwable $e) {
$e = new ResponseException('Internal server error. ' . $e->getMessage(), 500, $e);
}
return $this->responseFactory->createErrorResponse($e, $this->imageStorage->getConfig());
}
/**
* @throws FileNotFoundException
* @throws \SixtyEightPublishers\FileStorage\Exception\FilesystemException
* @throws SignatureException
*/
private function processRequest(RequestInterface $request): object
{
$path = $this->stripBasePath($request->getUrlPath());
$this->validateSignature($request, $path);
$pathInfo = $this->createPathInfo($path);
try {
$path = $this->getFilePath($pathInfo);
} catch (FileNotFoundException $e) {
$modifiers = $pathInfo->getModifiers();
try {
$noImageInfo = $this->imageStorage->resolveNoImage(
$pathInfo->withModifiers(null)->getPath(),
);
} catch (InvalidArgumentException $_) {
throw $e;
}
$noImageInfo = $noImageInfo->withExtension($pathInfo->getExtension());
$path = $this->getFilePath($noImageInfo->withModifiers($modifiers));
}
return $this->responseFactory->createImageResponse(
$this->imageStorage->getFilesystem(),
ImagePersisterInterface::FILESYSTEM_PREFIX_CACHE . $path,
$this->imageStorage->getConfig(),
);
}
private function stripBasePath(string $path): string
{
$path = ltrim($path, '/');
$basePath = $this->imageStorage->getConfig()[ConfigInterface::BASE_PATH];
assert(is_string($basePath));
if (!empty($basePath) && 0 === strncmp($path, $basePath, $basePathLength = strlen($basePath))) {
$path = ltrim(substr($path, $basePathLength), '/');
}
return $path;
}
/**
* @throws InvalidArgumentException
*/
private function createPathInfo(string $path): PathInfoInterface
{
$parts = explode('/', $path);
if (2 > ($pathCount = count($parts))) {
throw new InvalidArgumentException('Missing modifier in requested path.');
}
$modifiers = $parts[$pathCount -2];
unset($parts[$pathCount - 2]);
$pathInfo = $this->imageStorage->createPathInfo(implode('/', $parts));
if (null === $pathInfo->getExtension()) {
throw new InvalidArgumentException('Missing file extension in requested path.');
}
return $pathInfo->withEncodedModifiers($modifiers);
}
/**
* @throws FileNotFoundException
* @throws \SixtyEightPublishers\FileStorage\Exception\FilesystemException
*/
private function getFilePath(PathInfoInterface $pathInfo): string
{
if (true === $this->imageStorage->exists($pathInfo)) {
return $pathInfo->getPath();
}
return $this->imageStorage->save(
$this->imageStorage->createResource($pathInfo),
);
}
/**
* @throws SignatureException
*/
private function validateSignature(RequestInterface $request, string $path): void
{
$signatureStrategy = $this->imageStorage->getSignatureStrategy();
if (null === $signatureStrategy) {
return;
}
$signatureParameterName = $this->imageStorage->getConfig()[Config::SIGNATURE_PARAMETER_NAME];
assert(is_string($signatureParameterName));
$token = (string) ($request->getQueryParameter($signatureParameterName) ?? ''); # @phpstan-ignore-line
if (!$signatureStrategy->verifyToken($token, $path)) {
throw new SignatureException('Request contains invalid signature.');
}
}
}