Ruby on Rails Connect to Dropbox : Authorization and Upload

I had something in mind when I tried Heroku but I can’t attach any file. The reason was because file attachment was not allowed. At the time, I was trying to build my side project which was using carrierwave as its file attachment handler. But it didn’t result any good, because every time I tried to upload a file it always showed error. That’s why I came up an idea, what if I use any kind of cloud storage service as its container to hold the file attachment. There were 2 service that came up in my mind, they were dropbox and ubuntu one.

So right now, I just successfully connecting Ruby on Rails app with Dropbox. Even though, I still can’t figure out that my idea could be realized with this implementation. But, just forget it for awhile, maybe this discovery could lead me into something better. Be positive right?

Before we start with rails, we should first create an account in dropbox, then go to developer site. Next, you should create an app in My Apps tab. After creation, you will be given an App Key and an App Secret which will be used in your programming code. Okay, initial step is done, now let’s dig into the code.

For ruby on rails, there’s a library which can be used to build an app for dropbox. You can go to the github page and follow the instruction for the installation. After the installation, you can try these step :

    1. Don’t forget to add the gem code in Gemfile : gem 'dropbox'
    2. If you missed the installation part, you can do it here by running the command : bundle install
    3. Create a controller named dropbox, you can create it by running : rails g controller dropbox index
    4. This is my dropbox_controller.rb looks like :
      require 'ftools'
      
      class DropboxController < ApplicationController
        def index
          @filebox = Filebox.new
        end       
      
        def authorize
          if params[:oauth_token] then
            dropbox_session = Dropbox::Session.deserialize(session[:dropbox_session])
            dropbox_session.authorize(params)
            session[:dropbox_session] = dropbox_session.serialize # re-serialize the authenticated session       
      
            redirect_to :action => 'upload'
          else
            dropbox_session = Dropbox::Session.new('your_consumer_key', 'your_consumer_secret')
            session[:dropbox_session] = dropbox_session.serialize
            redirect_to dropbox_session.authorize_url(:oauth_callback => url_for(:action => 'authorize'))
          end
        end
      
        def upload
          return redirect_to(:action => 'authorize') unless session[:dropbox_session]
          dropbox_session = Dropbox::Session.deserialize(session[:dropbox_session])
          return redirect_to(:action => 'authorize') unless dropbox_session.authorized?
      
          if request.method == 'POST' then
            dropbox_session.mode = :dropbox
            File.copy("/" + params[:file].tempfile.path, '~/Documents/dropbox/' + params[:file].original_filename)
            params[:file].tempfile.unlink
            dropbox_session.upload '~/Documents/dropbox/' + params[:file].original_filename, 'RCase'
            render :text => 'Uploaded OK'
          else
            redirect_to(:action => 'index', :notice => 'Upload Fail')
          end
        end
      end

      You can see in the upload action, I made a little change in request.method == 'POST'. I tried :post but it didn’t work so I changed it into that. Another change that I made was the upload process. I want to upload a file with precise name and I found it hard. So, before upload, I copied the file from /tmp folder and rename it with original filename, then I delete the file in /tmp. So when uploading, the system will read the duplicate one which is in certain folder (I admit this way is a bit static).

    5. Create 2 views, they are index.html.erb and _form.html.erb
      . This what index.html.erblooks like :

       <h1>Connect to Dropbox</h1>
      <%= notice %>
      <%= render 'form' %>

      and this what _form.html.erb looks like :

      <%= form_tag({:controller => :dropbox, :action => :upload}, :method => "post", :multipart => true) do |f| %>
      
        <div class="field">
          <%= notice %>
        </div>
        <div class="field">
          <%= label_tag "File name" %><br />
          <%= file_field_tag :file %>
        </div>
        <div class="actions">
          <%= submit_tag "Upload" %>
        </div>
      <% end %>

Those were the settings that should be made to connect your Ruby on Rails 3 app to dropbox. For the first connect, you will be shown an authorization page from Dropbox, when you allow it to authorize your app, Dropbox will create a session which you can use for the next connection.

I would like to clarify that I still can’t figure out that I can realize my idea by combining dropbox as attachment storage for carrierwave and mongoid. This is the first step to prove it whether I can do it or not. But for now, just enjoy the process.

Advertisements

2 thoughts on “Ruby on Rails Connect to Dropbox : Authorization and Upload

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s