Wednesday, October 12, 2011

Dropbox Updates Have Landed

Just wanted to let you guys know that I have made some updates to the Dropbox API Library. These changes are in reaction to the new functionality available in the new Dropbox API. There are a few new methods and I removed the link method. I will outline the changes below.

Here is the Github Repo

I did make some changes to some of the existing methods. The now accept an array of parameters so that you can specify different parameters that can be passed to the API. There were a few new methods that were added, they are as follows:
revisions($path, array $params = array(), $root='dropbox')
restore($path, $revision, $root='dropbox')
search($path, $query, array $params = array(), $root='dropbox')
shares($path, $root='dropbox')
media($path, $root='dropbox')
The revisions method
revisions($path, array $params = array(), $root='dropbox')
Will return an array of revisions for a particular file or the revisions of files in a particular folder.

The restore method
restore($path, $revision, $root='dropbox')
Will roll a particular file back to a previous revision. Specify the revision by using the 'rev' hash that is returned when you make a call to the revisions method.

The search method
search($path, $query, array $params = array(), $root='dropbox')
Will search a particular path in your dropbox folders for any files that match the query.

The shares and media methods have replaced the link method.
shares($path, $root='dropbox')
media($path, $root='dropbox')
Both methods link to a file in dropbox the difference is that media links directly to the file and should be used for things link streaming. Shares creates a new link to a file and is more geared towards publicly displaying your content. Both calls also return an expiration date which indicate when the link will become invalid.

As always if you have any questions or problems post them in the comments but as it stands right now things seem to be pretty stable.

6 comments:

  1. Hi Jim,
    Thanks for what looks like a great library, but I’m loading sparks into PyroCMS, a CMS built onto CI (not that this matters).

    So far, simple sparks have been a doddle and all went well.

    Now I’m using your dropbox spark, I’ve hit a problem as $params need to get passed.

    Your instructions say:

    ...load the library like this:

    $this->load->library('dropbox', $params);
    You can also use this library with Sparks. Simply install using sparks then call.

    $this->load->spark('dropbox/1.0.0');

    If I use

    $this->load->spark('dropbox/1.2.2');

    then of course, I get

    Call to a member function get_request_token() on a non-object ...

    as the $params (which is an array of credential) aren’t getting passed.

    If I try

    $this->load->spark('dropbox/1.2.2', $params);
    Could not autoload object of type ‘key’ (xxxx
    ...

    because it seems you can’t pass $params like that.

    Been through the spark docs too, and unless I’m missing something, I don’t see how I’d do this. Any suggestions? Thanks!

    ReplyDelete
    Replies
    1. Hmm yes it appears that sparks has altered their loader. The second parameter in the load sparks call is now used to autoload additional resources not pass parameters. I will have to investigate this further. In the mean time you can move the library files into the appropriate places then load it as you would a standard CI library.

      Delete
  2. Great library that I almost have up and running in my app.

    I am running into an issue when I try upload a file into Dropbox.

    For the $filepath param, I'm passing in $_FILES["userfile"]["temp_name"] from the form where the user selected the file. It works but it uploads the file with the wacky tmp file name.

    $_FILES["file"]["name"] doesn't work, since it really doesn't point to the file on the server, but it's got the name I want.

    How do I upload and get the filename I expect?

    Thanks!

    ReplyDelete
    Replies
    1. sorry, $_FILES["userfile"]["temp_name"] should be: $_FILES["userfile"]["tmp_name"] !

      I CAN get it to work with a few extra steps:

      >>>
      $dbobj = $this->dropbox->add('/'.$folder_name, $_FILES["userfile"]["tmp_name"], $params, 'sandbox');
      $tmp_name = substr($_FILES["userfile"]["tmp_name"],4);
      $dbobj = $this->dropbox->copy('/'.$folder_name.'/'.$tmp_name, '/'.$folder_name.'/'.$_FILES["userfile"]["name"], 'sandbox');
      $dbobj = $this->dropbox->delete('/'.$folder_name.'/'.$tmp_name, 'sandbox');
      <<<

      I'm just thinking that I'm missing something obvious here.

      Thanks for any wisdom you can share!

      -Paul

      Delete
    2. Hi Paul,

      Unfortunately the way the library is designed (and partly the way Dropboxes API works) the file name used is whatever it's named on your server. So since you are using tmp_name which is created by PHP you will get unpredictable file names.

      There are a few ways around this:

      First you could just rename the file on your server prior to adding it to dropbox. This is probably the better option simply because it will reduce the number of calls you make to the dropbox API.

      Second you could essentially do what you are already doing and alter the file name after it's added to dropbox. If you go this route I would recommend you use the 'move' method instead of a copy and delete. It accomplishes the rename with a single call instead of two.

      Delete
  3. Hello how I download a file from my dropbox?

    ReplyDelete