Tools

Working with Dropbox

Dropbox client applets allow only basic functionality for interacting with the file storage service. To get the full breadth of capabilities, you'll need to use their SDK and write your own code

The advent of high-speed networks and affordable high-capacity storage breathed new life into that once fickle online service  file hosting. Unlike the old BBS and FTP services, today's file hosting employs Web 2.0 standards to display an intuitive interface. It can handle a wide range of file formats, and it has become consistently reliable.

In this article, I examine one of the most popular such services: Dropbox. I'll look at what this service offers and how it interacts with its users. And after going over its SDK, I'll explain how to use it to build a custom service client. In the process, I'll demonstrate how to log into a user's account, how to move files to and from that account, how to do a query, and how to manage the account.

For reasons of length, all sample code is written with Cocoa and Objective-C in mind.

Overview of the Service

Dropbox is the brainchild of MIT graduates Drew Houston and Arash Ferdowsi. Established in 2007, the service gives users a place to store their files online. It lets users access those same files from any locale or platform. Dropbox is a cross-platform service, requiring only an AJAX-enabled Web browser to use.

A Dropbox account comes in two flavors. A free account entitles users up to 2 GB of space. Users can earn more storage space, up to a maximum of 8 GB, by inviting other users to join Dropbox.

A paid account entitles users to more storage space, from 50 GB to 1000 GB. Paid users get preferential treatment in terms of support and features. At the time of writing, both free and paid accounts offer the same feature set.

The typical Dropbox account uses the familiar file/folder metaphor to organize its user data (Figure 1). Above the file/folder list is a button toolbar, showing the possible actions users can make on their accounts. Above that toolbar is a sequence of tabs, each tab linked to a separate panel view.

Figure 1.

By default, the account gets two folders: Photos and Public. A click on the toolbar button New Folder adds an empty folder to the account. Setting a folder's check-box, then clicking the button Share a folder prompts for a user's e-mail address, inviting that user to share the folder. A click on a folder displays that folder's contents.

Accessing a Dropbox account with a Web browser has specific limitations. Some actions, for instance, take more steps on the browser than on the desktop. Consider the act of renaming a file. On Dropbox, a rename action takes about four steps. On Mac OS X, it takes at least two.

Then there is the lack of keyboard control. On the Windows desktop, a Ctrl-C copies a file; a Ctrl-V pastes that copy into the desired folder. This is not possible with the Web browser, which prefers a pointing device.

Finally, drag and drop support is minimal at best. Users can move a file to a folder by dragging. But if the desired folder is inside another folder, the same drag action does not cause a drill-down. Moreover, dragging a file or folder to the desktop does not start a download action.

Clients to the Service

To address the limits of the Web browser, Dropbox provides software clients for most major platforms. These clients enable users log into their accounts and access their files. But they do not provide access to the full range of Dropbox services. To get the right client for a given platform, go to http://www.dropbox.com/install.

Each Dropbox client brings its own user experience. Consider the OS X client (Figure 2). It runs as a background process. It has its own menu on which it lists the commands for controlling the process.

Figure 2.

The OS X client adds its own folder to the user's home directory. In that folder, it recreates the file/folder structure from user's account. The client keeps both folder and account synchronized. So, files added to the account will appear on the folder. Files removed from the same account will be removed from the folder.

Not all Dropbox clients are as transparent or as integrated as the OS X client. The iOS client, for instance, is a separate application. To use it, users may have to end their current app process. The clients are not scriptable. They cannot be controlled using an applet or a shell script. This makes the clients difficult to integrate with databases or SCM.

These Dropbox clients use SSL to connect securely with the user account. But they do not encrypt files on the fly. If a file is unencrypted to start, it stays unencrypted after being stored on the account. The same clients do not perform diffs, and they do not authenticate files copied to or from the account.

In short, these official Dropbox clients provide only a basic backup/restore feature. If users want other features, they will need a custom client solution.

SDK for the Service

To help developers write a custom client, Dropbox provides an SDK. This SDK defines a standard interface for linking with a user's account and for managing its files. It uses HTTPS as its network protocol, and renders each protocol command in REST. The SDK is available for five major languages.

With the SDK, we can add a new folder to the account or rename an existing one. We can upload a file to a specific folder or download it from the folder to the client platform. We can even query for details about a specific file or folder on that account.

At the time of writing, the latest stable version of the SDK is 1.0. Each SDK package has a different minor version depending on the target language. For instance, the ObjC package has the lowest version of 1.0.0. The Android and Java packages have the highest one at 1.2.2. It is not known if the version difference reflects the amount of work spent on the SDK package or the priority of support for that package.

Preparing to Link

Registration gives us two developer keys with which to enable a Dropbox session. Registration does require a valid Dropbox account. Each developer key should contain only alphanumeric characters. Any other characters will mean an invalid key. To check the keys, we could use the NSCharacterSet class as shown in Listing One. Here, we have the keys rendered as constant NSString objects. We create an instance of NSCharacterSet, one with only alphanumeric characters; then we invert that instance. Next, we use the instance method rangeOfCharacterFromSet: to check each key character against the inverted set. If the method returns a valid NSRange object, the key in question is invalid.

Listing Two shows how we prepare a controller object to support Dropbox. This controller class, FooDrop, declares the private property pOut_ in its @interface block. It also declares the accessor dboxClient with which to link to that property. Both property and accessor are instances of DBRestClient.

Next, in its @implementation block, FooDrop defines its awakeFromNib method. This method runs after FooDrop is loaded from its xib bundle. The first half of the method sets and validates the developer keys as described in Listing One. The second half creates an instance of DBSession using the factory method initWithConsumerKey:consumerSecret:. It sets FooDrop as the delegate and passes the DBSession instance to the class method setSharedSession:. This makes the DBSession instance available to other objects in the project, provided they import the Dropbox.h header.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!