// Boiler plate to setup cloud providers registry var CLOUD_PROVIDERS; if (typeof CLOUD_PROVIDERS === 'undefined') CLOUD_PROVIDERS = eval('(function() { try { return CLOUD_PROVIDERS || {} } catch(e) { return {} } })()'); (function() { // @todo: This may need adjusting to support fetching binary. function xhrGET(url, onload, onerror) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'text'; xhr.onload = function () { if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { onload(xhr.response); } else { onerror(new Error('Request failed')); } }; xhr.onerror = onerror; xhr.send(null); } // @todo: this may need adjusting to support putting binary. function xhrPUT(url, data, onload, onerror) { var xhr = new XMLHttpRequest(); xhr.open('PUT', url, true); xhr.responseType = 'text'; xhr.onload = function () { if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { onload(xhr.response); } else { onerror(new Error('Request failed')); } }; xhr.onerror = onerror; xhr.send(data); } function xhrDELETE(url, onload, onerror) { var xhr = new XMLHttpRequest(); xhr.open('DELETE', url, true); xhr.responseType = 'text'; xhr.onload = function() { if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { onload(xhr.response); } else { onerror(new Error('Request failed')); } }; xhr.onerror = onerror; xhr.send(); } var settings = { remoteAPIEndpoint: 'http://asmjs.brigadoon.outoforder.cc/cloud' }; var provider = { vendor: 'Urkle!', allFiles: function(options, onsuccess, onerror) { xhrGET(settings.remoteAPIEndpoint + '/storage/files?appToken=' + encodeURIComponent(options.applicationtoken), function(data) { var json = JSON.parse(data), ret = []; ret = json.data.map(function(f) { if (f.url.substr(0, 1) == '/') { f.url = settings.remoteAPIEndpoint + f.url; } f.timestamp = new Date(parseInt(f.timestamp)); return f; }); onsuccess(ret) }, onerror); }, read: function(options, url, onsuccess, onerror) { xhrGET(url, onsuccess, onerror); }, write: function(options, fileinfo, data, onsuccess, onerror) { var q = 'appToken=' + encodeURIComponent(options.applicationtoken) +'&path=' + encodeURIComponent(fileinfo.path) +'×tamp=' + encodeURIComponent(fileinfo.timestamp.getTime()); xhrPUT(settings.remoteAPIEndpoint + '/storage/files?' + q, data, onsuccess, onerror); }, rm: function(options, fileinfo, onsuccess, onerror) { xhrDELETE(settings.remoteAPIEndpoint + '/storage/files/' + fileinfo.path, onsuccess, onerror); }, isAvailable: function(options) { return false; } }; // expose out to the page CLOUD_PROVIDERS['Urkle'] = provider; })();