Upload xml/json files as content-type

Lets say you have a batch of xml or json files.
(Perhaps exported from DB, or collected from some REST API)

Which you want to “import” into Enonic XP as some content-type, and them display as html and in it’s original form (and perhaps some stripped version)

Currently one can upload XML/JSON files, but they end up as content type “Code”.

I can’t find any documentation for this “base” content type here:
http://xp.readthedocs.org/en/stable/developer/site/content-types/base-types.html?highlight=code

So perhaps its possible to make some smart controller for content-type “Code”?

Here’s a stupid idea:

What if it was possible to make a custom content-type, with the section in the content type definition xml file being empty. Then make it possible somehow to upload XML/JSON files as this content-type.

Why do this: Well some of these XML/JSON files may have deeply nested structures which may take a very long time to write content-type definition for. I would rather spend my time on the view…

So I made a template to handle content-type “Code”, used my page controller, and made and added a Code part to it.

In this example Code part controller stream becomes undefined. Which it shouldn’t. Bug?

var assign = require(’/lib/assign’);
var contentLib = require(’/lib/xp/content’);
var portal = require(’/lib/xp/portal’);
var thymeleaf = require(’/lib/xp/thymeleaf’);

exports.get = function () {
var view = resolve(‘code.html’);
//var component = portal.getComponent();
var partConfig = portal.getComponent().config;

var content = portal.getContent();
//log.info(JSON.stringify(content, null, 4));
var attachments = contentLib.getAttachments(content._id);
//log.info(JSON.stringify(attachments, null, 4));
var firstAttachmentName = attachments[Object.keys(attachments)[0]]['name'];
log.info(firstAttachmentName);
var stream = contentLib.getAttachmentStream({
    key: content._id,
    name: firstAttachmentName
});
log.info(JSON.stringify(stream, null, 4));
var model = assign({}, partConfig, {});
return {
    body: thymeleaf.render(view, model)
}

};

Forgot to mention I’m currently on Enonic 6.4.2

Workaround using httpClientLib.request

var assign = require(’/lib/assign’);
var contentLib = require(’/lib/xp/content’);
var httpClientLib = require(’/lib/xp/http-client’);
var portal = require(’/lib/xp/portal’);
var thymeleaf = require(’/lib/xp/thymeleaf’);

function getAttachment(id) {
return httpClientLib.request({
url: portal.attachmentUrl({ id: id, type: ‘absolute’ }),
method: ‘GET’
}).body;
}

exports.get = function () {
var view = resolve(‘code.html’);
//var component = portal.getComponent();
var partConfig = portal.getComponent().config;

var content = portal.getContent();
//log.info(JSON.stringify(content, null, 4));
var attachments = contentLib.getAttachments(content._id);
//log.info(JSON.stringify(attachments, null, 4));
var firstAttachmentName = attachments[Object.keys(attachments)[0]]['name'];
log.info(firstAttachmentName);
var stream = contentLib.getAttachmentStream({
    key: content._id,
    name: firstAttachmentName
});
log.info(JSON.stringify(stream, null, 4));

/*
var url = portal.attachmentUrl({
id: content._id,
});
log.info(JSON.stringify(url, null, 4));
*/

var file = getAttachment(content._id);
log.info(JSON.stringify(file, null, 4));

/*
return {
‘status’: 303,
‘redirect’: url
}
*/

//var model = assign({}, file, {});
var model = {
    file: file
};
return {
    body: thymeleaf.render(view, model)
};

};

Just mentioning that one will have to publish each uploaded file to make it available.
So I’m not very happy with this approach.

Would rather like some API with CRUD. Making another Topic for that feature request.

When you upload an image to Enonic, it’s smart enough to find meta information.

So if there was something like a custom file upload controller, then that could be as smart as the image uploader.

Another possible problem with using file upload might be indexing and searching.
Are uploaded files searchable?

I’m guessing normal content types would be indexed in such a way you could search for specific keys.

For example if it had a input type textline with name=“MySpecialKey” you could narrow search to just that key?

I’m guessing that would not be possible for uploaded files.

Hi.

At the moment, nothing is extracted from attachments, but this will be implemented in 6.6
For attachments in general, an extraction algorithm will extract whats possible from the file and indexed as one separate field, e.g attachment.content or somthing similar. Normal content are indexed with keys and values since it is structured data.

So; a content with an attachment would look like:

name = 'my-attachment'
displayName = 'My Attachment
data.something = 'fisk'
attachment.content = 'once upon a time something something'

So, the thing I think you would like would require the data to be parsed as a structured document first, meaning:
-> fixed format and special handling of the data given (e.g a special content type)
-> transforming to a structured document
-> normal, automatic indexing of document properties

Hi,

It’s possible to the unstructured content type to import anything as content. You define your properties as you go. No limitations.

Here’s an example of an unstructured content create using the API:

var fp = req.params;
var result = contentSvc.create({
    name: 'Signup -' + Math.floor((Math.random() * 1000000000) + 1),
    parentPath: saveLocation,
    displayName: fp['name'],
    branch: 'draft',
    contentType: 'base:unstructured',
    data: {
        name: fp['name'],
        phone: fp['phone'],
        email: fp['email'],
        company: fp['company'],
        message: fp['message']
    }
});