symfonysonata-adminsonatarackspace-cloudsonata-media-bundle

Sonata Media CDN Rackspace


I have the next problem with Sonata Media: I'm trying to use the Rackspace CDN for uploading images: My config file looks like this based on current documentation:

cdn:
    server:
        path: %cdn_url%
filesystem:
    local:
        directory:  %kernel.root_dir%/../web/uploads/media
        create:     false
    rackspace:
        url: %rackspace.opencloud.host%
        secret:
            username: %rackspace.opencloud.username%
            apiKey: %rackspace.opencloud.api_key%
        region: LON
        containerName: projectName
        create_container: false
    replicate:
        master: sonata.media.adapter.filesystem.opencloud
        slave: sonata.media.adapter.filesystem.local

And on providers config:

  providers:
    image:
        filesystem: sonata.media.filesystem.replicate
        cdn: sonata.media.cdn.server
        resizer: sonata.media.resizer.square
        allowed_extensions: ['jpg', 'png', 'gif', 'jpeg']
        allowed_mime_types: ['image/pjpeg','image/jpeg','image/png','image/x-png', 'image/gif']

The problem is(how I discovered this bug)if Rackspace is down or incorrect username/password are provided on every page of the app I'm getting this answer:

Client error response [status code] 401 [reason phrase] Unauthorized [url] https://lon.auth.api.rackspacecloud.com/v2.0/tokens

This is because Gaufrette Opencloud tries to create a connection on Kernel load.

The quickest solution as a temporary fix was to create a compiler pass and check if the authenticate method returns false then replace argument 0 for replicate definition with the local filesystem adaptor.

My questions are:

  1. How can I avoid creating the Rackspace connection on Kernel Load?
  2. In case Rackspace is down how can I swap between Rackspace or other adapter(local or other ftp server)

Thank you in advance and please in case there are not sufficient information provided please leave a comment.


Solution

  • Apparently there is a solution for lazy loading implemented in Gaufrette: https://github.com/KnpLabs/KnpGaufretteBundle/issues/72

    All I had to do is:

    sonata.media.adapter.open_stack:
        class: OpenCloud\Rackspace
        arguments: [ %rackspace.opencloud.host%, { username: %rackspace.opencloud.username%, apiKey: %rackspace.opencloud.api_key% }]
    sonata.media.adapter.object_store_factory:
        class: Gaufrette\Adapter\OpenStackCloudFiles\ObjectStoreFactory
        arguments: [ @sonata.media.adapter.open_stack, "LON", ""]
    sonata.media.adapter.filesystem.lazyopencloud:
        class: Gaufrette\Adapter\LazyOpenCloud
        arguments: [ @sonata.media.adapter.object_store_factory, %rackspace.opencloud.container_name%]
    

    And change replicate master to sonata.media.adapter.filesystem.lazyopencloud

    Hope it helps :)