This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Forums » eZ Publish 5 Platform » How to store an uploaded file with...
expandshrink

How to store an uploaded file with the new API

How to store an uploaded file with the new API

Tuesday 12 November 2013 8:11:17 am - 3 replies

Hi every one.

Does someone manage to store an uploaded file with the new API ?

It's seems we have to create an new BinaryCreateStruct first. There's an IOService from the Core with the newBinaryCreateStructFromUploadedFile method.

But how to get this service ? The repository is not API repository but the core one.

What to do next with the BinaryCreateStruct ? How to save in the storage dir (in a way to be compatible with the cluster mode) ?

Tuesday 12 November 2013 5:36:49 pm

Good that you ask !

First things, to be fully transparent, the IO API doesn't qualify yet as a public API. This is why it is in Core only, and not in API.

That being said, it does work, and is available for you to use. As is it is now, you need a dedicated IOService instance for your usage. Images, Binary files, etc, all have their own. Creating own for your usage isn't very intuitive, but isn't very complicated either.

You need to declare a new IO service in your own services.yml. This could be an example:

services:
    me.bundle.io:
        class: %ezpublish.core.io.service.class%
        factory_service: ezpublish.core.io.factory
        factory_method: getService 
        arguments:
            - @ezpublish.core.io.handler.legacy
            - false

The "false" arguments tells the factory not to search for a setting (this really has to be refactored) containing the prefix for stored elements.

And that's pretty much it. Just obtain the service using the container, and that's it.

When it comes to using it, the IO API looks like the content API. It provides eZ\Publish\Core\IO\Values\BinaryFile objects, and accepts eZ\Publish\Core\IO\Values\BinaryFileCreateStruct or eZ\Publish\Core\IO\Values\BinaryFileUpdateStruct objects.

To create a file, first, obtain a struct using IOService::newBinaryCreateStructFromLocalFile( $sourcePath ), where $sourcePath is your file's path / source URI. Then provide the path you want to store it to, without the VarDir part !.

$ioService = $container->get( '<span style="font-size: 1.1em;">me.bundle.io' );
$struct = $ioService->newBinaryFileCreateStruct( '/tmp/myfile.zip' );
$struct->id = 'myfolder/myfile.zip'; // the file will be in var/ezdemo_site/storage/myfolder/myfile.zip
$binaryFile = $ioService->createBinaryFile( $struct ); 

You can get the URI to your file using $binaryFile->uri, as well as other metadata.

To open your file in the future, just load it with the ID you used:

$ioService = $container->get( '<span>me.bundle.io' );
$file = $ioService->load( 'myfolder/myfile.zip' ); 

Hope it helps.

Modified on Tuesday 12 November 2013 5:40:04 pm by Bertrand Dunogier

Thursday 14 November 2013 8:56:31 am

Thanks Bertrand,

It works perfectly.

Just a correction in your code : newBinaryFileCreateStruct doesn't exist, it's newBinaryFileCreateStructFromlocalFile (as you mentioned above).

One more question :

It seems core IO service have a newBinaryFileCreateStructFromUploadedFile method. Is it safe to use it ? For my case, I didn't have to rely on it because I used symfony httpfoundation uploaded file object.

Thursday 14 November 2013 11:49:34 am

Glad I could help. I think I'll add a cookbook recipe or a tutorial about that, as a matter of fact.

I'll fix my mistake, thank you. Gotta admit I got a bit pissed off by the WYSIWYG editor when it comes to code happy.gif Emoticon

Regarding newBinaryFileCreateStructFromUploadedFile, yes, it should work as expected. Feel free to look at the code, it's really simple. Having one for the HttpFoundation uploaded file object would really make sense, though.

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from