From 8bad35127e60409163e28ef33e2ac578f25b1c48 Mon Sep 17 00:00:00 2001 From: Ron Hess Date: Tue, 6 Mar 2012 18:39:48 -0800 Subject: [PATCH 1/3] first implementation of getChatterFileData --- forcetk.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/forcetk.js b/forcetk.js index f754db9..ae7c5ac 100644 --- a/forcetk.js +++ b/forcetk.js @@ -181,6 +181,56 @@ if (forcetk.Client === undefined) { }); } + + /** + * Utility wrappers for calling Chatter Files API + * @author Ron Hess + * + */ + forcetk.Client.prototype.groups = function(callback, error) { + this.ajax('/' + this.apiVersion + '/chatter/groups/', callback, error); + } + + forcetk.Client.prototype.myfiles = function(callback, error) { + this.ajax('/' + this.apiVersion + '/chatter/users/'+'005A0000000Y9F4'+ + '/files', callback, error); + } + + /** + * getChatterFileData -- pull the data from the API and construct a base64 blob useful in + * data URI ( http://en.wikipedia.org/wiki/Data_URI_scheme ) specifications to tags + * + * @param fileId is the chatter file ID + * + * @renditionTypes is one of + * PDF, FLASH, THUMB720BY480, THUMB240BY180, THUMB120BY90, + * Default: THUMB120BY90 + */ + forcetk.Client.prototype.getChatterFileData = function(fileId,renditionType,callback,error,retry) { + var that = this; + var url = '/services/data' + '/' + this.apiVersion + + '/chatter/files/'+fileId+'/rendition'; + + if ( renditionType != null ) { + url += '?type=' + renditionType; + } + + this.getChatterFile( url, null, function(response) { + // fetch the file from the response of type arraybuffer + var uInt8Array = new Uint8Array(response); + var i = uInt8Array.length; + var binaryString = new Array(i); + while (i--) { + binaryString[i] = String.fromCharCode(uInt8Array[i]); + } + var data = binaryString.join(''); + + // callback to the user with the binary data + callback( window.btoa(data) ); + }, + error, retry); + } + /** * Utility function to query the Chatter API and download a file * Note, raw XMLHttpRequest because JQuery mangles the arraybuffer From b80604776266f7441590d5f74a8d6a540519d00d Mon Sep 17 00:00:00 2001 From: Ron Hess Date: Tue, 6 Mar 2012 18:50:01 -0800 Subject: [PATCH 2/3] pass in a user id to myfiles --- forcetk.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forcetk.js b/forcetk.js index ae7c5ac..fd4e6b1 100644 --- a/forcetk.js +++ b/forcetk.js @@ -191,8 +191,8 @@ if (forcetk.Client === undefined) { this.ajax('/' + this.apiVersion + '/chatter/groups/', callback, error); } - forcetk.Client.prototype.myfiles = function(callback, error) { - this.ajax('/' + this.apiVersion + '/chatter/users/'+'005A0000000Y9F4'+ + forcetk.Client.prototype.myfiles = function(userid, callback, error) { + this.ajax('/' + this.apiVersion + '/chatter/users/'+userid+ '/files', callback, error); } From 40f8c6642de8ad6c6d0a533c3b1dbfe60c4ecb99 Mon Sep 17 00:00:00 2001 From: Ron Hess Date: Wed, 7 Mar 2012 08:39:17 -0800 Subject: [PATCH 3/3] don't cache chatterFile requests thru the proxy --- forcetk.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/forcetk.js b/forcetk.js index fd4e6b1..25e57b5 100644 --- a/forcetk.js +++ b/forcetk.js @@ -248,8 +248,9 @@ if (forcetk.Client === undefined) { var url = this.instanceUrl + path; var request = new XMLHttpRequest(); - - request.open("GET", (this.proxyUrl !== null) ? this.proxyUrl: url, true); + + var ts = $.now(); // cache false + request.open("GET", (this.proxyUrl !== null) ? this.proxyUrl + '?_='+ts: url, true); request.responseType = "arraybuffer"; request.setRequestHeader(that.authzHeader, "OAuth " + that.sessionId);