Skip to content

FEATURE: 3334 Resource targets without space tunnels #3445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 9.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions Neos.Flow/Classes/ResourceManagement/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\BaseUriProvider;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\ResourceManagement\Target\AbsoluteBaseUriAwareTarget;
use Neos\Utility\ObjectAccess;
use Neos\Flow\ResourceManagement\Storage\StorageInterface;
use Neos\Flow\ResourceManagement\Storage\WritableStorageInterface;
use Neos\Flow\ResourceManagement\Target\TargetInterface;
use Neos\Flow\Utility\Algorithms;
use Neos\Flow\Utility\Environment;
use Neos\Utility\Unicode\Functions as UnicodeFunctions;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -64,6 +67,12 @@ class ResourceManager
*/
protected $persistenceManager;

/**
* @Flow\Inject
* @var BaseUriProvider
*/
protected $baseUriProvider;

/**
* @var array
*/
Expand Down Expand Up @@ -373,7 +382,7 @@ public function deleteResource(PersistentResource $resource, $unpublishResource
* @return string|false A URI as a string or false if the collection of the resource is not found
* @api
*/
public function getPublicPersistentResourceUri(PersistentResource $resource)
public function getPublicPersistentResourceUri(PersistentResource $resource, ?UriInterface $baseUri = null)
{
$this->initialize();

Expand All @@ -382,7 +391,9 @@ public function getPublicPersistentResourceUri(PersistentResource $resource)
}
/** @var TargetInterface $target */
$target = $this->collections[$resource->getCollectionName()]->getTarget();

if ($target instanceof AbsoluteBaseUriAwareTarget) {
$target->setAbsoluteBaseUri($baseUri ?? $this->baseUriProvider->getConfiguredBaseUriOrFallbackToCurrentRequest());
}
return $target->getPublicPersistentResourceUri($resource);
}

