-
-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathFileSystemTarget.php
More file actions
447 lines (401 loc) · 16.2 KB
/
Copy pathFileSystemTarget.php
File metadata and controls
447 lines (401 loc) · 16.2 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
namespace Neos\Flow\ResourceManagement\Target;
/*
* This file is part of the Neos.Flow 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\Annotations as Flow;
use Neos\Error\Messages\Error;
use Neos\Flow\Http\BaseUriProvider;
use Neos\Flow\ResourceManagement\CollectionInterface;
use Neos\Flow\ResourceManagement\Publishing\MessageCollector;
use Neos\Flow\ResourceManagement\PersistentResource;
use Neos\Flow\ResourceManagement\ResourceMetaDataInterface;
use Neos\Flow\ResourceManagement\ResourceRepository;
use Neos\Flow\ResourceManagement\Storage\PackageStorage;
use Neos\Flow\ResourceManagement\Storage\StorageInterface;
use Neos\Utility\Files;
use Neos\Utility\Unicode\Functions as UnicodeFunctions;
use Neos\Flow\ResourceManagement\Target\Exception as TargetException;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
/**
* A target which publishes resources to a specific directory in a file system.
*/
class FileSystemTarget implements TargetInterface, AbsoluteBaseUriAwareTarget
{
/**
* @var array
*/
protected $options = [];
/**
* Name which identifies this publishing target
*
* @var string
*/
protected $name;
/**
* @var list<\Closure(int $iteration): void>
*/
protected $callbacks = [];
/**
* The path (in a filesystem) where resources are published to
*
* @var string
*/
protected $path;
/**
* The configured publicly accessible web URI which points to the root path of this target.
* Can be relative to website's base Uri, for example "_Resources/MySpecialTarget/".
* If resources should be served from a different domain, make sure to specify an absolute URI though
*
* @var string
*/
protected $baseUri = '';
/**
* The resolved absolute web URI for this target. If $baseUri was absolute this will be the same,
* otherwise the request base uri will be prepended.
*
* @var string
*/
protected string $absoluteBaseUri;
/**
* If the generated URI path segment containing the sha1 should be divided into multiple segments
*
* @var boolean
*/
protected $subdivideHashPathSegment = true;
/**
* A list of extensions that are excluded and must not be published by this target.
*
* @var array
*/
protected $excludedExtensions = [];
/**
* @Flow\Inject
* @var ResourceRepository
*/
protected $resourceRepository;
/**
* @Flow\Inject(name="Neos.Flow:SystemLogger")
* @var LoggerInterface
*/
protected $logger;
/**
* @Flow\Inject
* @var MessageCollector
*/
protected $messageCollector;
/**
* Constructor
*
* @param string $name Name of this target instance, according to the resource settings
* @param array $options Options for this target
*/
public function __construct($name, array $options = [])
{
$this->name = $name;
$this->options = $options;
}
public function setAbsoluteBaseUri(UriInterface $baseUri): void
{
if (($this->baseUri[0] ?? '') === '/' || str_contains($this->baseUri, '://')) {
$this->absoluteBaseUri = $this->baseUri;
return;
}
$this->absoluteBaseUri = (string)$baseUri . $this->baseUri;
}
/**
* Injects the (system) logger based on PSR-3.
*
* @param LoggerInterface $logger
* @return void
*/
public function injectLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Initializes this resource publishing target
*
* @return void
* @throws TargetException
*/
public function initializeObject()
{
foreach ($this->options as $key => $value) {
$isOptionSet = $this->setOption($key, $value);
if (!$isOptionSet) {
throw new TargetException(sprintf('An unknown option "%s" was specified in the configuration of a resource FileSystemTarget. Please check your settings.', $key), 1361525952);
}
}
if (!is_writable($this->path)) {
@Files::createDirectoryRecursively($this->path);
}
if (!is_dir($this->path) && !is_link($this->path)) {
throw new TargetException('The directory "' . $this->path . '" which was configured as a publishing target does not exist and could not be created.', 1207124538);
}
if (!is_writable($this->path)) {
throw new TargetException('The directory "' . $this->path . '" which was configured as a publishing target is not writable.', 1207124546);
}
}
/**
* Returns the name of this target instance
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param \Closure(int $iteration): void $callback Function called after each resource publishing
*/
public function onPublish(\Closure $callback): void
{
$this->callbacks[] = $callback;
}
protected function invokeOnPublishCallbacks(int $iteration): void
{
foreach ($this->callbacks as $callback) {
$callback($iteration);
}
}
/**
* Checks if the PackageStorage has been previously initialized with symlinks
* and clears them. Otherwise the original sources would be overwritten.
*
* @param StorageInterface $storage
* @return void
*/
protected function checkAndRemovePackageSymlinks(StorageInterface $storage)
{
if (!$storage instanceof PackageStorage) {
return;
}
foreach ($storage->getPublicResourcePaths() as $packageKey => $path) {
$targetPathAndFilename = $this->path . $packageKey;
if (Files::is_link($targetPathAndFilename)) {
Files::unlink($targetPathAndFilename);
}
}
}
/**
* Publishes the whole collection to this target
*
* @param CollectionInterface $collection The collection to publish
* @return void
*/
public function publishCollection(CollectionInterface $collection)
{
$storage = $collection->getStorage();
$this->checkAndRemovePackageSymlinks($storage);
$iteration = 0;
foreach ($collection->getObjects() as $object) {
$sourceStream = $object->getStream();
if ($sourceStream === false) {
$this->handleMissingData($object, $collection);
continue;
}
$this->publishFile($sourceStream, $this->getRelativePublicationPathAndFilename($object));
fclose($sourceStream);
$this->invokeOnPublishCallbacks($iteration);
$iteration++;
}
}
/**
* Publishes the given persistent resource from the given storage
*
* @param PersistentResource $resource The resource to publish
* @param CollectionInterface $collection The collection the given resource belongs to
* @return void
*/
public function publishResource(PersistentResource $resource, CollectionInterface $collection)
{
$sourceStream = $resource->getStream();
if ($sourceStream === false) {
$this->handleMissingData($resource, $collection);
return;
}
$this->publishFile($sourceStream, $this->getRelativePublicationPathAndFilename($resource));
fclose($sourceStream);
}
/**
* Handle missing data notification
*
* @param ResourceMetaDataInterface $resource
* @param CollectionInterface $collection
*/
protected function handleMissingData(ResourceMetaDataInterface $resource, CollectionInterface $collection)
{
$message = sprintf('Could not publish resource %s with SHA1 hash %s of collection %s because there seems to be no corresponding data in the storage.', $resource->getFilename(), $resource->getSha1(), $collection->getName());
$this->messageCollector->append($message);
}
/**
* Unpublishes the given persistent resource
*
* @param PersistentResource $resource The resource to unpublish
* @return void
*/
public function unpublishResource(PersistentResource $resource)
{
$resources = $this->resourceRepository->findSimilarResources($resource);
if (count($resources) > 1) {
$message = sprintf('Did not unpublish resource %s with SHA1 hash %s because it is used by other Resource objects.', $resource->getFilename(), $resource->getSha1());
$this->messageCollector->append($message, Error::SEVERITY_NOTICE);
return;
}
$this->unpublishFile($this->getRelativePublicationPathAndFilename($resource));
}
/**
* Returns the web accessible URI pointing to the given static resource
*
* @param string $relativePathAndFilename Relative path and filename of the static resource
* @return string The URI
*/
public function getPublicStaticResourceUri($relativePathAndFilename)
{
return $this->absoluteBaseUri . $this->encodeRelativePathAndFilenameForUri($relativePathAndFilename);
}
/**
* Returns the web accessible URI pointing to the specified persistent resource
*
* @param PersistentResource $resource PersistentResource object
* @return string The URI
* @throws Exception
*/
public function getPublicPersistentResourceUri(PersistentResource $resource)
{
return $this->absoluteBaseUri . $this->encodeRelativePathAndFilenameForUri($this->getRelativePublicationPathAndFilename($resource));
}
/**
* Applies rawurlencode() to all path segments of the given $relativePathAndFilename
*
* @param string $relativePathAndFilename
* @return string
*/
protected function encodeRelativePathAndFilenameForUri($relativePathAndFilename)
{
return implode('/', array_map('rawurlencode', explode('/', $relativePathAndFilename)));
}
/**
* Publishes the given source stream to this target, with the given relative path.
*
* @param resource $sourceStream Stream of the source to publish
* @param string $relativeTargetPathAndFilename relative path and filename in the target directory
* @return void
* @throws TargetException
*/
protected function publishFile($sourceStream, $relativeTargetPathAndFilename)
{
$pathInfo = UnicodeFunctions::pathinfo($relativeTargetPathAndFilename);
if (isset($pathInfo['extension']) && array_key_exists(strtolower($pathInfo['extension']), $this->excludedExtensions) && $this->excludedExtensions[strtolower($pathInfo['extension'])] === true) {
throw new TargetException(sprintf('Could not publish "%s" into resource publishing target "%s" because the filename extension "%s" is excluded.', $sourceStream, $this->name, strtolower($pathInfo['extension'])), 1447148472);
}
$targetPathAndFilename = $this->path . $relativeTargetPathAndFilename;
$streamMetaData = stream_get_meta_data($sourceStream);
$sourcePathAndFilename = $streamMetaData['uri'] ?? null;
if (@fstat($sourceStream) === false) {
throw new TargetException(sprintf('Could not publish "%s" into resource publishing target "%s" because the source file is not accessible (file stat failed).', $sourceStream, $this->name), 1375258499);
}
// If you switch from FileSystemSymlinkTarget than we need to remove the symlink before trying to write the file
$targetPathAndFilenameInfo = new \SplFileInfo($targetPathAndFilename);
if ($targetPathAndFilenameInfo->isLink() && $targetPathAndFilenameInfo->getRealPath() === $sourcePathAndFilename) {
Files::unlink($targetPathAndFilename);
}
if (!file_exists(dirname($targetPathAndFilename))) {
Files::createDirectoryRecursively(dirname($targetPathAndFilename));
}
if (!is_writable(dirname($targetPathAndFilename))) {
throw new Exception(sprintf('Could not publish "%s" into resource publishing target "%s" because the target file "%s" is not writable.', $sourceStream, $this->name, $targetPathAndFilename), 1428917322);
}
try {
$targetFileHandle = fopen($targetPathAndFilename, 'w');
$result = stream_copy_to_stream($sourceStream, $targetFileHandle);
fclose($targetFileHandle);
} catch (\Exception $exception) {
$result = false;
}
if ($result === false) {
throw new TargetException(sprintf('Could not publish "%s" into resource publishing target "%s" because the source file could not be copied to the target location.', $sourceStream, $this->name), 1375258399, (isset($exception) ? $exception : null));
}
$this->logger->debug(sprintf('FileSystemTarget: Published file. (target: %s, file: %s)', $this->name, $relativeTargetPathAndFilename));
}
/**
* Removes the specified target file from the public directory
*
* This method fails silently if the given file could not be unpublished or already didn't exist anymore.
*
* @param string $relativeTargetPathAndFilename relative path and filename in the target directory
* @return void
*/
protected function unpublishFile($relativeTargetPathAndFilename)
{
$targetPathAndFilename = $this->path . $relativeTargetPathAndFilename;
if (!file_exists($targetPathAndFilename)) {
$message = sprintf('Did not remove file %s because it did not exist.', $targetPathAndFilename);
$this->messageCollector->append($message, Error::SEVERITY_NOTICE);
return;
}
if (!Files::unlink($targetPathAndFilename)) {
$message = sprintf('Removal of file %s failed.', $targetPathAndFilename);
$this->messageCollector->append($message, Error::SEVERITY_WARNING);
return;
}
Files::removeEmptyDirectoriesOnPath(dirname($targetPathAndFilename));
}
/**
* Determines and returns the relative path and filename for the given Storage Object or PersistentResource. If the given
* object represents a persistent resource, its own relative publication path will be empty. If the given object
* represents a static resources, it will contain a relative path.
*
* No matter which kind of resource, persistent or static, this function will return a sub directory structure
* if no relative publication path was defined in the given object.
*
* @param ResourceMetaDataInterface $object PersistentResource or Storage Object
* @return string The relative path and filename, for example "c/8/2/8/c828d0f88ce197be1aff7cc2e5e86b1244241ac6/MyPicture.jpg" (if subdivideHashPathSegment is on) or "c828d0f88ce197be1aff7cc2e5e86b1244241ac6/MyPicture.jpg" (if it's off)
*/
protected function getRelativePublicationPathAndFilename(ResourceMetaDataInterface $object): string
{
if ($object->getRelativePublicationPath() !== '') {
return $object->getRelativePublicationPath() . $object->getFilename();
}
if ($this->subdivideHashPathSegment) {
$sha1Hash = $object->getSha1();
return $sha1Hash[0] . '/' . $sha1Hash[1] . '/' . $sha1Hash[2] . '/' . $sha1Hash[3] . '/' . $sha1Hash . '/' . $object->getFilename();
}
return $object->getSha1() . '/' . $object->getFilename();
}
/**
* Set an option value and return if it was set.
*
* @param string $key
* @param mixed $value
* @return boolean
*/
protected function setOption($key, $value)
{
switch ($key) {
case 'baseUri':
case 'path':
case 'excludedExtensions':
$this->$key = $value;
break;
// Only for b/c - remove with next major
case 'extensionBlacklist':
$this->excludedExtensions = $value;
break;
case 'subdivideHashPathSegment':
$this->subdivideHashPathSegment = (boolean)$value;
break;
default:
return false;
}
return true;
}
}