├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── examples └── db-browser │ ├── app.js │ ├── public │ └── style.css │ └── views │ ├── file_browser.jade │ ├── layout.jade │ └── login.jade ├── index.js ├── lib ├── dropbox-node.js ├── errors.js └── util │ ├── escape-path.js │ └── stringify-params.js ├── package.json ├── spec └── DropboxClientSpec.js └── specs.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | example.js 3 | notes.txt -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | examples 3 | example.js 4 | notes.txt 5 | .git 6 | .gitignore 7 | .npmignore 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Evan Meagher 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dropbox-node 2 | 3 | An OAuth-enabled Node.js client for working with the Dropbox API. 4 | 5 | ## DISCLAIMER 6 | 7 | This project has been deprecated in favor of [Dropbox's official JavaScript library](https://github.com/dropbox/dropbox-js). 8 | 9 | ## Installation 10 | 11 | dropbox-node depends on [node-oauth](http://github.com/ciaranj/node-oauth). 12 | 13 | To install via npm 14 | 15 | npm install dropbox-node 16 | 17 | To install by hand, download the module and create a symlink in `~/.node_libraries` 18 | 19 | $ ln -s /path/to/dropbox-node/ ~/.node_libraries/dropbox-node 20 | 21 | ## Usage 22 | 23 | To start, grab a consumer key and secret from [dropbox.com/developers](https://dropbox.com/developers). 24 | 25 | ### Object construction and access key pair retrieval 26 | First construct a DropboxClient object, passing in the consumer key and secret. 27 | 28 | var dropbox = new DropboxClient(consumer_key, consumer_secret) 29 | 30 | Before calling any Dropbox API methods, an access token pair must be obtained. This can be done one of two ways: 31 | 32 | 1. If the access token and secret are known a priori, they can be passed directly into the DropboxClient constructor. 33 | 34 | var dropbox = new DropboxClient(consumer_key, consumer_secret, 35 | access_token, access_token_secret) 36 | 37 | 2. Otherwise, `getAccessToken` must be called in order to initialize the OAuth credentials. 38 | 39 | dropbox.getAccessToken(dropbox_email, dropbox_password, callback) 40 | 41 | The callback given to `getAccessToken` takes an error object, an access token, and an access token secret (see example below). **Please note that users' passwords should never be stored.** It is best to acquire a token once and then use it for all subsequent requests. 42 | 43 | ### Calling API methods 44 | 45 | dropbox-node provides methods covering [each of the Dropbox API methods](https://www.dropbox.com/developers/docs). For example, to fetch and print the display name and email address associated with your account: 46 | 47 | dropbox.getAccountInfo(function (err, data) { 48 | if (err) console.log('Error: ' + err) 49 | else console.log(data.display_name + ', ' + data.email) 50 | }) 51 | 52 | Note that (at least at the start of a user's session) a valid access token pair must be obtained prior to interacting with the rest of the Dropbox API. This means that the API methods must be invoked within the callback passed to `getAccessToken` unless it is guaranteed that `getAccessToken` was called previously. As an example of this latter case, if building a web app, one could call API methods directly in a route that can only be accessed after going through a sign-in phase in which a call to `getAccessToken` is made. 53 | 54 | Here we upload a file and remotely move it around before deleting it. 55 | 56 | dropbox.getAccessToken(email, pwd, function (err, token, secret) { 57 | // Upload foo.txt to the Dropbox root directory. 58 | dropbox.putFile('foo.txt', 'foo.txt', function (err, data) { 59 | if (err) return console.error(err) 60 | 61 | // Move it into the Public directory. 62 | dropbox.move('foo.txt', 'Public/foo.txt', function (err, data) { 63 | if (err) return console.error(err) 64 | 65 | // Delete the file. 66 | dropbox.deleteItem('Public/foo.txt', function (err, data) { 67 | if (err) console.error(err.stack) 68 | }) 69 | }) 70 | }) 71 | }) 72 | 73 | For a more practical example, check out this [walkthrough of building a simple Dropbox file browser](http://evanmeagher.net/2010/10/dropbox-file-browser). 74 | 75 | ### Optional arguments 76 | 77 | Optional arguments (as specified in the [Dropbox API documentation](https://www.dropbox.com/developers/docs)) can be given to API methods via an argument object. 78 | 79 | For example, here we call `getAccountInfo` and direct the API to include the HTTP status code in the response. 80 | 81 | dropbox.getAccountInfo({ status_in_response: true }, callback) 82 | 83 | Each method (except `getAccessToken`) can optionally take an access token and an access token secret as strings. This is the one way to get around having to call `getAccessToken` in cases where a valid key pair is known. 84 | 85 | For example, here we fetch the metadata about the Dropbox root directory, passing in an explicit key pair stored in variables. 86 | 87 | dropbox.getMetadata('', { token: token, secret: secret }, callback) 88 | 89 | ## API 90 | 91 | ### new DropboxClient() 92 | 93 | ### DropboxClient#getAccessToken(email, password, callback(err, access_token, access_token_secret)) 94 | 95 | Fetches an access token and secret based on the email and password given. Stores the token and secret in the DropboxClient instance and calls the callback them. 96 | 97 | ### DropboxClient#getAccountInfo([optargs], callback(err, accountInfo)) 98 | https://www.dropbox.com/developers/reference/api#account-info 99 | 100 | Gets account information from the client. 101 | 102 | ### DropboxClient#createAccount(email, first_name, last_name, password, [optargs], callback(err, accountInfo)) 103 | 104 | Creates a new Dropbox account. 105 | 106 | ### DropboxClient#getFile(path, [optargs], [callback(err, body)]) 107 | https://www.dropbox.com/developers/reference/api#files-GET 108 | 109 | Retrieves a file specified by the path. `callback` will be called with a possible error and the buffer of the contents of the file. This method also returns a readable stream that can be used to pipe the contents. 110 | 111 | ```js 112 | dropboxClient('mybigfile.mpeg').pipe(fs.createWriteStream('localbigfile.mpeg'); 113 | ``` 114 | 115 | `optargs` can also have a `rev` field to specify the revision of the file to download, and `range` for [HTTP Range Retrieval Requests](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2). 116 | 117 | ```js 118 | // download the first 1024 byte 119 | dropboxClient('file.zip', { range: 'bytes=0-1024'}, function(err, data) { 120 | console.log(data.length); // 1024. that is if the file is at least 1024 bytes 121 | }); 122 | ``` 123 | 124 | ### DropboxClient#putFile(filepath, remotepath, [optargs], callback(err, metadata)) 125 | https://www.dropbox.com/developers/reference/api#files_put 126 | 127 | Uploads a file specified by `filepath` to `remotepath` on Dropbox. Dropbox currently does not support streaming uploads, and the max upload is 150 MB. `optargs` can also take additional fields `overwrite` and `parent_rev`. 128 | 129 | ### DropboxClient#put(contents, remotepath, [optargs], callback(err, metadata)) 130 | o 131 | Similar to `putFile()` but places `contents` into a created file at `remotepath`. `contents` can be a buffer or string. 132 | 133 | ### DropboxClient#getMetadata(path, [optargs], callback(err, metadata)) 134 | https://www.dropbox.com/developers/reference/api#metadata 135 | 136 | Gets metadata of file/folder specified by `path`. `optargs` can have fields `hash`, `list`, `include_deleted` and `rev`. 137 | 138 | ### DropboxClient#delta([cursor], [optargs], callback(err, changes)) 139 | https://www.dropbox.com/developers/reference/api#delta 140 | 141 | Keeps up with changes from a client's Dropbox. `changes` is an array of arrays with first element as the path and second as metadata. 142 | 143 | ### DropboxClient#changesStream([startingCursor], [optargs]) 144 | Convenient method that provides a more friendly API to `delta()`. Returns an event emitter that emits `data` events with `path` and `metadata` parameters on changes to the client's Dropbox. Also can emit `reset` and `error` events. The returned event emitter also has a `pause()` and `resume()` methods. 145 | 146 | ### DropboxClient#search(folderpath, query, [optargs], callback(err, results)) 147 | https://www.dropbox.com/developers/reference/api#search 148 | 149 | Searches `folderpath` for files matching `query`. `results` is an array of metadata. `optargs` can take `file_limit` and `include_deleted`. 150 | 151 | ### DropboxClient#getThumbnail(filepath, [optargs], [callback(err, body, metadata)]) 152 | https://www.dropbox.com/developers/reference/api#thumbnails 153 | 154 | Downloads a thumbnail image located at `filepath`. Like `getFile()`, the `callback` can get buffered data or the returned readable stream can be piped. `optargs` can take `format` and `size` fields. 155 | 156 | ### DropboxClient#shares(path, [optargs], [callback(err, link)]) 157 | https://www.dropbox.com/developers/reference/api#shares 158 | 159 | Creates and gets a link to file/folder specified by `path`. 160 | 161 | ### DropboxClient#media(filepath, [optargs], [callback(err, link)]) 162 | https://www.dropbox.com/developers/reference/api#media 163 | 164 | Creates and gets a direct link to file specified by `filepath`. 165 | 166 | ### DropboxClient#copy(from_path, to_path, [optargs], callback) 167 | https://www.dropbox.com/developers/reference/api#fileops-copy 168 | 169 | Copies a file. `from_copy_ref` field can be given in `optargs` to use it instead of `from_path`. 170 | 171 | ### DropboxClient#createFolder(path, [optargs], callback(err, metadata)) 172 | https://www.dropbox.com/developers/reference/api#fileops-create-folder 173 | 174 | Creates a folder at the given path. 175 | 176 | ### DropboxClient#deleteItem(path, [optargs], callback(err, metadata)) 177 | https://www.dropbox.com/developers/reference/api#fileops-delete 178 | 179 | Deletes file or folder from path. 180 | 181 | ### DropboxClient#move(from_path, to_path, [optargs], callback(err, metadata)) 182 | https://www.dropbox.com/developers/reference/api#fileops-move 183 | 184 | Moves a file to another path. 185 | 186 | 187 | ## Testing 188 | 189 | dropbox-node depends on [jasmine-node](http://github.com/mhevery/jasmine-node) for testing. Note that the currently-implemented tests are trivial, due to a lack of a way to effectively mock the Dropbox API. 190 | 191 | Run specs with `node specs.js` from the root `dropbox-node` directory. 192 | 193 | ## TODO 194 | * Improve test coverage. 195 | * Improve documentation. 196 | * Add ability to interact with application sandboxes. 197 | -------------------------------------------------------------------------------- /examples/db-browser/app.js: -------------------------------------------------------------------------------- 1 | // Read dropbox key and secret from the command line. 2 | var consumer_key = process.argv[2] 3 | , consumer_secret = process.argv[3]; 4 | 5 | if (consumer_key == undefined || consumer_secret == undefined) { 6 | console.log("Usage: node app.js "); 7 | process.exit(1); 8 | } 9 | 10 | var util = require('util') 11 | , DropboxClient = require('../../lib/dropbox-node').DropboxClient 12 | , express = require('express') 13 | , app = express.createServer(); 14 | 15 | // Create and configure an Express server. 16 | var app = express.createServer(); 17 | app.configure(function () { 18 | app.use(express.static(__dirname + '/public')) 19 | , app.use(express.logger()) 20 | , app.use(express.bodyParser()) 21 | , app.use(express.cookieParser()) 22 | , app.use(express.session({ secret: '1ts-s3cr3t!'} )); 23 | }); 24 | 25 | // Login page. 26 | app.get('/', function (req, res) { 27 | res.render('login.jade', { 28 | locals: { 29 | title: 'Dropbox File Browser' 30 | } 31 | }); 32 | }); 33 | 34 | // Dropbox credential processing. 35 | app.post('/process_creds', function (req, res) { 36 | // Create a DropboxClient and initialize it with an access token pair. 37 | var dropbox = new DropboxClient(consumer_key, consumer_secret); 38 | dropbox.getAccessToken(req.body.email, 39 | req.body.password, 40 | function (err, token, secret) { 41 | req.session.access_token = token; 42 | req.session.access_token_secret = secret; 43 | res.redirect('/file_browser'); 44 | }); 45 | }); 46 | 47 | // File browser page. 48 | app.get('/file_browser(/*)?', function (req, res) { 49 | // Fetch target metadata and render the page. 50 | if (req.session.access_token && req.session.access_token_secret) { 51 | var dropbox = new DropboxClient(consumer_key, 52 | consumer_secret, 53 | req.session.access_token, 54 | req.session.access_token_secret); 55 | dropbox.getMetadata(req.params[1] || '', function (err, metadata) { 56 | if (err) return console.log('Error: ' + util.inspect(err)); 57 | res.render('file_browser.jade', { 58 | locals: { 59 | title: 'Dropbox File Browser' 60 | , current_dir: (metadata.path.length > 0) ? metadata.path : 'root' 61 | , items: metadata.contents 62 | } 63 | }); 64 | }); 65 | } else res.redirect('home'); 66 | }); 67 | 68 | app.listen(3000); 69 | console.log('Dropbox browser running on port ' + app.address().port); 70 | -------------------------------------------------------------------------------- /examples/db-browser/public/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: "Helvetica Neue", "Lucida Grande", "Arial"; 3 | font-size: 13px; 4 | padding: 0; 5 | } 6 | 7 | div#wrapper { 8 | margin: 0 auto; 9 | padding: 25px; 10 | width: 500px; 11 | border: 3px solid #eee; 12 | background-color: #fefefe; 13 | } 14 | 15 | h1 { 16 | font-size: 24px; 17 | text-shadow: 1px 2px 2px #ddd; 18 | color: #333; 19 | } 20 | 21 | label { 22 | float: left; 23 | width: 5em; 24 | margin-right: 1em; 25 | } 26 | -------------------------------------------------------------------------------- /examples/db-browser/views/file_browser.jade: -------------------------------------------------------------------------------- 1 | h1 #{title} 2 | h2 #{current_dir} 3 | ul#dir_contents 4 | - items.forEach(function(item) { 5 | li 6 | - if(item.is_dir) 7 | a(href='/file_browser' + item.path) #{item.path} 8 | - else 9 | #{item.path} (#{item.size}) 10 | - }) 11 | -------------------------------------------------------------------------------- /examples/db-browser/views/layout.jade: -------------------------------------------------------------------------------- 1 | !!! 5 2 | html 3 | head 4 | title #{title} 5 | link(rel='stylesheet', href='/style.css') 6 | body 7 | #wrapper 8 | != body 9 | -------------------------------------------------------------------------------- /examples/db-browser/views/login.jade: -------------------------------------------------------------------------------- 1 | h1 Enter your Dropbox credentials 2 | form(method='post', action='/process_creds') 3 | div 4 | label(for='email') Email: 5 | input(type='text', name='email', id='email') 6 | div 7 | label(for='password') Password: 8 | input(type='password', name='password', id='password') 9 | div#editArticleSubmit 10 | input(type='submit', value='Login', class='submit') 11 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/dropbox-node'); -------------------------------------------------------------------------------- /lib/dropbox-node.js: -------------------------------------------------------------------------------- 1 | var pathLib = require('path') 2 | , querystring = require('querystring') 3 | , fs = require('fs') 4 | , escapePath = require('./util/escape-path') 5 | , stringifyParams = require('./util/stringify-params') 6 | , errors = require('./errors') 7 | , EventEmitter = require('events').EventEmitter 8 | , request = require('request') 9 | , API_URI = 'https://api.dropbox.com/1' 10 | , CONTENT_API_URI = 'https://api-content.dropbox.com/1'; 11 | 12 | 13 | // Returns a Dropbox Client, through which API calls can be made. 14 | var DropboxClient = exports.DropboxClient = 15 | function(consumer_key, consumer_secret, access_token, access_token_secret, options) { 16 | options = options || {}; 17 | this.consumer_key = consumer_key; 18 | this.consumer_secret = consumer_secret; 19 | this.access_token = access_token || undefined; 20 | this.access_token_secret = access_token_secret || undefined; 21 | this.root = options.sandbox ? 'sandbox' : 'dropbox'; 22 | } 23 | 24 | 25 | // Creates a request to the API with OAuth credentials 26 | DropboxClient.prototype.request = 27 | function(method, uri, optargs, body, callback) { 28 | if (typeof body === 'function') callback = body, body = undefined; 29 | optargs = optargs || {}; 30 | var oauth = { 31 | consumer_key: this.consumer_key 32 | , consumer_secret: this.consumer_secret 33 | , token: optargs.token || this.access_token 34 | , token_secret: optargs.secret || this.access_token_secret 35 | }; 36 | 37 | var requestOptions = { uri: uri, oauth: oauth }; 38 | if (body) { 39 | if (method === 'get') { 40 | requestOptions.headers = { Range: body }; 41 | } else { 42 | requestOptions.body = body; 43 | } 44 | } 45 | 46 | return request[method](requestOptions, callback ? 47 | function(err, res, body) { 48 | if (err) return callback(err); 49 | var contentType = res.headers['content-type']; 50 | 51 | // check if the response body is in JSON format 52 | if (contentType === 'application/json' || 53 | contentType === 'text/javascript') { 54 | body = JSON.parse(body); 55 | if (body.error) { 56 | var err = new Error(body.error); 57 | err.statusCode = res.statusCode; 58 | return callback(err); 59 | } 60 | 61 | } else if (errors[res.statusCode]) { 62 | var err = new Error(errors[res.statusCode]); 63 | err.statusCode = res.statusCode; 64 | return callback(err); 65 | } 66 | 67 | // check for metadata in headers 68 | if (res.headers['x-dropbox-metadata']) { 69 | var metadata = JSON.parse(res.headers['x-dropbox-metadata']); 70 | } 71 | 72 | callback(null, body, metadata); 73 | } : undefined); 74 | }; 75 | 76 | 77 | // Convenience methods 78 | ['get', 'post', 'put'].forEach(function(method) { 79 | DropboxClient.prototype['_' + method] = function(uri, optargs, body, callback) { 80 | return this.request(method, uri, optargs, body, callback); 81 | }; 82 | }); 83 | 84 | 85 | // Fetches an access token and access token secret pair based on the user's 86 | // email and password. As well as being stored internally, the key pair is 87 | // returned to the callback in case the application developer requires it. 88 | DropboxClient.prototype.getAccessToken = function(email, pwd, cb) { 89 | // Validate email and pwd. 90 | if (!email || !pwd) return cb(Error('Invalid arguments. Please provide ' + 91 | 'a valid email and password.')); 92 | 93 | var uri = API_URI + '/token?' + 94 | querystring.stringify({email: email, password: pwd}); 95 | var self = this; 96 | this._get(uri, {}, function(err, data) { 97 | if (err) return cb(err); 98 | // Store the key pair and fire callback. 99 | self.access_token = data.token; 100 | self.access_token_secret = data.secret; 101 | cb(null, data.token, data.secret); 102 | }); 103 | } 104 | 105 | 106 | // Retrieves information about the user's account as a JSON response. 107 | DropboxClient.prototype.getAccountInfo = function(optargs, cb) { 108 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 109 | var uri = API_URI + '/account/info/' + 110 | (optargs.status_in_response ? '?status_in_response=' + 111 | optargs.status_in_response : ''); 112 | this._get(uri, optargs, cb); 113 | } 114 | 115 | 116 | // Create a new Dropbox account. 117 | DropboxClient.prototype.createAccount = function(email, first_name, last_name 118 | , password, optargs, cb) { 119 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 120 | var params = { 121 | email: email 122 | , first_name: first_name 123 | , last_name: last_name 124 | , password: password 125 | , status_in_response: optargs.status_in_response 126 | } 127 | var uri = API_URI + '/account?' + querystring.stringify(params); 128 | 129 | this._get(uri, {}, cb); 130 | } 131 | 132 | 133 | // Retrieves contents of a file specified by path argument, relative to 134 | // user's Dropbox root. 135 | DropboxClient.prototype.getFile = function(path, optargs, cb) { 136 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 137 | else if (!optargs) optargs = {}; 138 | var uri = CONTENT_API_URI + '/files/' + this.root + '/' + escapePath(path) + 139 | (optargs.rev ? '?rev=' + optargs.rev : ''); 140 | 141 | return this._get(uri, optargs, optargs.range, cb); 142 | } 143 | 144 | 145 | // Uploads contents of a file specified by file to the remote path 146 | DropboxClient.prototype.putFile = function(file, path, optargs, cb) { 147 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 148 | var uri = CONTENT_API_URI + '/files_put/' + this.root + '/' + escapePath(path) + 149 | '?' + stringifyParams(optargs); 150 | var self = this; 151 | 152 | fs.readFile(file, function(err, data) { 153 | if (err) return cb(err); 154 | self.request('put', uri, optargs, data, cb); 155 | }); 156 | } 157 | 158 | 159 | // Uploads contents to the specified path 160 | DropboxClient.prototype.put = function(content, path, optargs, cb) { 161 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 162 | var uri = CONTENT_API_URI + '/files_put/' + this.root + '/' + escapePath(path) + 163 | '?' + stringifyParams(optargs); 164 | 165 | this._put(uri, optargs, content, cb); 166 | } 167 | 168 | 169 | // Gets metadata of file/folder specified by path relative to user's 170 | // Dropbox root. 171 | DropboxClient.prototype.getMetadata = function(path, optargs, cb) { 172 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 173 | var uri = API_URI + '/metadata/' + this.root + '/' + escapePath(path) + '?' + 174 | stringifyParams(optargs); 175 | 176 | this._get(uri, optargs, cb); 177 | } 178 | 179 | 180 | // Downloads a minimized jpeg thumbnail for a photo. See 181 | // https://www.dropbox.com/developers/docs#thumbnails for a list of 182 | // valid size specifiers. 183 | DropboxClient.prototype.getThumbnail = function(path, optargs, cb) { 184 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 185 | optargs = optargs || {}; 186 | var uri = CONTENT_API_URI + '/thumbnails/' + this.root + '/' + escapePath(path) + 187 | '?' + stringifyParams(optargs); 188 | 189 | return this._get(uri, optargs, cb); 190 | } 191 | 192 | 193 | // Creates and gets a link to the specified file or folder. 194 | // See https://www.dropbox.com/developers/docs/api#shares for explanation 195 | // of arguments. 196 | DropboxClient.prototype.share = function(path, optargs, cb) { 197 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 198 | optargs = optargs || {}; 199 | var uri = API_URI + '/shares/' + this.root + '/' + escapePath(path) + 200 | '?' + stringifyParams(optargs); 201 | 202 | return this._post(uri, optargs, cb); 203 | } 204 | 205 | 206 | // Creates and gets a direct link to the specified file. 207 | // See https://www.dropbox.com/developers/docs/api#media for explanation 208 | // of arguments. 209 | DropboxClient.prototype.media = function(path, optargs, cb) { 210 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 211 | optargs = optargs || {}; 212 | var uri = API_URI + '/media/' + this.root + '/' + escapePath(path) + 213 | '?' + stringifyParams(optargs); 214 | 215 | return this._post(uri, optargs, cb); 216 | } 217 | 218 | 219 | // Copies a file or folder to a new location. 220 | // See https://www.dropbox.com/developers/docs#fileops-copy for explanation 221 | // of arguments. 222 | DropboxClient.prototype.copy = function(from_path, to_path, optargs, cb) { 223 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 224 | optargs.root = this.root; 225 | if (!optargs.from_copy_ref) optargs.from_path = from_path; 226 | optargs.to_path = to_path; 227 | var uri = API_URI + '/fileops/copy?' + querystring.stringify( 228 | {root: this.root, from_path: from_path, to_path: to_path}); 229 | 230 | this._get(uri, optargs, cb); 231 | } 232 | 233 | 234 | // Creates a folder relative to the user's Dropbox root. 235 | // See https://www.dropbox.com/developers/docs#fileops-create-folder 236 | // for explanation of arguments. 237 | DropboxClient.prototype.createFolder = function(path, optargs, cb) { 238 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 239 | var uri = API_URI + '/fileops/create_folder?' + 240 | querystring.stringify({root: this.root, path: path}); 241 | 242 | this._get(uri, optargs, cb); 243 | } 244 | 245 | 246 | // Deletes a file or folder. 247 | // See https://www.dropbox.com/developers/docs#fileops-delete for 248 | // explanation of arguments. 249 | DropboxClient.prototype.deleteItem = function(path, optargs, cb) { 250 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 251 | var uri = API_URI + '/fileops/delete?' + 252 | querystring.stringify({root: this.root, path: path}); 253 | 254 | this._get(uri, optargs, cb); 255 | } 256 | 257 | 258 | // Moves a file or folder. 259 | // See https://www.dropbox.com/developers/docs#fileops-move for 260 | // explanation of arguments. 261 | DropboxClient.prototype.move = function(from_path, to_path, optargs, cb) { 262 | if (typeof optargs == 'function') cb = optargs, optargs = {}; 263 | var uri = API_URI + '/fileops/move?' + 264 | querystring.stringify({root: this.root, from_path: from_path, to_path: to_path}); 265 | 266 | this._get(uri, optargs, cb); 267 | } 268 | 269 | 270 | // Searches a folder 271 | // See https://www.dropbox.com/developers/reference/api#search 272 | DropboxClient.prototype.search = function(path, query, optargs, cb) { 273 | if (typeof optargs === 'function') cb = optargs, optargs = {}; 274 | optargs.query = query; 275 | var uri = API_URI + '/search/' + this.root + '/' + escapePath(path) + '?' + 276 | stringifyParams(optargs); 277 | 278 | this._get(uri, optargs, cb); 279 | } 280 | 281 | 282 | // Keep up with changes. 283 | // See https://www.dropbox.com/developers/reference/api#delta 284 | DropboxClient.prototype.delta = function(cursor, optargs, cb) { 285 | var cursorType = typeof cursor; 286 | if (cursorType === 'function') cb = cursor, optargs = {}; 287 | else if (typeof optargs === 'function') { 288 | cb = optargs; 289 | if (cursorType === 'object') { 290 | optargs = cursor; 291 | cursor = null; 292 | } else { 293 | optargs = {} 294 | } 295 | } 296 | 297 | var uri = API_URI + '/delta' + 298 | (cursor ? '?' + querystring.stringify({cursor: cursor}) : '') 299 | 300 | this._post(uri, optargs, cb); 301 | } 302 | 303 | 304 | // Continously stream changes through DropboxClient#delta 305 | // Returns an event emitter that has `pause()` and `resume()` methods. 306 | // It emits `reset` events on detta resets, and `data` events with 307 | // parameters `path` and `metadata` on new delta changes. 308 | DropboxClient.prototype.changesStream = function(cursor, optargs) { 309 | optargs = optargs || {}; 310 | var ee = new EventEmitter(); 311 | var iid; 312 | var self = this; 313 | 314 | function getDelta() { 315 | self.delta(cursor, optargs, function(err, data) { 316 | if (err) { return ee.emit('error', err); } 317 | 318 | // only emit changes if cursor was given 319 | if (cursor) { 320 | if (data.reset) { 321 | ee.emit('reset'); 322 | } 323 | 324 | for (var i = 0, len = data.entries.length; i < len; i++) { 325 | var e = data.entries[i]; 326 | ee.emit('data', e[0], e[1]); 327 | } 328 | } 329 | 330 | cursor = data.cursor; 331 | if (data.has_more) { 332 | ee.resume(); 333 | } 334 | }); 335 | } 336 | 337 | ee.resume = function() { 338 | getDelta(); 339 | clearInterval(iid); 340 | iid = setInterval(getDelta, 300000); // 5 minutes 341 | }; 342 | ee.resume(); 343 | 344 | ee.pause = function() { 345 | clearInterval(iid); 346 | }; 347 | 348 | return ee; 349 | } 350 | -------------------------------------------------------------------------------- /lib/errors.js: -------------------------------------------------------------------------------- 1 | // Response error codes 2 | module.exports = { 3 | '304': 'The folder contents have not changed' 4 | , '400': 'The extension is on Dropbox\'s ignore list.' 5 | , '403': 'An invalid copy operation was attempted (e.g. there is already a file at the given destination, or copying a shared folder into a shared folder).' 6 | , '404': 'The requested file or revision was not found.' 7 | , '406': 'There are too many file entries to return.' 8 | , '411': 'Chunked encoding was attempted for this upload, but is not supported by Dropbox.' 9 | , '415': 'The image is invalid and cannot be converted to a thumbnail.' 10 | } 11 | -------------------------------------------------------------------------------- /lib/util/escape-path.js: -------------------------------------------------------------------------------- 1 | // Escape URI-sensitive chars, but leave forward slashes alone. 2 | module.exports = function escapePath(p) { 3 | var p = encodeURIComponent(p) 4 | .replace(/%2F/g, '/') 5 | .replace(/\)/g, '%29') 6 | .replace(/\(/g, '%28') 7 | .replace(/!/g,'%21'); 8 | if (p[0] === '/') { p = p.slice(1); } 9 | return p; 10 | } 11 | -------------------------------------------------------------------------------- /lib/util/stringify-params.js: -------------------------------------------------------------------------------- 1 | var querystring = require('querystring'); 2 | 3 | // Stringify the valid params in optargs (excluding token and secret). 4 | module.exports = function stringifyParams(optargs) { 5 | var params = {}; 6 | Object.keys(optargs).filter(function (k) { 7 | return k !== 'token' && k !== 'secret'; 8 | }).forEach(function (k) { 9 | params[k] = optargs[k]; 10 | }); 11 | return querystring.stringify(params); 12 | } 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dropbox-node", 3 | "version": "0.4.0", 4 | "description": "A node.js client module for the official Dropbox API", 5 | "keywords": [ 6 | "cloud", 7 | "storage", 8 | "REST", 9 | "interface" 10 | ], 11 | "homepage": "http://github.com/evnm/dropbox-node", 12 | "author": "Evan Meagher (http://evanmeagher.net/)", 13 | "contributors": [ 14 | "Roly Fentanes (https://github.com/fent)" 15 | ], 16 | "main": "index", 17 | "repository": { 18 | "type": "git", 19 | "url": "git://github.com/evnm/dropbox-node.git" 20 | }, 21 | "scripts": { 22 | "test": "node specs.js" 23 | }, 24 | "dependencies": { 25 | "request": "2.9.x" 26 | }, 27 | "engines": { 28 | "node": "*" 29 | }, 30 | "directories": { 31 | "lib": "./lib" 32 | }, 33 | "devDependencies": {} 34 | } 35 | -------------------------------------------------------------------------------- /spec/DropboxClientSpec.js: -------------------------------------------------------------------------------- 1 | var DropboxClient = require('../lib/dropbox-node').DropboxClient, 2 | OAuth = require('oauth').OAuth; 3 | 4 | describe('dropbox-node', function() { 5 | oauth = new OAuth(null, null, null, null, 6 | null, null, 'HMAC-SHA1'); 7 | 8 | it('sets oauth object', function() { 9 | var dropbox = new DropboxClient(oauth, null, null); 10 | expect(dropbox.oauth).toBe(oauth); 11 | }); 12 | 13 | it('sets access token', function() { 14 | var dropbox = new DropboxClient(null, 'foo', null); 15 | expect(dropbox.access_token).toEqual('foo'); 16 | }); 17 | 18 | it('sets access token secret', function() { 19 | var dropbox = new DropboxClient(null, null, 'bar'); 20 | expect(dropbox.access_token_secret).toEqual('bar'); 21 | }); 22 | }); -------------------------------------------------------------------------------- /specs.js: -------------------------------------------------------------------------------- 1 | var jasmine = require('jasmine'); 2 | 3 | for (var key in jasmine) { 4 | global[key] = jasmine[key]; 5 | } 6 | 7 | var isVerbose = false, 8 | showColors = true; 9 | process.argv.forEach(function(arg) { 10 | switch (arg) { 11 | case '--color': showColors = true; break; 12 | case '--noColor': showColors = false; break; 13 | case '--verbose': isVerbose = true; break; 14 | } 15 | }); 16 | 17 | 18 | jasmine.executeSpecsInFolder(__dirname + '/spec', function(runner, log) { 19 | process.exit(runner.results().failedCount); 20 | }, isVerbose, showColors); 21 | --------------------------------------------------------------------------------