emscripten Humble Cloud interface
Edward Rudd
2015-08-11 2e32e892540d1afbb59ea056cfe80226174a8f6f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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, isBin, onload, onerror) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.responseType = isBin ? 'arraybuffer' : '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), false, 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, true, onsuccess, onerror);
        },
        write: function(options, fileinfo, data, onsuccess, onerror) {
            var q = 'appToken=' + encodeURIComponent(options.applicationtoken)
                   +'&path=' + encodeURIComponent(fileinfo.path)
                   +'&timestamp=' + 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 true;
        }
    };
 
    // expose out to the page
    CLOUD_PROVIDERS['Urkle'] = provider;
})();