Neos\Flow\ResourceManagement\ResourceManager::importResource PHP Method

importResource() public method

On a successful import this method returns a PersistentResource object representing the newly imported persistent resource and automatically publishes it to the configured publication target.
public importResource ( string | resource $source, string $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, string $forcedPersistenceObjectIdentifier = null ) : PersistentResource
$source string | resource A URI (can therefore also be a path and filename) or a PHP resource stream(!) pointing to the PersistentResource to import
$collectionName string Name of the collection this new resource should be added to. By default the standard collection for persistent resources is used.
$forcedPersistenceObjectIdentifier string INTERNAL: Force the object identifier for this resource to the given UUID
return PersistentResource A resource object representing the imported resource
    public function importResource($source, $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, $forcedPersistenceObjectIdentifier = null)
    {
        $this->initialize();
        if (!isset($this->collections[$collectionName])) {
            throw new Exception(sprintf('Tried to import a file into the resource collection "%s" but no such collection exists. Please check your settings and the code which triggered the import.', $collectionName), 1375196643);
        }
        /* @var CollectionInterface $collection */
        $collection = $this->collections[$collectionName];
        try {
            $resource = $collection->importResource($source);
            if ($forcedPersistenceObjectIdentifier !== null) {
                ObjectAccess::setProperty($resource, 'Persistence_Object_Identifier', $forcedPersistenceObjectIdentifier, true);
            }
            if (!is_resource($source)) {
                $pathInfo = UnicodeFunctions::pathinfo($source);
                $resource->setFilename($pathInfo['basename']);
            }
        } catch (Exception $exception) {
            throw new Exception(sprintf('Importing a file into the resource collection "%s" failed: %s', $collectionName, $exception->getMessage()), 1375197120, $exception);
        }
        $this->resourceRepository->add($resource);
        $this->systemLogger->log(sprintf('Successfully imported file "%s" into the resource collection "%s" (storage: %s, a %s. SHA1: %s)', $source, $collectionName, $collection->getStorage()->getName(), get_class($collection), $resource->getSha1()), LOG_DEBUG);
        return $resource;
    }

Usage Example

 /**
  * Imports a new image and persists it, including one variant
  *
  * @param string $importUri
  * @return string
  */
 public function importAction($importUri)
 {
     $imageResource = $this->resourceManager->importResource($importUri);
     $image = new Image($imageResource);
     $imageVariant = new ImageVariant($image);
     $this->assetRepository->add($image);
     $this->assetRepository->add($imageVariant);
     $this->response->setHeader('X-ImageVariantUuid', $this->persistenceManager->getIdentifierByObject($imageVariant));
     return 'ok';
 }
All Usage Examples Of Neos\Flow\ResourceManagement\ResourceManager::importResource