(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);
|
}
|
};
|
})();
|