Created
August 8, 2011 06:50
-
-
Save dusual/1131324 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function buildmessage(params){ | |
var boundary = 'sAxIqse3tPlHqUIUI9ofVlHvtdt3tpaG' | |
// | |
// var body = [ | |
// "Content-Type: multipart/form-data;boundary="+boundary , | |
// "Content-Length:" +params.content.length, | |
// "--"+boundary, | |
// "Content-Disposition: form-data, file=" + params.file, | |
// "--"+boundary, | |
// "Content-Type: application/octet-stream", | |
// params.content | |
// | |
// ].join('\r\n'); | |
// | |
body = '\ | |
--6ac6f72b1bcb4fe49a1162138b4c83a5 \ | |
Content-Disposition: form-data; name="file"; filename="test.txt" \ | |
Content-Type: text/plain \ | |
test file \ | |
\ | |
--6ac6f72b1bcb4fe49a1162138b4c83a5-- \ | |
' | |
return body | |
//return body | |
} | |
var _ajaxSendFileContents = function(message, file, content ) { | |
var _xhr = new XMLHttpRequest(); | |
// var params = {}; | |
// | |
// //for (i in message.parameters) { | |
// // params[message.parameters[i][0]] = message.parameters[i][1]; | |
// //} | |
// | |
// | |
// params.file = file | |
//// params.content = content ; | |
//// params.extra=''; | |
//// params = $.extend(params,message.parameters) | |
// | |
// | |
// | |
// | |
// | |
_xhr.onreadystatechange = function() { | |
//console.log(this); | |
if(_xhr.status == 200 && _xhr.readyState == 4){ | |
} | |
} | |
// var parameterString= '' ; | |
_xhr.open("POST", message.action, true); | |
// count = 0 | |
// for ( i in params) { | |
// if (i.indexOf('oauth') != -1 ){ | |
// parameterString += (count > 0 ? ",":"") | |
// | |
// i + "=" | |
// | |
// encodeURI(params[i]); | |
// | |
// } count +=1 ; | |
// } | |
// | |
// // parameterString = "OAuth realm='''" + parameterString | |
// | |
_xhr.setRequestHeader('Content-Type','multipart/form-data; boundary=6ac6f72b1bcb4fe49a1162138b4c83a5') | |
// _xhr.setRequestHeader('Authorization','OAuth file="test.txt",oauth_consumer_key="g82gi74es4x7s6m",oauth_nonce="2992320",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1298007098",oauth_token="fkrwme74smqyltg",oauth_version="1.0"') | |
// _xhr.setRequestHeader("Connection", "close"); | |
// //_xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); | |
// // _xhr.setRequestHeader("Authorization", parameterString); | |
// | |
// | |
// // console.log(parameterString); | |
// | |
// // set the necessary request headers | |
// | |
//// parameterString[parameterString - 1] = '' | |
//// message.action+='?'+parameterString; | |
//// message.action=message.action; | |
//// console.log(message.action) ; | |
// | |
params={} | |
body = buildmessage(params) | |
console.log('testbefore') | |
console.log(body) | |
_xhr.sendAsBinary(body); | |
console.log('testafter'); | |
}; | |
function escapePath(p) { | |
return encodeURIComponent(p) | |
.replace(/%2F/g, '/') | |
.replace(/\)/g, '%29') | |
.replace(/\(/g, '%28'); | |
} | |
var dropbox = { | |
setup: function() { | |
this._consumerKey = '53rzw7i7ip4bsgl'; | |
this._consumerSecret = 'wwsshz3w95utgtg'; | |
this._requestCounter = $.now(); | |
}, | |
authenticate: function(email, password, callback) { | |
var that = this; | |
this._request("/token", { | |
sendAuth: false, | |
success: function(data) { | |
console.log("authentication response", data); | |
that._accessToken = data.token; | |
that._accessTokenSecret = data.secret; | |
if (callback) { | |
callback(); | |
} | |
}, | |
error: function() { | |
console.error("authentication error", arguments); | |
} | |
}, { | |
email: email, | |
password: password | |
}); | |
}, | |
getInfo: function(callback) { | |
this._request("/account/info", { | |
success: function(data) { | |
console.log("account info", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("account info error", arguments); | |
} | |
}); | |
}, | |
getFiles: function(path,callback) { | |
this._request("/files/dropbox/"+path, { | |
success: function(data) { | |
console.log("account info", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("account info error", arguments); | |
} | |
}); | |
}, | |
putFiles: function(file,callback) { | |
//$.ajaxSetup({ | |
// data: {file:file} | |
// }); | |
this._request("/files/dropbox", {method:"POST",file:file.fileName,extra:file,subdomain:'api-content'}, { | |
success: function() { | |
console.log("post info" ); | |
if (callback) { | |
callback(); | |
} | |
}, | |
error: function() { | |
alert("failure"); | |
} | |
}); | |
}, | |
getMetadata: function(path,callback) { | |
this._request("/metadata/dropbox/"+path,{ | |
file_limit:10000, | |
list:true, | |
success: function(data) { | |
console.log("account info", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("Meta data error", arguments); | |
} | |
}); | |
}, | |
_request: function(path, params, data) { | |
var requestId = "dropboxjsonp" + (this._requestCounter++); | |
params = $.extend({}, { | |
subdomain: "api", // some methods need api-content.dropbox.com | |
apiVersion: "0", | |
sendAuth: true, | |
method: "GET", | |
success: $.noop, | |
error: $.noop | |
}, params || {}); | |
if (params.sendAuth && !this._accessToken) { | |
throw "Authenticated method called before authenticating"; | |
} | |
var url = "https://" + params.subdomain + ".dropbox.com/" + params.apiVersion + escapePath(path); | |
var message = { | |
action: url, | |
method: params.method, | |
parameters: { | |
oauth_consumer_key: this._consumerKey, | |
oauth_signature_method: "HMAC-SHA1", | |
callback: requestId | |
} | |
}; | |
$.extend(message.parameters, data); | |
if (params.sendAuth) { | |
message.parameters.oauth_token = this._accessToken; | |
} | |
var oauthBits = { | |
consumerSecret: this._consumerSecret | |
}; | |
if (params.sendAuth) { | |
oauthBits.tokenSecret = this._accessTokenSecret; | |
} | |
OAuth.setTimestampAndNonce(message); | |
OAuth.SignatureMethod.sign(message, oauthBits); | |
if (params.method == "POST"){ | |
test = true ; | |
_ajaxSendFileContents(message, params.extra.file); | |
} | |
/* | |
escaping path after oauth signature has been made | |
*/ | |
var url = "https://" + params.subdomain + ".dropbox.com/" + params.apiVersion + escapePath(path); | |
if (typeof test=="undefined"){ | |
$.ajax({ | |
dataType: "jsonp", | |
method: params.method, | |
url: url, | |
data: OAuth.getParameterMap(message.parameters), | |
jsonpCallback: requestId, | |
success: params.success , | |
error: params.error | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment