[SOLVED] Define Folders for PersistentResource

Hi!

I studied the following manual: https://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/ResourceManagement.html

However, one (for me important) point I didn’t get. I’m very sorry, when I missed something in the manual.

I would like to use different PRIVATE folders to store different type of object resources. For example the image resources of the objects “Image” at: Resources\Private\Persistence\Images, some PDF files at: Resources\Private\Persistence\Invoices and so on.

So, I guess a little bit and ask you for your thoughts.
Settings.yaml:

Neos:
  Flow:
    resource:
      targets:
        myImagePersistentResourcesTarget:
          target: 'Neos\Flow\ResourceManagement\Target\FileSystemSymlinkTarget'
          targetOptions:
            path: 'resource://Acme.Demo/Private/Persistent/Images/'
            baseUri: '_Resources/Persistent/'

This is my Image.php:

/**
 * @Flow\Entity
 */
class Image
{
    /**
     * @var \Neos\Flow\ResourceManagement\PersistentResource
     * @ORM\OneToOne
     */
    protected $file;

    /**
     * @return \Neos\Flow\ResourceManagement\PersistentResource
     */
    public function getFile(): \Neos\Flow\ResourceManagement\PersistentResource
    {
        return $this->file;
    }

    /**
     * @param \Neos\Flow\ResourceManagement\PersistentResource $file
     */
    public function setFile(\Neos\Flow\ResourceManagement\PersistentResource $file): void
    {
        $file->setRelativePublicationPath("myImagePersistentResourcesTarget");
        $this->file = $file;
    }
}

Is this somehow the right direction? I guess baseUri is wrong - but I do not fully understand, what’s expected here.

Thank you very much for your help!

Tobias

The Resource system works with storages and targets which together form a collection.

It sounds to me like you want to have a different STORAGE for your resources, not (or maybe both) a target. The target defines how the items in the storage are made available to the public (via the configured baseUri in this case), but the storage defines where the binary data is actually stored, which (depending on the used implementation class) could be a folder in the file system or a cloud drive or anything else than can store the information. So I guess you want to define a a custom “collection” with a custom storage and target and definitely configure the storage path to the path you want.

It seems I’ve made a mess of things here. Then let me take another step back:

Currently I save my object in the database, take the identifier and name my PDF file the same, then save it with file_put_contents() and so on.

The chapter I read looked like an elegant way for me to simply attach the PDF file my system just created to the object and let Flow do the rest.

Now after your hint I understood that I have to go back one step further and create a new collection. Concerning collections I only find the small chapter https://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/ResourceManagement.html#collections
Is there a more in-depth guide somewhere on how to create and use my own collections?

Thank you all very much!

Okay, I found a suitable solution and would like to share it with others having the same question / problem:

Settings.yaml:

Neos:
  Flow:
    resource:
      storages:
       yourNameStorage:
          storage: 'Neos\Flow\ResourceManagement\Storage\WritableFileSystemStorage'
          storageOptions:
            path: '%FLOW_PATH_DATA%YOUR/FOLDER/'

      targets:
        yourNameTarget:
          target: 'Acme\Demo\ResourceManagement\Target\PrivateTarget'
      collections:
        yourNameCollection:
          storage: 'yourNameStorage'
          target: 'yourNameTarget'

I created the following class “Acme\Demo\ResourceManagement\Target\PrivateTarget”:

class PrivateTarget implements TargetInterface
{
    public function getName()
    {
        // TODO: Implement getName() method.
    }

    public function publishCollection(CollectionInterface $collection)
    {
        // TODO: Implement publishCollection() method.
    }

    public function publishResource(PersistentResource $resource, CollectionInterface $collection)
    {
        // TODO: Implement publishResource() method.
    }

    public function unpublishResource(PersistentResource $resource)
    {
        // TODO: Implement unpublishResource() method.
    }

    public function getPublicStaticResourceUri($relativePathAndFilename)
    {
        // TODO: Implement getPublicStaticResourceUri() method.
    }

    public function getPublicPersistentResourceUri(PersistentResource $resource)
    {
        // TODO: Implement getPublicPersistentResourceUri() method.
    }

}

The object, which should contain the file:

/**
 * @var \Neos\Flow\ResourceManagement\PersistentResource
 * @ORM\OneToOne
 */
protected $file;

/**
 * @return \Neos\Flow\ResourceManagement\PersistentResource
 */
public function getFile(): \Neos\Flow\ResourceManagement\PersistentResource
{
    return $this->file;
}

/**
 * @param \Neos\Flow\ResourceManagement\PersistentResource $file
 */
public function setFile(\Neos\Flow\ResourceManagement\PersistentResource $file): void
{
    $this->file = $file;
}

Connect file with object:

$file = $this->resourceManager->importResourceFromContent("hellowWorld", "nameDoesntMatter.txt", 'yourNameCollection');

I hope, this helps someone else out there :slight_smile:

2 Likes