emscripten Humble Cloud interface
Edward Rudd
2014-08-20 5d19454921edad7c4c6e4d54da23d62c8193fe2c
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
var LibraryHUMBLE_CLOUD = {
    $HUMBLE_CLOUD__deps: ['$FS', '$MEMFS', '$PATH'],
    $HUMBLE_CLOUD__postset: 'HUMBLE_CLOUD.staticInit()',
    $HUMBLE_CLOUD: {
        applicationToken: '__NOTOKEN__',
        remoteAPIEndpoint: 'http://asmjs.brigadoon.outoforder.cc/cloud/',
 
        staticInit: function() {
            console.log('Humble Cloud Init');
            addRunDependency('HUMBLE_CLOUD');
            FS.createFolder('/', 'user_cloud', true, true);
            FS.mount(HUMBLE_CLOUD, {}, '/user_cloud');
            console.log('Doing stuff to synchronize files');
            removeRunDependency('HUMBLE_CLOUD');
        },
 
        mount: function(mount) {
            console.log("Mounting", mount);
            return MEMFS.mount.apply(null, arguments);
        },
        syncfs: function(mount, populate, callback) {
            window.HB_mount = mount;
            console.log("Syncing", mount, populate);
            // re-use IDBFS.getLocalSet for now
            IDBFS.getLocalSet(mount, function(err, local) {
                console.log('local files', local);
 
                mount.type.getRemoteSet(mount, function(err, remote) {
                  console.log('remote files', remote);
                });
            });
        },
 
        xhrLoad: function(url, onload, onerror) {
          var xhr = new XMLHttpRequest();
          xhr.open('GET', url, true);
          xhr.onload = function () {
            if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) {
              onload(xhr.response);
            } else {
              onerror();
            }
          };
          xhr.onerror = onerror;
          xhr.send(null);
        },
        getRemoteSet: function(mount, callback) {
          var url = mount.type.remoteAPIEndpoint + 'storage/files';
          HUMBLE_CLOUD.xhrLoad(url, function(data) {
            var entries = {},
                json = JSON.parse(data);
            for(var k in json.data) {
              var f = json.data[k];
              var absolute = PATH.join2(mount.mountpoint, f.path);
              entries[absolute] = {
                timestamp: new Date(parseInt(f.timestamp)),
                size: f.size
              }
            }
            return callback(null, { type: 'remote', entries: entries } );
          }, function(e) {
            callback(e || new Error('failed request'));
          });
        }
    },
    humble_cloud_init: function() {
        // Dummy function to pull in the rest of the functions
    }
};
 
autoAddDeps(LibraryHUMBLE_CLOUD, '$HUMBLE_CLOUD');
mergeInto(LibraryManager.library, LibraryHUMBLE_CLOUD);