From 269bfbb081294c592ec4ed3c121d8bdb8ba1b611 Mon Sep 17 00:00:00 2001 From: roadiz-ci Date: Fri, 8 Mar 2024 14:45:29 +0000 Subject: [PATCH] Merge tag v2.2.12 into develop --- src/Exceptions/PrivateDocumentException.php | 9 + src/Packages.php | 366 ------------------ src/Renderer/AbstractImageRenderer.php | 7 +- src/Renderer/VideoRenderer.php | 2 +- .../AbstractDocumentUrlGenerator.php | 36 +- 5 files changed, 23 insertions(+), 397 deletions(-) create mode 100644 src/Exceptions/PrivateDocumentException.php delete mode 100644 src/Packages.php diff --git a/src/Exceptions/PrivateDocumentException.php b/src/Exceptions/PrivateDocumentException.php new file mode 100644 index 0000000..133a087 --- /dev/null +++ b/src/Exceptions/PrivateDocumentException.php @@ -0,0 +1,9 @@ +requestStackContext = new RequestStackContext($requestStack); - $this->requestStack = $requestStack; - $this->fileAware = $fileAware; - $this->staticDomain = $staticDomain; - $this->versionStrategy = $versionStrategy; - $this->ready = false; - } - - /** - * Defer creating package collection not to create error - * when warming up cache on dependency injection. - * These packages need a valid Request object. - */ - protected function initializePackages(): void - { - $this->setDefaultPackage($this->getDefaultPackage()); - $packages = [ - static::DOCUMENTS => $this->getDocumentPackage(), - static::PUBLIC_PATH => $this->getPublicPathPackage(), - static::PRIVATE_PATH => $this->getPrivatePathPackage(), - static::FONTS_PATH => $this->getFontsPathPackage(), - ]; - if (null !== $this->getRequest()) { - $packages = array_merge($packages, [ - static::ABSOLUTE => $this->getAbsoluteDefaultPackage(), - static::ABSOLUTE_DOCUMENTS => $this->getAbsoluteDocumentPackage(), - ]); - } - foreach ($packages as $name => $package) { - $this->addPackage((string) $name, $package); - } - $this->ready = true; - } - - /** - * @inheritDoc - */ - public function getPackage($name = null): PackageInterface - { - if (false === $this->ready) { - $this->initializePackages(); - } - - return parent::getPackage($name); - } - - /** - * @return bool - */ - public function useStaticDomain(): bool - { - return $this->staticDomain != ""; - } - - /** - * @return string - */ - protected function getStaticDomainAndPort(): string - { - /* - * Add non-default port to static domain. - */ - $staticDomainAndPort = $this->staticDomain; - $request = $this->getRequest(); - if ( - null !== $request - && (($this->requestStackContext->isSecure() && $request->getPort() != 443) - || (!$this->requestStackContext->isSecure() && $request->getPort() != 80)) - ) { - $staticDomainAndPort .= ':' . $request->getPort(); - } - - /* - * If no protocol, use https as default - */ - if ( - !preg_match("~^//~i", $staticDomainAndPort) - && !preg_match("~^(?:f|ht)tps?://~i", $staticDomainAndPort) - ) { - $staticDomainAndPort = "https://" . $staticDomainAndPort; - } - - return $staticDomainAndPort; - } - - /** - * @return PathPackage|UrlPackage - */ - protected function getDefaultPackage(): PathPackage|UrlPackage - { - if ($this->useStaticDomain()) { - return new UrlPackage( - $this->getStaticDomainAndPort(), - $this->versionStrategy - ); - } - - return new PathPackage( - '/', - $this->versionStrategy, - $this->requestStackContext - ); - } - - /** - * @return PathPackage|UrlPackage - */ - protected function getAbsoluteDefaultPackage(): PathPackage|UrlPackage - { - if ($this->useStaticDomain()) { - return $this->getDefaultPackage(); - } - $scheme = ''; - if (null !== $this->getRequest()) { - $scheme = $this->getRequest()->getSchemeAndHttpHost(); - } - return new UrlPackage( - $scheme . $this->requestStackContext->getBasePath(), - $this->versionStrategy - ); - } - - /** - * @return PathPackage|UrlPackage - */ - protected function getDocumentPackage(): PathPackage|UrlPackage - { - if ($this->useStaticDomain()) { - return new UrlPackage( - $this->getStaticDomainAndPort() . $this->fileAware->getPublicFilesBasePath(), - $this->versionStrategy - ); - } - - return new PathPackage( - $this->fileAware->getPublicFilesBasePath(), - $this->versionStrategy, - $this->requestStackContext - ); - } - - /** - * @return PathPackage|UrlPackage - */ - protected function getAbsoluteDocumentPackage(): PathPackage|UrlPackage - { - if ($this->useStaticDomain()) { - return $this->getDocumentPackage(); - } - $scheme = ''; - if (null !== $this->getRequest()) { - $scheme = $this->getRequest()->getSchemeAndHttpHost(); - } - return new UrlPackage( - $scheme . $this->requestStackContext->getBasePath() . $this->fileAware->getPublicFilesBasePath(), - $this->versionStrategy - ); - } - - /** - * @return PathPackage - */ - protected function getPublicPathPackage(): PathPackage - { - return new PathPackage( - $this->fileAware->getPublicFilesPath(), - $this->versionStrategy - ); - } - - /** - * @return PathPackage - */ - protected function getPrivatePathPackage(): PathPackage - { - return new PathPackage( - $this->fileAware->getPrivateFilesPath(), - $this->versionStrategy - ); - } - - /** - * @return PathPackage - */ - protected function getFontsPathPackage(): PathPackage - { - return new PathPackage( - $this->fileAware->getFontsFilesPath(), - $this->versionStrategy - ); - } - - /** - * Shortcut for $this->getUrl($relativePath, static::FONTS_PATH). - * - * @param string $relativePath - * @return string - * @deprecated Use FilesystemOperator font.storage instead to support external filesystems. - */ - public function getFontsPath(string $relativePath): string - { - return $this->getUrl($relativePath, static::FONTS_PATH); - } - - /** - * Shortcut for $this->getUrl($relativePath, static::PUBLIC_PATH). - * - * @param string $relativePath - * @return string - * @deprecated Use FilesystemOperator documents.storage instead to support external filesystems. - */ - public function getPublicFilesPath(string $relativePath): string - { - return $this->getUrl($relativePath, static::PUBLIC_PATH); - } - - /** - * Shortcut for $this->getUrl($relativePath, static::PRIVATE_PATH). - * - * @param string $relativePath - * @return string - * @deprecated Use FilesystemOperator documents.storage instead to support external filesystems. - */ - public function getPrivateFilesPath(string $relativePath): string - { - return $this->getUrl($relativePath, static::PRIVATE_PATH); - } - - /** - * @param DocumentInterface $document - * @return string Document file absolute path according if document is private or not. - * @throws DocumentWithoutFileException - * @deprecated Use FilesystemOperator documents.storage instead to support external filesystems. - */ - public function getDocumentFilePath(DocumentInterface $document): string - { - if (!$document->isLocal()) { - throw new DocumentWithoutFileException($document); - } - if ($document->isPrivate()) { - return $this->getPrivateFilesPath($document->getRelativePath() ?? ''); - } - return $this->getPublicFilesPath($document->getRelativePath() ?? ''); - } - - /** - * @param DocumentInterface $document - * @return string Document folder absolute path according if document is private or not. - * @throws DocumentWithoutFileException - * @deprecated Use FilesystemOperator documents.storage instead to support external filesystems. - */ - public function getDocumentFolderPath(DocumentInterface $document): string - { - if (!$document->isLocal()) { - throw new DocumentWithoutFileException($document); - } - if ($document->isPrivate()) { - return $this->getPrivateFilesPath($document->getFolder()); - } - return $this->getPublicFilesPath($document->getFolder()); - } - - /** - * @return string - * @deprecated Use FilesystemOperator documents.storage instead to support external filesystems. - */ - public function getStaticDomain(): string - { - return $this->staticDomain; - } - - /** - * @param string $staticDomain - * @return Packages - * @deprecated Use FilesystemOperator documents.storage instead to support external filesystems. - */ - public function setStaticDomain(string $staticDomain): Packages - { - $this->staticDomain = $staticDomain; - return $this; - } - - /** - * @return null|Request - */ - protected function getRequest(): ?Request - { - return $this->requestStack->getMainRequest(); - } -} diff --git a/src/Renderer/AbstractImageRenderer.php b/src/Renderer/AbstractImageRenderer.php index fd7455a..4c7c60c 100644 --- a/src/Renderer/AbstractImageRenderer.php +++ b/src/Renderer/AbstractImageRenderer.php @@ -97,7 +97,12 @@ protected function parseSrcSetInner( ): string { $output = []; foreach ($srcSetArray as $set) { - if (isset($set['format']) && isset($set['rule']) && !empty($document->getRelativePath())) { + if ( + isset($set['format']) && + isset($set['rule']) && + !$document->isPrivate() && + !empty($document->getRelativePath()) + ) { $this->documentUrlGenerator->setOptions($this->urlOptionsResolver->resolve($set['format'])); $this->documentUrlGenerator->setDocument($document); $path = $this->documentUrlGenerator->getUrl($absolute); diff --git a/src/Renderer/VideoRenderer.php b/src/Renderer/VideoRenderer.php index 109e056..15a84e4 100644 --- a/src/Renderer/VideoRenderer.php +++ b/src/Renderer/VideoRenderer.php @@ -78,7 +78,7 @@ protected function getPosterUrl( $document->hasThumbnails() ) { $thumbnail = $document->getThumbnails()->first(); - if ($thumbnail instanceof DocumentInterface) { + if (false !== $thumbnail) { $this->documentUrlGenerator->setOptions($options); $this->documentUrlGenerator->setDocument($thumbnail); return $this->documentUrlGenerator->getUrl($absolute); diff --git a/src/UrlGenerators/AbstractDocumentUrlGenerator.php b/src/UrlGenerators/AbstractDocumentUrlGenerator.php index 5208d1a..2c05f7f 100644 --- a/src/UrlGenerators/AbstractDocumentUrlGenerator.php +++ b/src/UrlGenerators/AbstractDocumentUrlGenerator.php @@ -7,6 +7,7 @@ use League\Flysystem\FilesystemOperator; use Psr\Cache\CacheItemPoolInterface; use Psr\Cache\InvalidArgumentException; +use RZ\Roadiz\Documents\Exceptions\PrivateDocumentException; use RZ\Roadiz\Documents\Models\DocumentInterface; use RZ\Roadiz\Documents\OptionsResolver\ViewOptionsResolver; use Symfony\Component\HttpFoundation\UrlHelper; @@ -15,35 +16,18 @@ abstract class AbstractDocumentUrlGenerator implements DocumentUrlGeneratorInter { protected ?DocumentInterface $document; protected array $options; - protected CacheItemPoolInterface $optionsCacheAdapter; protected ViewOptionsResolver $viewOptionsResolver; protected OptionsCompiler $optionCompiler; - protected FilesystemOperator $documentsStorage; - private UrlHelper $urlHelper; - /** - * @param FilesystemOperator $documentsStorage - * @param UrlHelper $urlHelper - * @param CacheItemPoolInterface $optionsCacheAdapter - * @param DocumentInterface|null $document - * @param array $options - * @throws InvalidArgumentException - */ public function __construct( - FilesystemOperator $documentsStorage, - UrlHelper $urlHelper, - CacheItemPoolInterface $optionsCacheAdapter, - DocumentInterface $document = null, + protected FilesystemOperator $documentsStorage, + protected UrlHelper $urlHelper, + protected CacheItemPoolInterface $optionsCacheAdapter, array $options = [] ) { - $this->document = $document; $this->viewOptionsResolver = new ViewOptionsResolver(); $this->optionCompiler = new OptionsCompiler(); - $this->optionsCacheAdapter = $optionsCacheAdapter; - $this->setOptions($options); - $this->documentsStorage = $documentsStorage; - $this->urlHelper = $urlHelper; } /** @@ -85,23 +69,17 @@ public function setDocument(DocumentInterface $document): static return $this; } - /** - * @param bool $absolute - * - * @return string - */ public function getUrl(bool $absolute = false): string { if (null === $this->document) { throw new \InvalidArgumentException('Cannot get URL from a NULL document'); } - - $mountPath = $this->document->getMountPath(); - if ($this->document->isPrivate()) { - throw new \InvalidArgumentException('Cannot get URL from a private document'); + throw new PrivateDocumentException('Cannot get URL from a private document'); } + $mountPath = $this->document->getMountPath(); + if (null !== $mountPath && ($this->options['noProcess'] === true || !$this->document->isProcessable())) { $publicUrl = $this->documentsStorage->publicUrl($mountPath); if ($absolute && \str_starts_with($publicUrl, '/')) {