Images and content.create()

Enonic version: 6.0.0
OS: OSX

I am creating profiles for my users based on their Facebook profile. I wish to fetch profile pictures from Facebook when creating the profile content, and include the picture with the content.

How is this done? The example on content.create() is not very helpful.

Currently, I manage to get a user logged in (to Facebook), giving me an access token. I then fech the profile picture in the back-end and save it to a temporary location, ready for uploading into the content repository. I’m just not seeing how I can get the image data into the “data”-structure in the JSON…

We do not support creating images in Javascript yet. It’s on the plan, but cannot tell you when that is. The only thing you can do now is to integrate this using Java. You can bridge Javascript and Java anytime and there you will have a full Java API that supports everything. Does not have any examples on that in hand, but you can look at our libs to see how all of this works.

https://github.com/enonic/xp/tree/master/modules/lib-content

1 Like

Now that the portal.createMedia()-function exists, I’m wondering how I can get the request body as a stream when a user uploads an image file.

exports.post = function(req) {
  var stream = req.getInputStream() // Does not work
}

Have you got an example on how to this? It seems to me that req only contains the header data.

Look at the multipart functions in lib-portal:

This requires that the request is a multipart POST, that is what you normally use for uploading files.

1 Like

Awesome, thanks a bunch!

Now, my only problem is that the image isn’t visible in the content manager, due to the image being uploaded directly into the master branch, without a corresponding item in the draft branch. This has been promised to be handled in a future release.

Publish function will be available in 6.4! Start by creating it in the draft branch instead!