createMedia generating unknown type

Enonic version: 7.2.0
OS: YourOS

I’ve tried to create an image media using the createMedia function but it resulted in a media file of unknown format, could it be because of the response encode?

var response = libs.http.request({
          url: basePath + imageUrl,
          method: 'GET'
        });

imageContent = libs.content.createMedia({
              name: title,
              parentPath: path + "/images/" + category,
              mimeType: 'image/jpg',
              branch: "draft",
              data: response.bodyStream
            })

Hi Paulo,

Your code looks correct to me. I once experienced that the bodyStream was stripped away from the response object because I passed it as a parameter into another function from which I ran createMedia(), but if the two commands in your code above are within the same function then I don’t think that’s the problem here.

What is the size of the created media file? Is it 0 bytes, or if it is not: Can you check if it’s the same amount of bytes as the original remote image? I’m not sure if it’s supposed to be the same amount of bytes, or if maybe Enonic does some processing on the image file that modifies it. Maybe you can inspect the raw file contents as well, at least the first line to see if there is any difference.

Best wishes,
Bjørnar

If I click on the attachment it downloads the correct image and looking at the data its correct, its just the data -> media info that doesn’t match and there is no extension in the name.

Just to report that the issue was the mimetype sent to createMedia didn’t match with the actual file sent to it causing this behavior.

3 Likes