Hexagon Geospatial
MENU

Smart M.App

Looking for answers in regards to M.Apps, M.App Exchange or M.App Studio? Smart M.App Tech Discussion board is where you can search, ask your questions and assist others by sharing your knowledge. Join the conversation, connect, contribute and share.
Showing results for 
Search instead for 
Do you mean 
Reply
Frequent Contributor
Posts: 100
Registered: ‎10-16-2015

Example code: download content from Chest using promises

[ Edited ]

Hi,

 

I've written a small script to download a (geojson or csv) file from the chest using unix-like paths. It relies on jQuery's $.Deferred. 

/**
 * create a promise to download a file from the chest
 * this code needs to set id of the root where to search from
 * @param path [<directory>/].../<filename>.[csv|geojson|json]
 * @example 
 *  download('province/' + province + '/municipalities/' + municipality + '.geojson')
 *      .then( function( content ) {
 *          doSomethingWith( content );
 *      })
 *      .fail(function( error ) { alert error });
 * @returns {*} content of the file
 * @requires jquery
 */
function download ( path ) {

    var rootId = "<id-of-the-root-folder>";

    function getExtension( path ) {
        var match = path.match(/\.([^/.]+)$/);
        return match.length ? match[1] : null;
    }
    var extension = getExtension(path);

    /**
* recursive function to get the folder instance from the path
*/
function getFolder( path, parent ) { if( !parent ) { parent = rootId; } var url = 'api/v1/folders/' + parent + '.json?profile=full&start=0&maxresults=100'; var d = $.Deferred(); $GP.m_app.utils.connection( url ).then( function( folderResponse ) { var parts = path.split('/'); if( parts.length > 1 ) { var folderName = parts.shift(); var subPath = parts.join('/'); var result = folderResponse.entity.results[0]; var found = result.children.filter(function (child) { return child.name == folderName; }) if (found.length) { var folder = found[0];
// recurs getFolder(subPath, folder.id).then(function (folderResponse) { d.resolve(folderResponse); }).fail( function( error ) { d.reject( error ) }) } else { d.reject(Error('Folder not found: ' + folderName)) } } else { d.resolve( folderResponse ) } }) return d.promise(); } var d = $.Deferred(); getFolder( path ).then( function( folderResponse ) { var parts = path.split('/'); var fileName = parts[ parts.length -1 ]; var found = folderResponse.entity.results[0].children.filter(function (result) { return result.title == fileName; }); if (found.length) { var file = found[0]; var attachment; $GP.m_app.utils.connection('api/v1/items/' + file.id + '.json').then(function (response) { switch (extension) { case 'geojson': { for (var filename in response.entity.results[0].attachments) { if( filename.hasOwnProperty()) continue; if (filename.match('geojson.attachment')) { attachment = filename; break; } } if (!attachment) { d.reject(Error(filename + 'has no ' + extension + ' attachment')); } break; } case 'csv': attachment = 'default'; break; } if (attachment) {
// get the content $GP.m_app.utils.connection('api/v1/items/' + file.id + '/attachments/' + encodeURI(attachment)).then(function (response) { switch (extension) { case 'geojson': case 'json': d.resolve(JSON.parse(response.entity)); break; case 'csv': d.resolve(d3.csv.parse(response.entity)) break; default: d.resolve(response.entity); } }).fail( function( error ) { d.reject(error); }) } else { d.resolve(response); } }); } else { d.reject(Error('file not found:' + fileName)); } }); return d.promise(); }

 

 Tell me what you think about this.

Do you need immediate support?
Please submit a Ticket through our
Development Ticket Portal.