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

importResourceFromContent() public method

The given content typically is binary data or a text format. On a successful import this method returns a PersistentResource object representing the imported content and automatically publishes it to the configured publication target. The specified filename will be used when presenting the resource to a user. Its file extension is important because the resource management will derive the IANA Media Type from it.
public importResourceFromContent ( string $content, string $filename, string $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, string $forcedPersistenceObjectIdentifier = null ) : PersistentResource
$content string The binary content to import
$filename string The filename to use for the newly generated resource
$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 importResourceFromContent($content, $filename, $collectionName = ResourceManager::DEFAULT_PERSISTENT_COLLECTION_NAME, $forcedPersistenceObjectIdentifier = null)
    {
        if (!is_string($content)) {
            throw new Exception(sprintf('Tried to import content into the resource collection "%s" but the given content was a %s instead of a string.', $collectionName, gettype($content)), 1380878115);
        }
        $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), 1380878131);
        }
        /* @var CollectionInterface $collection */
        $collection = $this->collections[$collectionName];
        try {
            $resource = $collection->importResourceFromContent($content);
            $resource->setFilename($filename);
            if ($forcedPersistenceObjectIdentifier !== null) {
                ObjectAccess::setProperty($resource, 'Persistence_Object_Identifier', $forcedPersistenceObjectIdentifier, true);
            }
        } catch (Exception $exception) {
            throw new Exception(sprintf('Importing content into the resource collection "%s" failed: %s', $collectionName, $exception->getMessage()), 1381156155, $exception);
        }
        $this->resourceRepository->add($resource);
        $this->systemLogger->log(sprintf('Successfully imported content into the resource collection "%s" (storage: %s, a %s. SHA1: %s)', $collectionName, $collection->getStorage()->getName(), get_class($collection->getStorage()), $resource->getSha1()), LOG_DEBUG);
        return $resource;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @return Asset
  * @throws \Neos\Flow\ResourceManagement\Exception
  */
 protected function buildAssetObject()
 {
     $resource = $this->resourceManager->importResourceFromContent('Test', 'test.txt');
     $asset = new Asset($resource);
     $this->assetRepository->add($asset);
     return $asset;
 }
All Usage Examples Of Neos\Flow\ResourceManagement\ResourceManager::importResourceFromContent