2012-12-23 22:36:59 +00:00
|
|
|
/**
|
2013-01-20 19:13:20 +00:00
|
|
|
* @fileoverview Connection to the blob server and API for the RPCs it
|
|
|
|
* provides. All blob index UI code should use this connection to contact
|
|
|
|
* the server.
|
2012-12-23 22:36:59 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
goog.provide('camlistore.ServerConnection');
|
|
|
|
|
2012-12-23 23:18:09 +00:00
|
|
|
goog.require('goog.net.XhrIo');
|
|
|
|
goog.require('goog.uri.utils');
|
2013-01-26 00:22:09 +00:00
|
|
|
goog.require('camlistore.CamliCommon');
|
2012-12-23 22:36:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {camlistore.ServerType.DiscoveryDocument} config Discovery document
|
|
|
|
* for the current server.
|
2012-12-23 23:18:09 +00:00
|
|
|
* @param {Function=} opt_sendXhr Function for sending XHRs for testing.
|
2012-12-23 22:36:59 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2012-12-23 23:18:09 +00:00
|
|
|
camlistore.ServerConnection = function(config, opt_sendXhr) {
|
2012-12-23 22:36:59 +00:00
|
|
|
/**
|
|
|
|
* @type {camlistore.ServerType.DiscoveryDocument}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.config_ = config;
|
2012-12-23 23:18:09 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-29 15:41:02 +00:00
|
|
|
* @type {function()}
|
2012-12-23 23:18:09 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.sendXhr_ = opt_sendXhr || goog.net.XhrIo.send;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
camlistore.ServerConnection.prototype.getRecentlyUpdatedPermanodes =
|
|
|
|
function(success, opt_thumbnailSize, opt_fail) {
|
|
|
|
|
|
|
|
var path = goog.uri.utils.appendPath(
|
|
|
|
this.config_.searchRoot, 'camli/search/recent');
|
|
|
|
if (!!opt_thumbnailSize) {
|
|
|
|
path = goog.uri.utils.appendParam(path, 'thumbnails', opt_thumbnailSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendXhr_(
|
|
|
|
path,
|
|
|
|
goog.bind(this.getRecentlyUpdatedPermanodesDone_, this,
|
|
|
|
success, opt_fail));
|
2012-12-23 22:36:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-12-23 23:18:09 +00:00
|
|
|
/**
|
|
|
|
* @param {Function} success Success callback.
|
|
|
|
* @param {Function?} fail Optional fail callback.
|
|
|
|
* @param {goog.events.Event} e Event that triggered this
|
|
|
|
*/
|
|
|
|
camlistore.ServerConnection.prototype.getRecentlyUpdatedPermanodesDone_ =
|
|
|
|
function(success, fail, e) {
|
|
|
|
var xhr = e.target;
|
|
|
|
var error = !xhr.isSuccess();
|
|
|
|
var result = null;
|
|
|
|
if (!error) {
|
|
|
|
result = xhr.getResponseJson();
|
|
|
|
error = !result;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
if (fail) {
|
|
|
|
fail()
|
|
|
|
} else {
|
|
|
|
// TODO(bslatkin): Add a default failure event handler to this class.
|
|
|
|
console.log('Failed XHR in ServerConnection');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
success(result);
|
|
|
|
};
|
2012-12-24 01:52:23 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-20 21:56:13 +00:00
|
|
|
* @param {function(string)} success Success callback, called with permanode
|
|
|
|
* blobref.
|
2012-12-24 01:52:23 +00:00
|
|
|
* @param {Function=} opt_fail Optional fail callback.
|
|
|
|
*/
|
2013-01-20 21:56:13 +00:00
|
|
|
camlistore.ServerConnection.prototype.createPermanode =
|
|
|
|
function(success, opt_fail) {
|
2013-01-20 19:13:20 +00:00
|
|
|
// TODO(bradfitz): stop depending on camli.js. For now, cheating:
|
2012-12-24 01:52:23 +00:00
|
|
|
camliCreateNewPermanode({
|
|
|
|
success: success,
|
|
|
|
fail: opt_fail
|
|
|
|
});
|
|
|
|
};
|
2013-01-20 21:56:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {File} file File to be uploaded.
|
|
|
|
* @param {function(string)} success Success callback, called with blobref of
|
|
|
|
* uploaded file.
|
|
|
|
* @param {Function=} opt_fail Optional fail callback.
|
|
|
|
*/
|
|
|
|
camlistore.ServerConnection.prototype.uploadFile =
|
|
|
|
function(file, success, opt_fail) {
|
|
|
|
// TODO(bradfitz): stop depending on camli.js. For now, cheating:
|
|
|
|
camliUploadFile(file, {
|
|
|
|
success: success,
|
|
|
|
fail: opt_fail
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} permanode Permanode blobref.
|
|
|
|
* @param {string} attribute Name of the attribute to set.
|
|
|
|
* @param {string} value Value to set the attribute to.
|
|
|
|
* @param {function(string)} success Success callback, called with blobref of
|
|
|
|
* uploaded file.
|
|
|
|
* @param {Function=} opt_fail Optional fail callback.
|
|
|
|
*/
|
|
|
|
camlistore.ServerConnection.prototype.newSetAttributeClaim =
|
|
|
|
function(permanode, attribute, value, success, opt_fail) {
|
|
|
|
// TODO(bradfitz): stop depending on camli.js. For now, cheating:
|
|
|
|
camliNewSetAttributeClaim(permanode, attribute, value, {
|
|
|
|
success: success,
|
|
|
|
fail: opt_fail
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-21 00:18:22 +00:00
|
|
|
/**
|
|
|
|
* @param {string} permanode Permanode blobref.
|
|
|
|
* @param {string} attribute Name of the attribute to set.
|
|
|
|
* @param {string} value Value to set the attribute to.
|
|
|
|
* @param {function(string)} success Success callback, called with blobref of
|
|
|
|
* uploaded file.
|
|
|
|
* @param {Function=} opt_fail Optional fail callback.
|
|
|
|
*/
|
|
|
|
camlistore.ServerConnection.prototype.newAddAttributeClaim =
|
|
|
|
function(permanode, attribute, value, success, opt_fail) {
|
|
|
|
// TODO(bradfitz): stop depending on camli.js. For now, cheating:
|
|
|
|
camliNewAddAttributeClaim(permanode, attribute, value, {
|
|
|
|
success: success,
|
|
|
|
fail: opt_fail
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-20 21:56:13 +00:00
|
|
|
camlistore.ServerConnection.prototype.describeWithThumbnails =
|
|
|
|
function(blobref, thumbnailSize, success, opt_fail) {
|
|
|
|
// TODO(bradfitz): stop depending on camli.js. For now, cheating:
|
|
|
|
camliDescribeBlob(blobref, {
|
|
|
|
thumbnails: thumbnailSize,
|
|
|
|
success: success,
|
|
|
|
fail: opt_fail
|
|
|
|
});
|
|
|
|
}
|