Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Updated version of file browser demo using dropbox-node v0.2.0.
var sys = require('sys')
, DropboxClient = require('dropbox').DropboxClient
, express = require('express')
, app = express.createServer()
, consumer_key = '85m56oj9a6ekr1k'
, consumer_secret = 'kirag26pr0pbf3z'
app.configure(function () {
app.use(express.logger())
app.use(express.bodyDecoder())
app.use(express.cookieDecoder())
app.use(express.session())
})
// CSS file request.
app.get('/*.css', function (req, res) {
res.render(req.params[0] + '.sass', { layout: false })
})
// Login page.
app.get('/', function (req, res) {
res.render('login.jade', {
locals: { title: 'Dropbox File Browser' }
})
})
// Dropbox credential processing.
app.post('/process_creds', function (req, res) {
// Create a DropboxClient and initialize it with an access token pair.
req.session.db = new DropboxClient(consumer_key, consumer_secret)
req.session.db.getAccessToken(req.body.email, req.body.password, function (err) {
if (err) return console.log('Error: ' + sys.inspect(err))
res.redirect('/file_browser')
})
})
// File browser page.
app.get('/file_browser(/*)?', function (req, res) {
// Fetch target metadata and render the page.
if (req.session.db) {
req.session.db.getMetadata(req.params[1] || '', function (err, metadata) {
if (err) return console.log('Error: ' + sys.inspect(err))
// NOTE: Use this to strip leading path(s): str.replace(/^.*\//g, '')
res.render('file_browser.jade', {
locals: {
title: 'Dropbox File Browser',
current_dir: (metadata.path.length > 0) ? metadata.path : 'root',
items: metadata.contents
}
})
})
} else res.redirect('home')
})
app.listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.