emscripten Humble Cloud interface
Aaron Mandle
2014-08-21 ad564e0822e384fba0f6d825d08f9b56943db7e6
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
(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 requestUploadUrls = function (completedCallback) {
      $.ajax({
        url: apiRoute('generate_upload_urls'),
        type: "GET",
        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,
          processData: false,
          contentType: false,
          success: onSuccess,
          error: onError
        });
      }
    };
 
    this.read = function (filepath, onSuccess, onError) {
      $.ajax({
        url: apiRoute(filepath),
        type: "GET",
        success: onSuccess,
        error: onError
      });
    };
 
    this.rm = function (filepath, onSuccess, onError) {
      $.ajax({
        url: apiRoute(filepath),
        type: "DELETE",
        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) {
    return appToken + resource;
  };
  CLOUD_PROVIDERS['CloudFile'] = {
    vendor: 'Humble Bundle Inc',
    listFiles: function (options, onSuccess, onError) {
      cloudFile.read(namespacedPath(options.applicationToken, '/'), onSuccess, function (err) {
        if (err.status == 404) { // not a real directory
          onSuccess([]);
        }
      });
    },
    downloadFile: function (options, url, onSuccess, onError) {
      cloudFile.read(namespacedPath(options.applicationToken, url), onSuccess, onError)
    },
    uploadFile: function (options, fileinfo, data, onSuccess, onError) {
      cloudFile.write(namespacedPath(options.applicationToken, fileinfo.path), data, onSuccess, onError)
    },
    removeFile: function (options, fileinfo, onSuccess, onError) {
      cloudFile.rm(namespacedPath(options.applicationToken, fileinfo.path), onSuccess, onError);
    }
  };
})();