Comparing version 0.6.4 to 0.6.5
1199
lib/dbox.js
var request = require("request") | ||
var qs = require("querystring") | ||
var path = require("path") | ||
var qs = require("querystring") | ||
var path = require("path") | ||
exports.app = function(config){ | ||
var root = config.root || "sandbox" | ||
var helpers = require("./helpers")(config) | ||
return { | ||
root: root, | ||
exports.app = function(config) { | ||
var root = config.root || "sandbox" | ||
var helpers = require("./helpers")(config) | ||
requesttoken: function(cb){ | ||
var signature = helpers.sign({}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": "https://api.dropbox.com/1/oauth/request_token", | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
var obj = qs.parse(b) | ||
obj.authorize_url = "https://www.dropbox.com/1/oauth/authorize?oauth_token=" + obj.oauth_token | ||
cb(e ? null : r.statusCode, obj) | ||
}) | ||
}, | ||
return { | ||
root: root, | ||
accesstoken: function(options, cb){ | ||
var signature = helpers.sign(options) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": "https://api.dropbox.com/1/oauth/access_token", | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, qs.parse(b)) | ||
}) | ||
}, | ||
requesttoken: function(cb) { | ||
var signature = helpers.sign({}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": "https://api.dropbox.com/1/oauth/request_token", | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
var obj = qs.parse(b) | ||
obj.authorize_url = "https://www.dropbox.com/1/oauth/authorize?oauth_token=" + obj.oauth_token | ||
cb(e ? null : r.statusCode, obj) | ||
}) | ||
}, | ||
// creates client object | ||
client: function(options){ | ||
var options = options | ||
accesstoken: function(options, cb) { | ||
var signature = helpers.sign(options) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": "https://api.dropbox.com/1/oauth/access_token", | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, qs.parse(b)) | ||
}) | ||
}, | ||
return { | ||
account: function(cb){ | ||
var signature = helpers.sign(options) | ||
var args = { | ||
"method": "POST", | ||
"headers": { "content-type": "application/x-www-form-urlencoded" }, | ||
"url": "https://api.dropbox.com/1/account/info", | ||
"body": qs.stringify(signature) | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
delta: function(args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = {} | ||
} | ||
if (config.scope) { | ||
args.path_prefix = path.join("/", config.scope); | ||
} | ||
// creates client object | ||
client: function(options) { | ||
var options = options | ||
var entries = [] | ||
var REQUEST_CONCURRENCY_DELAY = 20 | ||
var reset; | ||
var fetch = function(args){ | ||
var signature = helpers.sign(options, args) | ||
var body = qs.stringify(signature) | ||
var opts = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": "https://api.dropbox.com/1/delta", | ||
"body": body | ||
} | ||
return request(opts, function(e, r, b){ | ||
var status = e ? null : r.statusCode | ||
var output = helpers.parseJSON(b) | ||
if(typeof reset == 'undefined'){ | ||
reset = output.reset | ||
} | ||
return { | ||
account: function(cb) { | ||
var signature = helpers.sign(options) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded" | ||
}, | ||
"url": "https://api.dropbox.com/1/account/info", | ||
"body": qs.stringify(signature) | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
if(output && output.hasOwnProperty("entries")){ | ||
output["entries"].forEach(function(entry){ | ||
entries.push(entry) | ||
}) | ||
} | ||
if(output && output.hasOwnProperty("has_more") && output.has_more == true){ | ||
args["cursor"] = output.cursor | ||
fetch(args) | ||
}else{ | ||
if(output){ | ||
output["entries"] = entries | ||
output["reset"] = reset | ||
} | ||
// console.log("MADE IT:", status, output) | ||
cb(status, output) | ||
} | ||
}) | ||
} | ||
fetch(args) | ||
delta: function(args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = {} | ||
} | ||
}, | ||
if (config.scope) { | ||
args.path_prefix = path.join("/", config.scope); | ||
} | ||
get: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api-content.dropbox.com", | ||
action: "files", | ||
path: path, | ||
query: signature | ||
}) | ||
var entries = [] | ||
var REQUEST_CONCURRENCY_DELAY = 20 | ||
var reset; | ||
var args = { | ||
"method": "GET", | ||
"url": url, | ||
"encoding": null | ||
} | ||
return request(args, function(e, r, b) { | ||
if (e) { | ||
cb(null, null, null); | ||
} else { | ||
var headers = (r.headers['x-dropbox-metadata'] !== undefined) ? helpers.parseJSON(r.headers['x-dropbox-metadata']) : {}; | ||
cb(r.statusCode, b, headers); | ||
} | ||
}) | ||
}, | ||
var fetch = function(args) { | ||
var signature = helpers.sign(options, args) | ||
var body = qs.stringify(signature) | ||
var opts = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": "https://api.dropbox.com/1/delta", | ||
"body": body | ||
} | ||
stream: function(path, args) { | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api-content.dropbox.com", | ||
action: "files", | ||
path: path, | ||
query: signature | ||
}) | ||
return request(opts, function(e, r, b) { | ||
var status = e ? null : r.statusCode | ||
var output = helpers.parseJSON(b) | ||
var args = { | ||
"method": "GET", | ||
"url": url, | ||
"encoding": null | ||
} | ||
if (typeof reset == 'undefined') { | ||
reset = output.reset | ||
} | ||
return request(args); | ||
}, | ||
if (output && output.hasOwnProperty("entries")) { | ||
output["entries"].forEach(function(entry) { | ||
entries.push(entry) | ||
}) | ||
} | ||
if (output && output.hasOwnProperty("has_more") && output.has_more == true) { | ||
args["cursor"] = output.cursor | ||
fetch(args) | ||
} else { | ||
if (output) { | ||
output["entries"] = entries | ||
output["reset"] = reset | ||
} | ||
// console.log("MADE IT:", status, output) | ||
cb(status, output) | ||
} | ||
}) | ||
} | ||
put: function(path, body, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
fetch(args) | ||
var url = helpers.url({ | ||
hostname: "api-content.dropbox.com", | ||
action: "files_put", | ||
path: path, | ||
query: signature | ||
}) | ||
var args = { | ||
"method": "PUT", | ||
"headers": { "content-length": body.length }, | ||
"url": url | ||
} | ||
// do not send empty body | ||
if(body.length > 0) args["body"] = body | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
}, | ||
metadata: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "metadata", | ||
path: path, | ||
query: signature | ||
}) | ||
var args = { | ||
"method": "GET", | ||
"url": url | ||
} | ||
return request(args, function(e, r, b){ | ||
// this is a special case, since the dropbox api returns a | ||
// 304 response with an empty body when the 'hash' option | ||
// is provided and there have been no changes since the | ||
// hash was computed | ||
if (e) { | ||
cb(null, null) | ||
} else { | ||
cb(r.statusCode, r.statusCode == 304 ? {} : helpers.parseJSON(b)) | ||
} | ||
}) | ||
}, | ||
get: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
// | ||
// Loads a dropbox folder | ||
// (recursive by default) | ||
// | ||
readdir: function (path, options, callback) { | ||
if (arguments.length < 3) { | ||
callback = options; | ||
options = options || {}; | ||
} | ||
options.recursive = (options.recursive !== false); // default true | ||
options.details = (options.details === true); // default false | ||
var signature = helpers.sign(options, args) | ||
var results = [], | ||
REQUEST_CONCURRENCY_DELAY = 20, | ||
callbacks = 0, | ||
self = this; | ||
// | ||
// Remark: REQUEST_CONCURRENCY_DELAY represents the millisecond, | ||
// delay between outgoing requests to dropbox | ||
// | ||
function load (path) { | ||
callbacks++; | ||
// | ||
// Give the dropbox API a delay between requests, | ||
// by wrapping each depth level in a setTimeout delay | ||
// | ||
setTimeout(function(){ | ||
self.metadata(path, function (status, reply) { | ||
// | ||
// If we have found any contents on this level of the folder | ||
// | ||
if (reply.contents) { | ||
reply.contents.forEach(function (item) { | ||
// | ||
// Add the item into our results array (details or path) | ||
// | ||
var itemPath = helpers.scopedPath(item.path) | ||
if(options.details){ | ||
item.path = itemPath | ||
results.push(item) | ||
}else{ | ||
results.push(itemPath) | ||
} | ||
// | ||
// If we have encountered another folder, we can recurse on it | ||
// | ||
if (item.is_dir && options.recursive) { | ||
load(itemPath); | ||
} | ||
}); | ||
} | ||
callbacks--; | ||
if (callbacks === 0) { | ||
callback(status, results); | ||
} | ||
}); | ||
}, REQUEST_CONCURRENCY_DELAY) | ||
} | ||
load(path, results); | ||
}, | ||
var url = helpers.url({ | ||
hostname: "api-content.dropbox.com", | ||
action: "files", | ||
path: path, | ||
query: signature | ||
}) | ||
revisions: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "revisions", | ||
path: path, | ||
query: signature | ||
}) | ||
var args = { | ||
"method": "GET", | ||
"url": url, | ||
"encoding": null | ||
} | ||
var args = { | ||
"method": "GET", | ||
"url": url | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
return request(args, function(e, r, b) { | ||
if (e) { | ||
cb(null, null, null); | ||
} else { | ||
var headers = (r.headers['x-dropbox-metadata'] !== undefined) ? helpers.parseJSON(r.headers['x-dropbox-metadata']) : {}; | ||
cb(r.statusCode, b, headers); | ||
} | ||
}) | ||
}, | ||
restore: function(path, rev, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
signature["rev"] = rev | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "restore", | ||
path: path | ||
}) | ||
stream: function(path, args) { | ||
var signature = helpers.sign(options, args) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
var url = helpers.url({ | ||
hostname: "api-content.dropbox.com", | ||
action: "files", | ||
path: path, | ||
query: signature | ||
}) | ||
search: function(path, query, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
signature["query"] = query | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "search", | ||
path: path | ||
}) | ||
var args = { | ||
"method": "GET", | ||
"url": url, | ||
"encoding": null | ||
} | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
return request(args); | ||
}, | ||
shares: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "shares", | ||
path: path | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
put: function(path, body, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
media: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "media", | ||
path: path | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
var signature = helpers.sign(options, args) | ||
cpref: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "copy_ref", | ||
path: path, | ||
query: signature | ||
}) | ||
var args = { | ||
"method": "GET", | ||
"url": url | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
var url = helpers.url({ | ||
hostname: "api-content.dropbox.com", | ||
action: "files_put", | ||
path: path, | ||
query: signature | ||
}) | ||
thumbnails: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api-content.dropbox.com", | ||
action: "thumbnails", | ||
path: path, | ||
query: signature | ||
}) | ||
var args = { | ||
"method": "PUT", | ||
"headers": { | ||
"content-length": body.length | ||
}, | ||
"url": url | ||
} | ||
var args = { | ||
"method": "GET", | ||
"url": url, | ||
"encoding": null | ||
} | ||
return request(args, function(e, r, b){ | ||
if (e) { | ||
cb(null, null, null) | ||
} else { | ||
var headers = (r.headers['x-dropbox-metadata'] !== undefined) ? helpers.parseJSON(r.headers['x-dropbox-metadata']) : {}; | ||
cb(r.statusCode, b, headers) | ||
} | ||
}) | ||
}, | ||
// do not send empty body | ||
if (body.length > 0) args["body"] = body | ||
cp: function(from_path, to_path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
var signature = helpers.sign(options, args) | ||
// check for copy ref | ||
if(from_path.hasOwnProperty("copy_ref")){ | ||
signature['from_copy_ref'] = from_path["copy_ref"] | ||
}else{ | ||
signature['from_path'] = helpers.filePath(from_path) | ||
} | ||
signature["root"] = root // API quirk that this is reqired for this call | ||
signature["to_path"] = helpers.filePath(to_path) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "fileops/copy" | ||
}) | ||
var body = qs.stringify(signature) | ||
metadata: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
var signature = helpers.sign(options, args) | ||
mv: function(from_path, to_path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "metadata", | ||
path: path, | ||
query: signature | ||
}) | ||
var signature = helpers.sign(options, args) | ||
signature["root"] = root // API quirk that this is reqired for this call | ||
signature["from_path"] = helpers.filePath(from_path) | ||
signature["to_path"] = helpers.filePath(to_path) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "fileops/move" | ||
}) | ||
var args = { | ||
"method": "GET", | ||
"url": url | ||
} | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
// this is a special case, since the dropbox api returns a | ||
// 304 response with an empty body when the 'hash' option | ||
// is provided and there have been no changes since the | ||
// hash was computed | ||
if (e) { | ||
cb(null, null) | ||
} else { | ||
cb(r.statusCode, r.statusCode == 304 ? {} : helpers.parseJSON(b)) | ||
} | ||
}) | ||
}, | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
// | ||
// Loads a dropbox folder | ||
// (recursive by default) | ||
// | ||
readdir: function(path, options, callback) { | ||
if (arguments.length < 3) { | ||
callback = options; | ||
options = options || {}; | ||
} | ||
options.recursive = (options.recursive !== false); // default true | ||
options.details = (options.details === true); // default false | ||
rm: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var results = [], | ||
REQUEST_CONCURRENCY_DELAY = 20, | ||
callbacks = 0, | ||
self = this; | ||
// | ||
// Remark: REQUEST_CONCURRENCY_DELAY represents the millisecond, | ||
// delay between outgoing requests to dropbox | ||
// | ||
function load(path) { | ||
callbacks++; | ||
// | ||
// Give the dropbox API a delay between requests, | ||
// by wrapping each depth level in a setTimeout delay | ||
// | ||
setTimeout(function() { | ||
self.metadata(path, function(status, reply) { | ||
// | ||
// If we have found any contents on this level of the folder | ||
// | ||
if (reply.contents) { | ||
reply.contents.forEach(function(item) { | ||
// | ||
// Add the item into our results array (details or path) | ||
// | ||
var itemPath = helpers.scopedPath(item.path) | ||
if (options.details) { | ||
item.path = itemPath | ||
results.push(item) | ||
} else { | ||
results.push(itemPath) | ||
} | ||
// | ||
// If we have encountered another folder, we can recurse on it | ||
// | ||
if (item.is_dir && options.recursive) { | ||
load(itemPath); | ||
} | ||
}); | ||
} | ||
callbacks--; | ||
if (callbacks === 0) { | ||
callback(status, results); | ||
} | ||
}); | ||
}, REQUEST_CONCURRENCY_DELAY) | ||
} | ||
load(path, results); | ||
}, | ||
var signature = helpers.sign(options, args) | ||
signature["root"] = root | ||
signature["path"] = helpers.filePath(path) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "fileops/delete" | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
revisions: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
mkdir: function(path, args, cb){ | ||
if(!cb){ | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var signature = helpers.sign(options, args) | ||
signature["root"] = root | ||
signature["path"] = helpers.filePath(path) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "fileops/create_folder" | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b){ | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "revisions", | ||
path: path, | ||
query: signature | ||
}) | ||
} | ||
var args = { | ||
"method": "GET", | ||
"url": url | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
restore: function(path, rev, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
signature["rev"] = rev | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "restore", | ||
path: path | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
search: function(path, query, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
signature["query"] = query | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "search", | ||
path: path | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
shares: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "shares", | ||
path: path | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
media: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "media", | ||
path: path | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
cpref: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "copy_ref", | ||
path: path, | ||
query: signature | ||
}) | ||
var args = { | ||
"method": "GET", | ||
"url": url | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
thumbnails: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
var url = helpers.url({ | ||
hostname: "api-content.dropbox.com", | ||
action: "thumbnails", | ||
path: path, | ||
query: signature | ||
}) | ||
var args = { | ||
"method": "GET", | ||
"url": url, | ||
"encoding": null | ||
} | ||
return request(args, function(e, r, b) { | ||
if (e) { | ||
cb(null, null, null) | ||
} else { | ||
var headers = (r.headers['x-dropbox-metadata'] !== undefined) ? helpers.parseJSON(r.headers['x-dropbox-metadata']) : {}; | ||
cb(r.statusCode, b, headers) | ||
} | ||
}) | ||
}, | ||
cp: function(from_path, to_path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
// check for copy ref | ||
if (from_path.hasOwnProperty("copy_ref")) { | ||
signature['from_copy_ref'] = from_path["copy_ref"] | ||
} else { | ||
signature['from_path'] = helpers.filePath(from_path) | ||
} | ||
signature["root"] = root // API quirk that this is reqired for this call | ||
signature["to_path"] = helpers.filePath(to_path) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "fileops/copy" | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
mv: function(from_path, to_path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
signature["root"] = root // API quirk that this is reqired for this call | ||
signature["from_path"] = helpers.filePath(from_path) | ||
signature["to_path"] = helpers.filePath(to_path) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "fileops/move" | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
rm: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
signature["root"] = root | ||
signature["path"] = helpers.filePath(path) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "fileops/delete" | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
}, | ||
mkdir: function(path, args, cb) { | ||
if (!cb) { | ||
cb = args | ||
args = null | ||
} | ||
var signature = helpers.sign(options, args) | ||
signature["root"] = root | ||
signature["path"] = helpers.filePath(path) | ||
var url = helpers.url({ | ||
hostname: "api.dropbox.com", | ||
action: "fileops/create_folder" | ||
}) | ||
var body = qs.stringify(signature) | ||
var args = { | ||
"method": "POST", | ||
"headers": { | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": body.length | ||
}, | ||
"url": url, | ||
"body": body | ||
} | ||
return request(args, function(e, r, b) { | ||
cb(e ? null : r.statusCode, e ? null : helpers.parseJSON(b)) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
var oauth = require("./oauth") | ||
var path = require("path") | ||
var qs = require("querystring") | ||
var path = require("path") | ||
var qs = require("querystring") | ||
module.exports = function(config){ | ||
var root = config.root || "sandbox" | ||
var signer = oauth(config.app_key, config.app_secret) | ||
var scope = config.scope = config.scope || "" | ||
return { | ||
sign: function(token, args){ | ||
var signature = signer(token) | ||
if(args != null) | ||
for(var attr in args) | ||
if (args.hasOwnProperty(attr)) signature[attr] = args[attr] | ||
module.exports = function(config) { | ||
var root = config.root || "sandbox" | ||
var signer = oauth(config.app_key, config.app_secret) | ||
var scope = config.scope = config.scope || "" | ||
return signature | ||
}, | ||
parseJSON: function(str) { | ||
var scopePath = path.join("/", scope) | ||
var rx = RegExp("^" + scopePath, "i") | ||
try { | ||
var obj = JSON.parse(str) | ||
if(scope){ | ||
// replace path on main object | ||
if(obj.hasOwnProperty("path")) | ||
obj.path = obj.path.replace(rx, "") | ||
return { | ||
sign: function(token, args) { | ||
var signature = signer(token) | ||
// replace paths in array | ||
if(obj.length){ | ||
for (var i = 0; i < obj.length; i++) { | ||
if(obj[i].hasOwnProperty("path")){ | ||
obj[i]["path"] = obj[i]["path"].replace(rx, "") | ||
} | ||
} | ||
} | ||
// replace entries paths from delta call | ||
if(obj.hasOwnProperty("entries") && obj["entries"].length){ | ||
var resultSet = [] | ||
for (var i = 0; i < obj["entries"].length; i++) { | ||
// readdir | ||
if(obj["entries"][i][0].match(rx) && obj["entries"][i][0] != scopePath){ | ||
var fpath = obj["entries"][i][0].replace(rx, "") | ||
var entry = obj["entries"][i][1] | ||
if(entry && entry.hasOwnProperty("path")){ | ||
entry.path = entry.path.replace(rx, "") | ||
} | ||
// console.log("pushing", fpath, entry) | ||
resultSet.push([ fpath, entry ]) | ||
} | ||
} | ||
// console.log("resultSet..", resultSet) | ||
obj['entries'] = resultSet | ||
} | ||
} | ||
} catch (e) { | ||
console.log("INVALID", e) | ||
var obj = {} | ||
} | ||
return obj | ||
}, | ||
scopedPath: function(fpath){ | ||
if(scope){ | ||
var rx = RegExp("^" + path.join("/", scope)) | ||
return fpath.replace(rx, "") | ||
}else{ | ||
return fpath | ||
} | ||
}, | ||
filePath: function(fpath){ | ||
return path.join(config.scope, fpath) | ||
}, | ||
/** | ||
* Builds url based on args | ||
* | ||
* var url = helpers.url({ | ||
* base: "https://api-content.dropbox.com", | ||
* action: "files", | ||
* path: "myfile.txt", | ||
* query: signature | ||
* }) | ||
* | ||
* | ||
* https://api-content.dropbox.com/1/files/sandbox/myfile.txt | ||
* ?oauth_token=o3v22uxb76xim22 | ||
* &oauth_consumer_key=umdezbv48ck01fx | ||
* &oauth_signature=tjmajxw7sci88o6%26xxdg7s05i8yk21b | ||
* &oauth_timestamp=1361772981 | ||
* &oauth_nonce=136177298119121587 | ||
* &oauth_signature_method=PLAINTEXT | ||
* &oauth_version=1.0 | ||
* | ||
*/ | ||
url: function(obj){ | ||
if(!obj.hostname || !obj.action) | ||
throw "must have proper base, version, and action" | ||
if (args != null) | ||
for (var attr in args) | ||
if (args.hasOwnProperty(attr)) signature[attr] = args[attr] | ||
// calculate if fileops path | ||
var fileop = obj.action.split("/")[0] == "fileops" | ||
// fileops calls desn't want root in path | ||
var rootpath = fileop ? "" : root | ||
// fileops calls desn't want scope in path | ||
var scopepath = fileop ? "" : scope | ||
return signature | ||
}, | ||
// we wont always have this | ||
var filepath = obj.path ? qs.escape(obj.path) : "" | ||
// build full path | ||
var fullpath = path.join(obj.hostname) | ||
fullpath = path.join(fullpath, obj.version || "1") | ||
fullpath = path.join(fullpath, obj.action) | ||
fullpath = path.join(fullpath, rootpath) | ||
fullpath = path.join(fullpath, scopepath) | ||
fullpath = path.join(fullpath, filepath) | ||
// add protocol | ||
var fullurl = "https://" + fullpath | ||
// add querystring if we have one | ||
if(obj.hasOwnProperty("query")) fullurl += ("?" + qs.stringify(obj.query)) | ||
return fullurl | ||
} | ||
} | ||
parseJSON: function(str) { | ||
var scopePath = "/" + scope | ||
var rx = RegExp("^" + scopePath, "i") | ||
try { | ||
var obj = JSON.parse(str) | ||
if (scope) { | ||
// replace path on main object | ||
if (obj.hasOwnProperty("path")) | ||
obj.path = obj.path.replace(rx, "") | ||
// replace paths in array | ||
if (obj.length) { | ||
for (var i = 0; i < obj.length; i++) { | ||
if (obj[i].hasOwnProperty("path")) { | ||
obj[i]["path"] = obj[i]["path"].replace(rx, "") | ||
} | ||
} | ||
} | ||
// replace entries paths from delta call | ||
if (obj.hasOwnProperty("entries") && obj["entries"].length) { | ||
var resultSet = [] | ||
for (var i = 0; i < obj["entries"].length; i++) { | ||
// readdir | ||
if (obj["entries"][i][0].match(rx) && obj["entries"][i][0] != scopePath) { | ||
var fpath = obj["entries"][i][0].replace(rx, "") | ||
var entry = obj["entries"][i][1] | ||
if (entry && entry.hasOwnProperty("path")) { | ||
entry.path = entry.path.replace(rx, "") | ||
} | ||
// console.log("pushing", fpath, entry) | ||
resultSet.push([fpath, entry]) | ||
} | ||
} | ||
// console.log("resultSet..", resultSet) | ||
obj['entries'] = resultSet | ||
} | ||
} | ||
} catch (e) { | ||
console.log("INVALID", e) | ||
var obj = {} | ||
} | ||
return obj | ||
}, | ||
scopedPath: function(fpath) { | ||
if (scope) { | ||
var rx = RegExp("^" + "/" + scope) | ||
return fpath.replace(rx, "") | ||
} else { | ||
return fpath | ||
} | ||
}, | ||
filePath: function(fpath) { | ||
return path.join(config.scope, fpath) | ||
}, | ||
/** | ||
* Builds url based on args | ||
* | ||
* var url = helpers.url({ | ||
* base: "https://api-content.dropbox.com", | ||
* action: "files", | ||
* path: "myfile.txt", | ||
* query: signature | ||
* }) | ||
* | ||
* | ||
* https://api-content.dropbox.com/1/files/sandbox/myfile.txt | ||
* ?oauth_token=o3v22uxb76xim22 | ||
* &oauth_consumer_key=umdezbv48ck01fx | ||
* &oauth_signature=tjmajxw7sci88o6%26xxdg7s05i8yk21b | ||
* &oauth_timestamp=1361772981 | ||
* &oauth_nonce=136177298119121587 | ||
* &oauth_signature_method=PLAINTEXT | ||
* &oauth_version=1.0 | ||
* | ||
*/ | ||
url: function(obj) { | ||
if (!obj.hostname || !obj.action) | ||
throw "must have proper base, version, and action" | ||
// calculate if fileops path | ||
var fileop = obj.action.split("/")[0] == "fileops" | ||
// fileops calls desn't want root in path | ||
var rootpath = fileop ? "" : root | ||
// fileops calls desn't want scope in path | ||
var scopepath = fileop ? "" : scope | ||
// we wont always have this | ||
var filepath = obj.path ? qs.escape(obj.path) : "" | ||
// build full path | ||
var fullpath = path.join(obj.hostname) | ||
fullpath = path.join(fullpath, obj.version || "1") | ||
fullpath = path.join(fullpath, obj.action) | ||
fullpath = path.join(fullpath, rootpath) | ||
fullpath = path.join(fullpath, scopepath) | ||
fullpath = path.join(fullpath, filepath) | ||
// add protocol | ||
var fullurl = "https://" + fullpath | ||
// add querystring if we have one | ||
if (obj.hasOwnProperty("query")) fullurl += ("?" + qs.stringify(obj.query)) | ||
return fullurl | ||
} | ||
} | ||
} |
@@ -1,45 +0,45 @@ | ||
module.exports = function (consumerKey, consumerSecret){ | ||
module.exports = function(consumerKey, consumerSecret) { | ||
var encode = function(data){ | ||
return encodeURIComponent(data || ""). | ||
replace(/\!/g, "%21"). | ||
replace(/\'/g, "%27"). | ||
replace(/\(/g, "%28"). | ||
replace(/\)/g, "%29"). | ||
replace(/\*/g, "%2A") | ||
} | ||
var encode = function(data) { | ||
return encodeURIComponent(data || ""). | ||
replace(/\!/g, "%21"). | ||
replace(/\'/g, "%27"). | ||
replace(/\(/g, "%28"). | ||
replace(/\)/g, "%29"). | ||
replace(/\*/g, "%2A") | ||
} | ||
var getSignature = function(tokenSecret){ | ||
return encode(consumerSecret) + "&" + encode(tokenSecret) | ||
} | ||
var getTimestamp = function(){ | ||
return (Math.floor((new Date()).getTime() / 1000)).toString() | ||
} | ||
var getNonce = function(timestamp){ | ||
return timestamp + Math.floor( Math.random() * 100000000) | ||
} | ||
return function (options){ | ||
var options = JSON.parse(JSON.stringify(options)) | ||
var secret = options["oauth_token_secret"] | ||
var signature = getSignature(secret) | ||
var timestamp = getTimestamp() | ||
var nonce = getNonce(timestamp) | ||
var getSignature = function(tokenSecret) { | ||
return encode(consumerSecret) + "&" + encode(tokenSecret) | ||
} | ||
options["oauth_consumer_key"] = consumerKey, | ||
options["oauth_signature"] = signature, | ||
options["oauth_timestamp"] = timestamp, | ||
options["oauth_nonce"] = nonce, | ||
options["oauth_signature_method"] = "PLAINTEXT", | ||
options["oauth_version"] = "1.0" | ||
var getTimestamp = function() { | ||
return (Math.floor((new Date()).getTime() / 1000)).toString() | ||
} | ||
delete options["authorize_url"] | ||
delete options["oauth_token_secret"] | ||
delete options["uid"] | ||
var getNonce = function(timestamp) { | ||
return timestamp + Math.floor(Math.random() * 100000000) | ||
} | ||
return options | ||
} | ||
} | ||
return function(options) { | ||
var options = JSON.parse(JSON.stringify(options)) | ||
var secret = options["oauth_token_secret"] | ||
var signature = getSignature(secret) | ||
var timestamp = getTimestamp() | ||
var nonce = getNonce(timestamp) | ||
options["oauth_consumer_key"] = consumerKey, | ||
options["oauth_signature"] = signature, | ||
options["oauth_timestamp"] = timestamp, | ||
options["oauth_nonce"] = nonce, | ||
options["oauth_signature_method"] = "PLAINTEXT", | ||
options["oauth_version"] = "1.0" | ||
delete options["authorize_url"] | ||
delete options["oauth_token_secret"] | ||
delete options["uid"] | ||
return options | ||
} | ||
} |
{ | ||
"name": "node-dbox", | ||
"description": "NodeJS SDK for the Dropbox API", | ||
"version": "0.6.4", | ||
"version": "0.6.5", | ||
"author": "Brock Whitten <brock@sintaxi.com>", | ||
@@ -94,2 +94,2 @@ "contributors": [{ | ||
}] | ||
} | ||
} |
@@ -11,3 +11,3 @@ # node-dbox | ||
We have full documentation under [our wiki page.] (https://github.com/WebArtWork/node-dbox/wiki/How-to-use) | ||
Have a look on how to use at [our wiki page.] (https://github.com/WebArtWork/node-dbox/wiki/How-to-use) | ||
@@ -20,5 +20,5 @@ ## Posibility | ||
Have a look on full documented api at [our wiki page.] (https://github.com/WebArtWork/node-dbox/wiki/Home-page). | ||
We have full documentation under [our wiki page.] (https://github.com/WebArtWork/node-dbox/wiki/Home-page). | ||
# Our Todo List | ||
## Our Todo List | ||
@@ -28,3 +28,3 @@ 1) fix all tests<br> | ||
# Known Bugs | ||
## Known Bugs | ||
@@ -31,0 +31,0 @@ 1) Not working on windows due to paths |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
885
32298