MediaSource

From Cloudrexx Development Wiki
Jump to: navigation, search

MediaSources provide abstract access to files. This allows to work with paths relative to the installation root, even if the files are in a different folder or even on a different server.

Get a MediaSource

Use previously registered MediaSources

Registered MediaSources can be fetched by their identifier as follows:

$cx->getMediaSourceManager()->getMediaType('<identifier>');

Register a MediaSource

In order to add a folder to be available in MediaBrowser, you may add a registered MediaSource. To achieve this, register an event listener for the event "mediasource:load":

// in ComponentController::registerEventListeners()
$myEventListener = new \Cx\<ComponentNS>\Model\Event\<MyEventListenerName>($this->cx);
$this->cx->getEvents()->addEventListener('mediasource.load', $myEventListener);

Then you can add the MediaSource in your listener:

    public function mediasourceLoad(MediaSourceManager $mediaBrowserConfiguration)
    {
        global $_ARRAYLANG;
        \Env::get('init')->loadLanguageData('MediaBrowser');

        $mediaSource = new MediaSource(
            '<myComponent><identifier>', // name of MediaSource, unique over the whole system
            $_ARRAYLANG['TXT_...'], // user friendly name of the MediaSource
            array(
                $this->cx->getWebsiteImagesPath() . '/...', // Path
                $this->cx->getWebsiteImagesWebPath() . '/...', // Web path
            )
        );
        $mediaType->setAccessIds(array(13)); // access IDs that are allowed to access this MediaSource
        $mediaBrowserConfiguration->addMediaType($mediaType);
    }

Create an unregistered MediaSource

If you want to work with files without adding the folder to the MediaBrowser, you need an unregistered MediaSource. The following pseude-code shows how this can be achieved. This code can simply be added where the MediaSource is needed:

$mediaSource = new MediaSource(
    '<myComponent><identifier>', // name of MediaSource, unique over the whole system
    $_ARRAYLANG['TXT_...'], // user friendly name of the MediaSource
    array(
        $this->cx->getWebsiteImagesPath() . '/...', // Path
        $this->cx->getWebsiteImagesWebPath() . '/...', // Web path
    )
);

File operations

Apart from the access to files described in DataSource the following operations can be performed:

Read files

$recursive = true;
$dir = 'icons'; 
$files = $mediaSource->getFileSystem()->getFileList($dir, $recursive);

Others

See core/MediaSource/Model/Entity/FileSystem.interface.php