Import media from url

Hi! In my company, we had two projects using enonic that we needed the creation of a media content using a url instead of a file from multipart-form, for example. If there’s any way I could create this media, please help me. I’ve tried in a controller, do a request with httpClientLib, create a stream with ioLib using the response and then creating a media object. This doesn’t worked. Would be nice for next versions, implement a function like content.createMedia(), but content.createMediaFromUrl() or something like this.

Thanks

1 Like

Hi.

Do you have source-code for your example?

Hi @PVMerlo

We are planning to improve stream (binary) support in http-client lib, by adding a bodyStream property on the result of the request function. Then you will be able to pass the result to createMedia data parameter.

It will work something like this:

var response = httpClientLib.request({
    url: 'http://host/image.png',
    method: 'GET'
});

var responseStream = response.bodyStream; // this field is currently missing 

var mediaResult = contentLib.createMedia({
    name: 'myImage',
    parentPath: '/some/path',
    mimeType: 'image/png',
    data: responseStream
});

Then it becomes simple to create your own createMediaFromUrl() function.

Hopefully this will be a part of the next 6.6 version.

2 Likes

Nice, @aro! Thanks :smiley:

Stream support in http-client has been implemented and it will be in 6.6

This will be included in the 6.6 version of Enonic XP. Hopefully we will release next week.

1 Like

Here is also an example from ContentHive widget of saving an image from URL by creating a ByteSource in Java and then using createMedia in javascript:

https://bitbucket.org/openxp/contenthive/src/b1af218126d470968dcaca08ba9fbed54988d083/src/main/java/openxp/app/contenthive/HttpService.java?at=master&fileviewer=file-view-default#HttpService.java-26

https://bitbucket.org/openxp/contenthive/src/b1af218126d470968dcaca08ba9fbed54988d083/src/main/resources/services/newbees/newbees.js?at=master&fileviewer=file-view-default#newbees.js-248

3 Likes