"Fossies" - the Fresh Open Source Software Archive

Member "neos-development-collection-7.0.1/Neos.Media/Classes/Domain/Model/AssetSource/Neos/NeosAssetSource.php" (23 Feb 2021, 7264 Bytes) of package /linux/www/neos-development-collection-7.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "NeosAssetSource.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 declare(strict_types=1);
    3 
    4 namespace Neos\Media\Domain\Model\AssetSource\Neos;
    5 
    6 /*
    7  * This file is part of the Neos.Media package.
    8  *
    9  * (c) Contributors of the Neos Project - www.neos.io
   10   *
   11  * This package is Open Source Software. For the full copyright and license
   12  * information, please view the LICENSE file which was distributed with this
   13  * source code.
   14  */
   15 
   16 use Neos\Flow\Core\Bootstrap;
   17 use Neos\Flow\Http\HttpRequestHandlerInterface;
   18 use GuzzleHttp\Psr7\Uri;
   19 use Neos\Flow\Mvc\ActionRequest;
   20 use Neos\Flow\ResourceManagement\ResourceManager;
   21 use Neos\Media\Domain\Model\AssetInterface;
   22 use Neos\Media\Domain\Model\AssetSource\AssetProxyRepositoryInterface;
   23 use Neos\Media\Domain\Model\AssetSource\AssetSourceInterface;
   24 use Neos\Media\Domain\Service\AssetService;
   25 use Neos\Media\Domain\Service\ThumbnailService;
   26 use Neos\Flow\Annotations as Flow;
   27 use Neos\Media\Exception\AssetServiceException;
   28 use Neos\Media\Exception\ThumbnailServiceException;
   29 use Psr\Http\Message\UriInterface;
   30 
   31 /**
   32  * Asset source for Neos native assets
   33  */
   34 final class NeosAssetSource implements AssetSourceInterface
   35 {
   36     /**
   37      * @var string
   38      */
   39     private $assetSourceIdentifier;
   40 
   41     /**
   42      * @var NeosAssetProxyRepository
   43      */
   44     private $assetProxyRepository;
   45 
   46     /**
   47      * @Flow\Inject
   48      * @var ThumbnailService
   49      */
   50     protected $thumbnailService;
   51 
   52     /**
   53      * @Flow\Inject
   54      * @var AssetService
   55      */
   56     protected $assetService;
   57 
   58     /**
   59      * @Flow\Inject
   60      * @var Bootstrap
   61      */
   62     protected $bootstrap;
   63 
   64     /**
   65      * @Flow\InjectConfiguration(path="asyncThumbnails")
   66      * @var bool
   67      */
   68     protected $asyncThumbnails;
   69 
   70     /**
   71      * @Flow\Inject
   72      * @var ResourceManager
   73      */
   74     protected $resourceManager;
   75 
   76     /**
   77      * @var string
   78      */
   79     protected $iconPath;
   80 
   81     /**
   82      * @var string
   83      */
   84     protected $description = '';
   85 
   86     /**
   87      * @param string $assetSourceIdentifier
   88      * @param array $assetSourceOptions
   89      */
   90     public function __construct(string $assetSourceIdentifier, array $assetSourceOptions)
   91     {
   92         if (preg_match('/^[a-z][a-z0-9-]{0,62}[a-z]$/', $assetSourceIdentifier) !== 1) {
   93             throw new \InvalidArgumentException(sprintf('Invalid asset source identifier "%s". The identifier must match /^[a-z][a-z0-9-]{0,62}[a-z]$/', $assetSourceIdentifier), 1513329665);
   94         }
   95         $this->assetSourceIdentifier = $assetSourceIdentifier;
   96 
   97         foreach ($assetSourceOptions as $optionName => $optionValue) {
   98             switch ($optionName) {
   99                 case 'asyncThumbnails':
  100                     // If the option value is empty, preserve the default value injected from the Neos:Media:asyncThumbnails setting:
  101                     if (!empty($optionValue)) {
  102                         if (!is_bool($optionValue)) {
  103                             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);
  104                         }
  105                         $this->asyncThumbnails = $optionValue;
  106                     }
  107                     break;
  108                 case 'icon':
  109                     $this->iconPath = $optionValue;
  110                     break;
  111                 case 'description':
  112                     $this->description = $optionValue;
  113                     break;
  114                 default:
  115                     throw new \InvalidArgumentException(sprintf('Unknown asset source option "%s" specified for Neos asset source "%s". Please check your settings.', $optionName, $assetSourceIdentifier), 1513327774);
  116             }
  117         }
  118     }
  119 
  120     /**
  121      * @param string $assetSourceIdentifier
  122      * @param array $assetSourceOptions
  123      * @return AssetSourceInterface
  124      */
  125     public static function createFromConfiguration(string $assetSourceIdentifier, array $assetSourceOptions): AssetSourceInterface
  126     {
  127         return new static($assetSourceIdentifier, $assetSourceOptions);
  128     }
  129 
  130     /**
  131      * @return string
  132      */
  133     public function getIdentifier(): string
  134     {
  135         return $this->assetSourceIdentifier;
  136     }
  137 
  138     /**
  139      * @return string
  140      */
  141     public function getLabel(): string
  142     {
  143         return 'Neos';
  144     }
  145 
  146     /**
  147      * Returns the resource path to Assetsources icon
  148      *
  149      * @return string
  150      */
  151     public function getIconUri(): string
  152     {
  153         return $this->resourceManager->getPublicPackageResourceUriByPath($this->iconPath);
  154     }
  155 
  156     /**
  157      *
  158      * @return string
  159      */
  160     public function getDescription(): string
  161     {
  162         return $this->description;
  163     }
  164 
  165     /**
  166      * @return AssetProxyRepositoryInterface
  167      */
  168     public function getAssetProxyRepository(): AssetProxyRepositoryInterface
  169     {
  170         if ($this->assetProxyRepository === null) {
  171             $this->assetProxyRepository = new NeosAssetProxyRepository($this);
  172         }
  173 
  174         return $this->assetProxyRepository;
  175     }
  176 
  177     /**
  178      * @return bool
  179      */
  180     public function isReadOnly(): bool
  181     {
  182         return false;
  183     }
  184 
  185     /**
  186      * Internal method used by NeosAssetProxy
  187      *
  188      * @param AssetInterface $asset
  189      * @return Uri|null
  190      * @throws AssetServiceException
  191      * @throws ThumbnailServiceException
  192      * @throws \Neos\Flow\Http\Exception
  193      * @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
  194      */
  195     public function getThumbnailUriForAsset(AssetInterface $asset): ?UriInterface
  196     {
  197         $actionRequest = ($this->asyncThumbnails ? $this->createActionRequest() : null);
  198         $thumbnailConfiguration = $this->thumbnailService->getThumbnailConfigurationForPreset('Neos.Media.Browser:Thumbnail', ($actionRequest !== null));
  199         $thumbnailData = $this->assetService->getThumbnailUriAndSizeForAsset($asset, $thumbnailConfiguration, $actionRequest);
  200         return isset($thumbnailData['src']) ? new Uri($thumbnailData['src']) : null;
  201     }
  202 
  203     /**
  204      * Internal method used by NeosAssetProxy
  205      *
  206      * @param AssetInterface $asset
  207      * @return Uri|null
  208      * @throws AssetServiceException
  209      * @throws ThumbnailServiceException
  210      * @throws \Neos\Flow\Http\Exception
  211      * @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
  212      */
  213     public function getPreviewUriForAsset(AssetInterface $asset): ?UriInterface
  214     {
  215         $actionRequest = ($this->asyncThumbnails ? $this->createActionRequest() : null);
  216         $thumbnailConfiguration = $this->thumbnailService->getThumbnailConfigurationForPreset('Neos.Media.Browser:Preview', ($actionRequest !== null));
  217         $thumbnailData = $this->assetService->getThumbnailUriAndSizeForAsset($asset, $thumbnailConfiguration, $actionRequest);
  218         return isset($thumbnailData['src']) ? new Uri($thumbnailData['src']) : null;
  219     }
  220 
  221     /**
  222      * @return ActionRequest|null
  223      */
  224     private function createActionRequest(): ?ActionRequest
  225     {
  226         $requestHandler = $this->bootstrap->getActiveRequestHandler();
  227         if ($requestHandler instanceof HttpRequestHandlerInterface) {
  228             return ActionRequest::fromHttpRequest($requestHandler->getHttpRequest());
  229         }
  230         return null;
  231     }
  232 }