Neos.Media/Classes/Domain/Service/AssetService.php
public function replaceAssetResource(AssetInterface $asset, PersistentResource $resource, array $options = [])
    {
        $originalAssetResource = $asset->getResource();
        $asset->setResource($resource);

        if (isset($options['keepOriginalFilename']) && (boolean)$options['keepOriginalFilename'] === true) {
            $asset->getResource()->setFilename($originalAssetResource->getFilename());
        }

        $uriMapping = [];
        $redirectHandlerEnabled = isset($options['generateRedirects']) && (boolean)$options['generateRedirects'] === true && $this->packageManager->isPackageAvailable('Neos.RedirectHandler');
        if ($redirectHandlerEnabled) {
            $originalAssetResourceUri = new Uri($this->resourceManager->getPublicPersistentResourceUri($originalAssetResource));
            $newAssetResourceUri = new Uri($this->resourceManager->getPublicPersistentResourceUri($asset->getResource()));
            $uriMapping[$originalAssetResourceUri->getPath()] = $newAssetResourceUri->getPath();
        }

        if (method_exists($asset, 'getVariants')) {
            $variants = $asset->getVariants();
            /** @var AssetVariantInterface $variant */
            foreach ($variants as $variant) {
                $originalVariantResource = $variant->getResource();
                $variant->refresh();

                if (method_exists($variant, 'getAdjustments')) {
                    foreach ($variant->getAdjustments() as $adjustment) {
                        if (method_exists($adjustment, 'refit') && $this->imageService->getImageSize($originalAssetResource) !== $this->imageService->getImageSize($resource)) {
                            $adjustment->refit($asset);
                        }
                    }
                }

                if ($redirectHandlerEnabled) {
                    $originalVariantResourceUri = new Uri($this->resourceManager->getPublicPersistentResourceUri($originalVariantResource));
                    $newVariantResourceUri = new Uri($this->resourceManager->getPublicPersistentResourceUri($variant->getResource()));
                    $uriMapping[$originalVariantResourceUri->getPath()] = $newVariantResourceUri->getPath();
                }

                $this->getRepository($variant)->update($variant);
            }
        }

        if ($redirectHandlerEnabled) {
            /** @var RedirectStorageInterface $redirectStorage */
            $redirectStorage = $this->objectManager->get(RedirectStorageInterface::class);
            foreach ($uriMapping as $originalUri => $newUri) {
                $existingRedirect = $redirectStorage->getOneBySourceUriPathAndHost($originalUri);
                if ($existingRedirect === null) {
                    $redirectStorage->addRedirect($originalUri, $newUri, 301);
                }
            }
        }

        $this->getRepository($asset)->update($asset);
        $this->emitAssetResourceReplaced($asset);
    }
public function replaceAssetResource(AssetInterface $asset, PersistentResource $resource, array $options = [])
    {
        $originalAssetResource = $asset->getResource();
        $asset->setResource($resource);

        if (isset($options['keepOriginalFilename']) && (boolean)$options['keepOriginalFilename'] === true) {
            $asset->getResource()->setFilename($originalAssetResource->getFilename());
        }

        $uriMapping = [];
        $redirectHandlerEnabled = isset($options['generateRedirects']) && (boolean)$options['generateRedirects'] === true && $this->packageManager->isPackageAvailable('Neos.RedirectHandler');
        if ($redirectHandlerEnabled) {
            $originalAssetResourceUri = new Uri($this->resourceManager->getPublicPersistentResourceUri($originalAssetResource));
            $newAssetResourceUri = new Uri($this->resourceManager->getPublicPersistentResourceUri($asset->getResource()));
            $uriMapping[$originalAssetResourceUri->getPath()] = $newAssetResourceUri->getPath();
        }

        if (method_exists($asset, 'getVariants')) {
            $variants = $asset->getVariants();
            /** @var AssetVariantInterface $variant */
            foreach ($variants as $variant) {
                $originalVariantResource = $variant->getResource();
                $variant->refresh();

                if (method_exists($variant, 'getAdjustments')) {
                    foreach ($variant->getAdjustments() as $adjustment) {
                        if (method_exists($adjustment, 'refit') && $this->imageService->getImageSize($originalAssetResource) !== $this->imageService->getImageSize($resource)) {
                            $adjustment->refit($asset);
                        }
                    }
                }

                if ($redirectHandlerEnabled) {
                    $originalVariantResourceUri = new Uri($this->resourceManager->getPublicPersistentResourceUri($originalVariantResource));
                    $newVariantResourceUri = new Uri($this->resourceManager->getPublicPersistentResourceUri($variant->getResource()));
                    $uriMapping[$originalVariantResourceUri->getPath()] = $newVariantResourceUri->getPath();
                }

                $this->getRepository($variant)->update($variant);
            }
        }

        if ($redirectHandlerEnabled) {
            /** @var RedirectStorageInterface $redirectStorage */
            $redirectStorage = $this->objectManager->get(RedirectStorageInterface::class);
            foreach ($uriMapping as $originalUri => $newUri) {
                $existingRedirect = $redirectStorage->getOneBySourceUriPathAndHost($originalUri);
                if ($existingRedirect === null) {
                    $redirectStorage->addRedirect($originalUri, $newUri, 301);
                }
            }
        }

        $this->getRepository($asset)->update($asset);
        $this->emitAssetResourceReplaced($asset);
    }
