/
NeosAssetSource.php
232 lines (207 loc) · 7.09 KB
/
NeosAssetSource.php
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
namespace Neos\Media\Domain\Model\AssetSource\Neos;
/*
* This file is part of the Neos.Media package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Http\HttpRequestHandlerInterface;
use GuzzleHttp\Psr7\Uri;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxyRepositoryInterface;
use Neos\Media\Domain\Model\AssetSource\AssetSourceInterface;
use Neos\Media\Domain\Service\AssetService;
use Neos\Media\Domain\Service\ThumbnailService;
use Neos\Flow\Annotations as Flow;
use Neos\Media\Exception\AssetServiceException;
use Neos\Media\Exception\ThumbnailServiceException;
use Psr\Http\Message\UriInterface;
/**
* Asset source for Neos native assets
*/
final class NeosAssetSource implements AssetSourceInterface
{
/**
* @var string
*/
private $assetSourceIdentifier;
/**
* @var NeosAssetProxyRepository
*/
private $assetProxyRepository;
/**
* @Flow\Inject
* @var ThumbnailService
*/
protected $thumbnailService;
/**
* @Flow\Inject
* @var AssetService
*/
protected $assetService;
/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;
/**
* @Flow\InjectConfiguration(path="asyncThumbnails")
* @var bool
*/
protected $asyncThumbnails;
/**
* @Flow\Inject
* @var ResourceManager
*/
protected $resourceManager;
/**
* @var string
*/
protected $iconPath;
/**
* @var string
*/
protected $description = '';
/**
* @param string $assetSourceIdentifier
* @param array $assetSourceOptions
*/
public function __construct(string $assetSourceIdentifier, array $assetSourceOptions)
{
if (preg_match('/^[a-z][a-z0-9-]{0,62}[a-z]$/', $assetSourceIdentifier) !== 1) {
throw new \InvalidArgumentException(sprintf('Invalid asset source identifier "%s". The identifier must match /^[a-z][a-z0-9-]{0,62}[a-z]$/', $assetSourceIdentifier), 1513329665);
}
$this->assetSourceIdentifier = $assetSourceIdentifier;
foreach ($assetSourceOptions as $optionName => $optionValue) {
switch ($optionName) {
case 'asyncThumbnails':
// If the option value is empty, preserve the default value injected from the Neos:Media:asyncThumbnails setting:
if (!empty($optionValue)) {
if (!is_bool($optionValue)) {
throw new \InvalidArgumentException(sprintf('Asset source option "%s" specified for Neos asset source "%s" must be either true or false. Please check your settings.', $optionName, $assetSourceIdentifier), 1522927471);
}
$this->asyncThumbnails = $optionValue;
}
break;
case 'icon':
$this->iconPath = $optionValue;
break;
case 'description':
$this->description = $optionValue;
break;
default:
throw new \InvalidArgumentException(sprintf('Unknown asset source option "%s" specified for Neos asset source "%s". Please check your settings.', $optionName, $assetSourceIdentifier), 1513327774);
}
}
}
/**
* @param string $assetSourceIdentifier
* @param array $assetSourceOptions
* @return AssetSourceInterface
*/
public static function createFromConfiguration(string $assetSourceIdentifier, array $assetSourceOptions): AssetSourceInterface
{
return new static($assetSourceIdentifier, $assetSourceOptions);
}
/**
* @return string
*/
public function getIdentifier(): string
{
return $this->assetSourceIdentifier;
}
/**
* @return string
*/
public function getLabel(): string
{
return 'Neos';
}
/**
* Returns the resource path to Assetsources icon
*
* @return string
*/
public function getIconUri(): string
{
return $this->resourceManager->getPublicPackageResourceUriByPath($this->iconPath);
}
/**
*
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
/**
* @return AssetProxyRepositoryInterface
*/
public function getAssetProxyRepository(): AssetProxyRepositoryInterface
{
if ($this->assetProxyRepository === null) {
$this->assetProxyRepository = new NeosAssetProxyRepository($this);
}
return $this->assetProxyRepository;
}
/**
* @return bool
*/
public function isReadOnly(): bool
{
return false;
}
/**
* Internal method used by NeosAssetProxy
*
* @param AssetInterface $asset
* @return Uri|null
* @throws AssetServiceException
* @throws ThumbnailServiceException
* @throws \Neos\Flow\Http\Exception
* @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
*/
public function getThumbnailUriForAsset(AssetInterface $asset): ?UriInterface
{
$actionRequest = ($this->asyncThumbnails ? $this->createActionRequest() : null);
$thumbnailConfiguration = $this->thumbnailService->getThumbnailConfigurationForPreset('Neos.Media.Browser:Thumbnail', ($actionRequest !== null));
$thumbnailData = $this->assetService->getThumbnailUriAndSizeForAsset($asset, $thumbnailConfiguration, $actionRequest);
return isset($thumbnailData['src']) ? new Uri($thumbnailData['src']) : null;
}
/**
* Internal method used by NeosAssetProxy
*
* @param AssetInterface $asset
* @return Uri|null
* @throws AssetServiceException
* @throws ThumbnailServiceException
* @throws \Neos\Flow\Http\Exception
* @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
*/
public function getPreviewUriForAsset(AssetInterface $asset): ?UriInterface
{
$actionRequest = ($this->asyncThumbnails ? $this->createActionRequest() : null);
$thumbnailConfiguration = $this->thumbnailService->getThumbnailConfigurationForPreset('Neos.Media.Browser:Preview', ($actionRequest !== null));
$thumbnailData = $this->assetService->getThumbnailUriAndSizeForAsset($asset, $thumbnailConfiguration, $actionRequest);
return isset($thumbnailData['src']) ? new Uri($thumbnailData['src']) : null;
}
/**
* @return ActionRequest|null
*/
private function createActionRequest(): ?ActionRequest
{
$requestHandler = $this->bootstrap->getActiveRequestHandler();
if ($requestHandler instanceof HttpRequestHandlerInterface) {
return ActionRequest::fromHttpRequest($requestHandler->getHttpRequest());
}
return null;
}
}