diff --git a/Neos.Flow/Classes/ResourceManagement/ResourceManager.php b/Neos.Flow/Classes/ResourceManagement/ResourceManager.php index 8956aa1770..8566bf5daf 100644 --- a/Neos.Flow/Classes/ResourceManagement/ResourceManager.php +++ b/Neos.Flow/Classes/ResourceManagement/ResourceManager.php @@ -12,9 +12,11 @@ */ 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; @@ -22,6 +24,7 @@ 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; /** @@ -64,6 +67,12 @@ class ResourceManager */ protected $persistenceManager; + /** + * @Flow\Inject + * @var BaseUriProvider + */ + protected $baseUriProvider; + /** * @var array */ @@ -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(); @@ -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); } @@ -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(); @@ -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); } @@ -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()); + } return $target->getPublicStaticResourceUri($packageKey . '/' . $relativePathAndFilename); } @@ -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); } /** diff --git a/Neos.Flow/Classes/ResourceManagement/Target/AbsoluteBaseUriAwareTarget.php b/Neos.Flow/Classes/ResourceManagement/Target/AbsoluteBaseUriAwareTarget.php new file mode 100644 index 0000000000..8228feccd6 --- /dev/null +++ b/Neos.Flow/Classes/ResourceManagement/Target/AbsoluteBaseUriAwareTarget.php @@ -0,0 +1,28 @@ +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. * @@ -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); } /** @@ -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)); } /** @@ -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 diff --git a/Neos.Flow/Classes/ResourceManagement/Target/TargetInterface.php b/Neos.Flow/Classes/ResourceManagement/Target/TargetInterface.php index 82a9b5826f..1b39cbbfd2 100644 --- a/Neos.Flow/Classes/ResourceManagement/Target/TargetInterface.php +++ b/Neos.Flow/Classes/ResourceManagement/Target/TargetInterface.php @@ -1,4 +1,5 @@