Expand All @@ -396,7 +407,7 @@ public function getPublicPersistentResourceUri(PersistentResource $resource)
* @throws Exception
* @api
*/
public function getPublicPersistentResourceUriByHash($resourceHash, $collectionName = self::DEFAULT_PERSISTENT_COLLECTION_NAME)
public function getPublicPersistentResourceUriByHash($resourceHash, $collectionName = self::DEFAULT_PERSISTENT_COLLECTION_NAME, ?UriInterface $baseUri = null)
{
$this->initialize();

Expand All @@ -409,7 +420,9 @@ public function getPublicPersistentResourceUriByHash($resourceHash, $collectionN
if ($resource === null) {
throw new Exception(sprintf('Could not determine persistent resource URI for "%s" because no PersistentResource object with that SHA1 hash could be found.', $resourceHash), 1375347691);
}

if ($target instanceof AbsoluteBaseUriAwareTarget) {
$target->setAbsoluteBaseUri($baseUri ?? $this->baseUriProvider->getConfiguredBaseUriOrFallbackToCurrentRequest());
}
return $target->getPublicPersistentResourceUri($resource);
}

Expand All @@ -422,12 +435,15 @@ public function getPublicPersistentResourceUriByHash($resourceHash, $collectionN
* @return string
* @api
*/
public function getPublicPackageResourceUri($packageKey, $relativePathAndFilename)
public function getPublicPackageResourceUri($packageKey, $relativePathAndFilename, ?UriInterface $baseUri = null)
{
$this->initialize();

/** @var TargetInterface $target */
$target = $this->collections[self::DEFAULT_STATIC_COLLECTION_NAME]->getTarget();
if ($target instanceof AbsoluteBaseUriAwareTarget) {
$target->setAbsoluteBaseUri($baseUri ?? $this->baseUriProvider->getConfiguredBaseUriOrFallbackToCurrentRequest());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically absoluteBaseUri is not fully correct, as the api would also allow passing an empty uri: new Uri('')

Now the value returned by getConfiguredBaseUriOrFallbackToCurrentRequest is likely just the plain host but might also contain the dir of the request path $SCRIPT_NAME.
That would fit with the description of absolute uri.

Now as soon as Neos.Flow.http.baseUri is not null even an empty string would be returned which does NOT fit again into idea.

}
return $target->getPublicStaticResourceUri($packageKey . '/' . $relativePathAndFilename);
}

Expand All @@ -438,11 +454,11 @@ public function getPublicPackageResourceUri($packageKey, $relativePathAndFilenam
* @return string
* @api
*/
public function getPublicPackageResourceUriByPath($path)
public function getPublicPackageResourceUriByPath($path, ?UriInterface $baseUri = null)
{
$this->initialize();
list($packageKey, $relativePathAndFilename) = $this->getPackageAndPathByPublicPath($path);
return $this->getPublicPackageResourceUri($packageKey, $relativePathAndFilename);
return $this->getPublicPackageResourceUri($packageKey, $relativePathAndFilename, $baseUri);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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 Psr\Http\Message\UriInterface;

/**
* Allows resource uris to be built with and absolute uri
*
* For targets implementing this interface, setAbsoluteBaseUri() must be invoked getting a resource uri
*
* - {@see TargetInterface::getPublicStaticResourceUri}
* - {@see TargetInterface::getPublicPersistentResourceUri}
*/
interface AbsoluteBaseUriAwareTarget
{
public function setAbsoluteBaseUri(UriInterface $baseUri): void;
}
54 changes: 14 additions & 40 deletions Neos.Flow/Classes/ResourceManagement/Target/FileSystemTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
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
class FileSystemTarget implements TargetInterface, AbsoluteBaseUriAwareTarget
{
/**
* @var array
Expand Down Expand Up @@ -71,7 +72,7 @@ class FileSystemTarget implements TargetInterface
*
* @var string
*/
protected $absoluteBaseUri;
protected string $absoluteBaseUri;

/**
* If the generated URI path segment containing the sha1 should be divided into multiple segments
Expand Down Expand Up @@ -105,12 +106,6 @@ class FileSystemTarget implements TargetInterface
*/
protected $messageCollector;

/**
* @Flow\Inject
* @var BaseUriProvider
*/
protected $baseUriProvider;

/**
* Constructor
*
Expand All @@ -123,6 +118,15 @@ public function __construct($name, array $options = [])
$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.
*
Expand Down Expand Up @@ -286,7 +290,7 @@ public function unpublishResource(PersistentResource $resource)
*/
public function getPublicStaticResourceUri($relativePathAndFilename)
{
return $this->getResourcesBaseUri() . $this->encodeRelativePathAndFilenameForUri($relativePathAndFilename);
return $this->absoluteBaseUri . $this->encodeRelativePathAndFilenameForUri($relativePathAndFilename);
}

/**
Expand All @@ -298,7 +302,7 @@ public function getPublicStaticResourceUri($relativePathAndFilename)
*/
public function getPublicPersistentResourceUri(PersistentResource $resource)
{
return $this->getResourcesBaseUri() . $this->encodeRelativePathAndFilenameForUri($this->getRelativePublicationPathAndFilename($resource));
return $this->absoluteBaseUri . $this->encodeRelativePathAndFilenameForUri($this->getRelativePublicationPathAndFilename($resource));
}

/**
Expand Down Expand Up @@ -387,36 +391,6 @@ protected function unpublishFile($relativeTargetPathAndFilename)
Files::removeEmptyDirectoriesOnPath(dirname($targetPathAndFilename));
}

/**
* Returns the resolved absolute base URI for resources of this target.
*
* @return string The absolute base URI for resources in this target
*/
protected function getResourcesBaseUri()
{
if ($this->absoluteBaseUri === null) {
$this->absoluteBaseUri = $this->detectResourcesBaseUri();
}

return $this->absoluteBaseUri;
}

/**
* Detects and returns the website's absolute base URI
*
* @return string The resolved resource base URI, @see getResourcesBaseUri()
* @throws \Neos\Flow\Http\Exception
*/
protected function detectResourcesBaseUri()
{
if ($this->baseUri !== '' && ($this->baseUri[0] === '/' || strpos($this->baseUri, '://') !== false)) {
return $this->baseUri;
}

$httpBaseUri = (string)$this->baseUriProvider->getConfiguredBaseUriOrFallbackToCurrentRequest();
return $httpBaseUri . $this->baseUri;
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Neos\Flow\ResourceManagement\Target;

/*
Expand All @@ -11,12 +12,12 @@
* source code.
*/

/**
* Interface for a resource publishing target
*/
use Neos\Flow\ResourceManagement\CollectionInterface;
use Neos\Flow\ResourceManagement\PersistentResource;

/**
* Interface for a resource publishing target
*/
interface TargetInterface
{
/**
Expand Down
Loading