public function replaceAssetResource ( AssetInterface $ asset , PersistentResource $ resource , array $ options = [ ] ) { $ originalAssetResource = $ asset -> getResource ( ) ; $ asset -> setResource ( $ resource ) ; if ( isset ( $ options [ 'keepOriginalFilename' ] ) && ( boolean ) $ options [ 'keepOriginalFilename' ] === true ) { $ asset -> getResource ( ) -> setFilename ( $ originalAssetResource -> getFilename ( ) ) ; } $ uriMapping = [ ] ; $ redirectHandlerEnabled = isset ( $ options [ 'generateRedirects' ] ) && ( boolean ) $ options [ 'generateRedirects' ] === true && $ this -> packageManager -> isPackageAvailable ( 'Neos.RedirectHandler' ) ; if ( $ redirectHandlerEnabled ) { $ originalAssetResourceUri = new Uri ( $ this -> resourceManager -> getPublicPersistentResourceUri ( $ originalAssetResource ) ) ; $ newAssetResourceUri = new Uri ( $ this -> resourceManager -> getPublicPersistentResourceUri ( $ asset -> getResource ( ) ) ) ; $ uriMapping [ $ originalAssetResourceUri -> getPath ( ) ] = $ newAssetResourceUri -> getPath ( ) ; } if ( method_exists ( $ asset , 'getVariants' ) ) { $ variants = $ asset -> getVariants ( ) ; /** @var AssetVariantInterface $variant */ foreach ( $ variants as $ variant ) { $ originalVariantResource = $ variant -> getResource ( ) ; $ variant -> refresh ( ) ; if ( method_exists ( $ variant , 'getAdjustments' ) ) { foreach ( $ variant -> getAdjustments ( ) as $ adjustment ) { if ( method_exists ( $ adjustment , 'refit' ) && $ this -> imageService -> getImageSize ( $ originalAssetResource ) !== $ this -> imageService -> getImageSize ( $ resource ) ) { $ adjustment -> refit ( $ asset ) ; } } } if ( $ redirectHandlerEnabled ) { $ originalVariantResourceUri = new Uri ( $ this -> resourceManager -> getPublicPersistentResourceUri ( $ originalVariantResource ) ) ; $ newVariantResourceUri = new Uri ( $ this -> resourceManager -> getPublicPersistentResourceUri ( $ variant -> getResource ( ) ) ) ; $ uriMapping [ $ originalVariantResourceUri -> getPath ( ) ] = $ newVariantResourceUri -> getPath ( ) ; } $ this -> getRepository ( $ variant ) -> update ( $ variant ) ; } } if ( $ redirectHandlerEnabled ) { /** @var RedirectStorageInterface $redirectStorage */ $ redirectStorage = $ this -> objectManager -> get ( RedirectStorageInterface :: class ) ; foreach ( $ uriMapping as $ originalUri => $ newUri ) { $ existingRedirect = $ redirectStorage -> getOneBySourceUriPathAndHost ( $ originalUri ) ; if ( $ existingRedirect === null ) { $ redirectStorage -> addRedirect ( $ originalUri , $ newUri , 301 ) ; } } } $ this -> getRepository ( $ asset ) -> update ( $ asset ) ; $ this -> emitAssetResourceReplaced ( $ asset ) ; }
Replace resource on an asset. Takes variants and redirect handling into account.

@param AssetInterface $asset
@param PersistentResource $resource
@param array $options
@return void
Replace resource on an asset . Takes variants and redirect handling into account .