You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/04/02 17:21:16 UTC

[11/41] git commit: Removed call delay for upload and implemented a retry on failure. MUCH faster and MUCH, MUCH more reliable.

Removed call delay for upload and implemented a retry on failure. MUCH faster and MUCH, MUCH more reliable.


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/bc03fb82
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/bc03fb82
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/bc03fb82

Branch: refs/pull/96/head
Commit: bc03fb8296d3dc9b608c0441550c929986ae76cd
Parents: da8ecff
Author: ryan bridges <rb...@apigee.com>
Authored: Tue Apr 1 14:17:35 2014 -0400
Committer: ryan bridges <rb...@apigee.com>
Committed: Tue Apr 1 14:17:35 2014 -0400

----------------------------------------------------------------------
 sdks/html5-javascript/lib/modules/Asset.js | 30 ++++++++++++++++---------
 1 file changed, 20 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bc03fb82/sdks/html5-javascript/lib/modules/Asset.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Asset.js b/sdks/html5-javascript/lib/modules/Asset.js
index b5bc68a..52cfd96 100644
--- a/sdks/html5-javascript/lib/modules/Asset.js
+++ b/sdks/html5-javascript/lib/modules/Asset.js
@@ -109,18 +109,30 @@ Usergrid.Asset.prototype.upload = function(data, callback) {
 		return;
 	}
 	var self = this;
+	var args=arguments;
+	var attempts=self.get("attempts");
+	if(isNaN(attempts)){
+		attempts=3;
+	}
+	self.set('content-type', data.type);
+	self.set('size', data.size);
 	var endpoint = [this._client.URI, this._client.orgName, this._client.appName, "assets", self.get("uuid"), 'data'].join('/'); //self._client.buildAssetURL(self.get("uuid"));
 
 	var xhr = new XMLHttpRequest();
 	xhr.open("POST", endpoint, true);
 	xhr.onerror = function(err) {
 		//callback(true, err);
-		doCallback(callback, [true, new UsergridError('The File APIs are not fully supported by your browser.')], self);
+		doCallback(callback, [new UsergridError('The File APIs are not fully supported by your browser.')], xhr, self);
 	};
 	xhr.onload = function(ev) {
-		if (xhr.status >= 300) {
-			doCallback(callback, [new UsergridError(JSON.parse(xhr.responseText)), null, self], self);
+		if(xhr.status >= 500 && attempts>0){
+			self.set('attempts', --attempts);
+			setTimeout(function(){self.upload.apply(self, args);}, 100);
+		}else if (xhr.status >= 300) {
+			self.set('attempts')
+			doCallback(callback, [new UsergridError(JSON.parse(xhr.responseText)), xhr, self], self);
 		} else {
+			self.set('attempts')
 			doCallback(callback, [null, xhr, self], self);
 		}
 	};
@@ -128,9 +140,7 @@ Usergrid.Asset.prototype.upload = function(data, callback) {
 	fr.onload = function() {
 		var binary = fr.result;
 		xhr.overrideMimeType('application/octet-stream');
-		setTimeout(function() {
-			xhr.sendAsBinary(binary);
-		}, 1000);
+		xhr.sendAsBinary(binary);
 	};
 	fr.readAsBinaryString(data);
 };
@@ -146,17 +156,17 @@ Usergrid.Asset.prototype.download = function(callback) {
 	var self = this;
 	var endpoint = [this._client.URI, this._client.orgName, this._client.appName, "assets", self.get("uuid"), 'data'].join('/');
 	var xhr = new XMLHttpRequest();
+
 	xhr.open("GET", endpoint, true);
 	xhr.responseType = "blob";
 	xhr.onload = function(ev) {
 		var blob = xhr.response;
-		//callback(null, blob);
-		doCallback(callback, [null, blob, self], self);
+		doCallback(callback, [null, xhr, self], self);
 	};
 	xhr.onerror = function(err) {
 		callback(true, err);
-		doCallback(callback, [new UsergridError(err), err, self], self);
+		doCallback(callback, [new UsergridError(err), xhr, self], self);
 	};
-
+	xhr.overrideMimeType(self.get('content-type'));
 	xhr.send();
 };