emscripten Humble Cloud interface
Aaron Mandle
2014-08-22 943ba1a780347d98f7cb53f972218668709ed0d2
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(function () {
  'use strict';
 
  window.CloudFile = function () {
    var that = this;
    var API_ROOT = '/api/v1/cloudfile/';
 
    function apiRoute(resource) {
      return window.location.origin + API_ROOT + resource;
    }
 
    this.uploadEndPoints = [];
 
    var isDirectory = function(path) {
      return path[path.length - 1] === '/';
    };
 
    var requestUploadUrls = function (completedCallback) {
      $.ajax({
        url: apiRoute('generate_upload_urls'),
        type: "GET",
        dataType: 'json',
        success: function (response) {
          that.uploadEndPoints = that.uploadEndPoints.concat(response);
          completedCallback();
        }
      });
    };
 
    this.write = function (filepath, value, onSuccess, onError) {
      var formdata = new FormData();
      formdata.append('data', new Blob([value]), filepath);
      var endPoint = that.uploadEndPoints.pop();
      if (endPoint === undefined) {
        requestUploadUrls(function () {
          that.write(filepath, value)
        });
      } else {
        $.ajax({
          url: endPoint,
          type: "POST",
          data: formdata,
          dataType: 'json',
          processData: false,
          contentType: false,
          success: onSuccess,
          error: onError
        });
      }
    };
 
    this.read = function (filepath, onSuccess, onError) {
      $.ajax({
        url: apiRoute(filepath),
        type: "GET",
        success: function(result) {
          if (isDirectory(filepath)) {
            onSuccess(JSON.parse(result));
          } else {
            onSuccess(result);
          }
        },
        error: onError
      });
    };
 
    this.rm = function (filepath, onSuccess, onError) {
      $.ajax({
        url: apiRoute(filepath),
        type: "DELETE",
        dataType: 'json',
        success: onSuccess,
        error: onError
      });
    }
  }
})();
 
 
var CLOUD_PROVIDERS;
if (!CLOUD_PROVIDERS) CLOUD_PROVIDERS = (typeof CLOUD_PROVIDERS !== 'undefined' ? CLOUD_PROVIDERS : null) || {};
 
(function () {
  'use-strict';
 
  var cloudFile = new CloudFile();
 
  var namespacedPath = function (appToken, resource) {
    if (resource[0] !== '/') resource = '/' + resource;
    return appToken + resource;
  };
 
  CLOUD_PROVIDERS['CloudFile'] = {
    vendor: 'Humble Bundle Inc',
    allFiles: function (options, onSuccess, onError) {
      cloudFile.read(
              namespacedPath(options.applicationToken, '/'),
              onSuccess,
              onError
      )
    },
    read: function (options, path, onSuccess, onError) {
      cloudFile.read(
        namespacedPath(options.applicationToken, path),
        onSuccess,
        function (err) {
          if (err.status == 404) {
            // path doesn't exist
            onSuccess([]);
          } else {
            onError(err);
          }
        }
      );
    },
    write: function (options, fileinfo, data, onSuccess, onError) {
      cloudFile.write(namespacedPath(options.applicationToken, fileinfo.path), data, onSuccess, onError)
    },
    rm: function (options, fileinfo, onSuccess, onError) {
      cloudFile.rm(namespacedPath(options.applicationToken, fileinfo.path), onSuccess, onError);
    }
  };
})();