Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add put; Quote filename; Remove encodePath

  • Loading branch information...
commit 6fa066dbab0ffc99922203323ca16a108bd2f310 1 parent 1096f53
@CrabDude CrabDude authored
Showing with 32 additions and 9 deletions.
  1. +32 −9 lib/dropbox-node.js
View
41 lib/dropbox-node.js
@@ -1,4 +1,5 @@
-var querystring = require('querystring')
+var pathLib = require('path')
+ , querystring = require('querystring')
, escapePath = require('./util/escape-path')
, stringifyParams = require('./util/stringify-params')
, OAuth = require('oauth').OAuth
@@ -116,13 +117,13 @@ DropboxClient.prototype.putFile = function(file, path, optargs, cb) {
// Build request body.
path = escapePath(path);
var body = ['--' + boundary
- , 'Content-Disposition: form-data; name=file; filename=' + file
+ , 'Content-Disposition: form-data; name=file; filename="' + file + '"'
, 'Content-Type: application/octet-stream'
, '', data.toString('binary'), '--' + boundary + '--', ''
].join('\r\n');
self.oauth.post(CONTENT_API_URI + '/files/dropbox/' + path +
- '?file=' + escapePath(file)
+ '?file=' + encodeURIComponent(file)
, optargs.token || self.access_token
, optargs.secret || self.access_token_secret
, body, content_type
@@ -134,6 +135,34 @@ DropboxClient.prototype.putFile = function(file, path, optargs, cb) {
}
+// Uploads contents to the specified path
+DropboxClient.prototype.put = function(content, path, optargs, cb) {
+ if (typeof optargs == 'function') cb = optargs, optargs = {};
+ var boundary = 'sAxIqse3tPlHqUIUI9ofVlHvtdt3tpaG'
+ , content_type = 'multipart/form-data; boundary=' + boundary
+ , self = this;
+
+ // Build request body.
+ var file = pathLib.basename(path);
+ path = escapePath(pathLib.dirname(path));
+ var body = ['--' + boundary
+ , 'Content-Disposition: form-data; name=file; filename="' + file + '"'
+ , 'Content-Type: application/octet-stream'
+ , '', content, '--' + boundary + '--', ''
+ ].join('\r\n');
+
+ self.oauth.post(CONTENT_API_URI + '/files/dropbox/' + path +
+ '?file=' + encodeURIComponent(file)
+ , optargs.token || self.access_token
+ , optargs.secret || self.access_token_secret
+ , body, content_type
+ , function(err, data, res) {
+ if (err) return cb(err);
+ cb(null, JSON.parse(data));
+ });
+}
+
+
// Gets metadata of file/folder specified by path relative to user's
// Dropbox root.
DropboxClient.prototype.getMetadata = function(path, optargs, cb) {
@@ -171,8 +200,6 @@ DropboxClient.prototype.getThumbnail = function(path, optargs, cb) {
// of arguments.
DropboxClient.prototype.copy = function(from_path, to_path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- from_path = escapePath(from_path);
- to_path = escapePath(to_path);
this.oauth.get(API_URI + '/fileops/copy?' +
querystring.stringify({root: 'dropbox'
, from_path: from_path
@@ -191,7 +218,6 @@ DropboxClient.prototype.copy = function(from_path, to_path, optargs, cb) {
// for explanation of arguments.
DropboxClient.prototype.createFolder = function(path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- path = escapePath(path);
this.oauth.get(API_URI + '/fileops/create_folder?' +
querystring.stringify({root: 'dropbox'
, path: path})
@@ -209,7 +235,6 @@ DropboxClient.prototype.createFolder = function(path, optargs, cb) {
// explanation of arguments.
DropboxClient.prototype.deleteItem = function(path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- path = escapePath(path);
this.oauth.get(API_URI + '/fileops/delete?' +
querystring.stringify({root: 'dropbox', path: path})
, optargs.token || this.access_token
@@ -226,8 +251,6 @@ DropboxClient.prototype.deleteItem = function(path, optargs, cb) {
// explanation of arguments.
DropboxClient.prototype.move = function(from_path, to_path, optargs, cb) {
if (typeof optargs == 'function') cb = optargs, optargs = {};
- from_path = escapePath(from_path);
- to_path = escapePath(to_path);
this.oauth.get(API_URI + '/fileops/move?' +
querystring.stringify({root: 'dropbox'
, from_path: from_path

0 comments on commit 6fa066d

Please sign in to comment.
Something went wrong with that request. Please try again.