janko / paperclip-dropbox

[OBSOLETE] Extends Paperclip with Dropbox storage.
MIT License
148 stars 36 forks source link

This gem is not maintained anymore and became obsolete. Over time Dropbox made some significant changes to its API, which breaks this gem's core functionality. You shouldn't be using it anymore.

While we're here, you also shouldn't be using Paperclip anymore, you should use Shrine instead.


Paperclip Dropbox

This gem extends Paperclip with Dropbox storage.

Setup

gem "paperclip-dropbox", ">= 1.1.7"

Example:

class User < ActiveRecord::Base
  has_attached_file :avatar,
    :storage => :dropbox,
    :dropbox_credentials => Rails.root.join("config/dropbox.yml"),
    :dropbox_options => {...}
end

Your config/dropbox.yml:

app_key: "..."
app_secret: "..."
access_token: "..."
access_token_secret: "..."
user_id: "..."
access_type: "dropbox|app_folder"

You can also pass a Proc to :dropbox_credentials, if you want the credentials to be dynamically evaluated.

In order to fill these in, you must create a Dropbox app and authorize it. There are two types of Dropbox apps: App folder or Full Dropbox. You can read about the differences and gotchas in this wiki. When you decide, don't forget to put "dropbox" or "app_folder" as the :access_type in your credentials.

After you have created an app, you will be given the "App key" and "App secret". Provide these and the access type to the authorization Rake task:

$ rake dropbox:authorize APP_KEY=your_app_key APP_SECRET=your_app_secret ACCESS_TYPE=dropbox|app_folder

For non-Rails projects, you must require this task in your Rakefile:

# Rakefile
load "paperclip/dropbox/tasks.rake"

Follow the instructions, and it will authorize the Dropbox app and output the rest of the credentials. Then you can fill in the rest of config/dropbox.yml.

And that's it. Everything should work now :)

The :dropbox_credentials option

:dropbox_credentials => Rails.root.join("config/dropbox.yml")
# or
:dropbox_credentials => {app_key: "foo", app_secret: "bar", ...}

For the list of required credentials, take a look at the config/dropbox.yml above.

The YAML file supports ERB:

app_key: <%= ENV["DROPBOX_APP_KEY"] %>

And it supports environment nesting (just like database.yml):

development:
  app_key: "..."
  ...
production:
  app_key: "..."
  ...

The :dropbox_options option

This is a hash containing any of the following options:

For example:

class User < ActiveRecord::Base
  has_attached_file :avatar,
    :storage => :dropbox,
    :dropbox_credentials => Rails.root.join("config/dropbox.yml"),
    :dropbox_options => {environment: ENV["RACK_ENV"]}
end

In Rails you don't need to specify it.

The :dropbox_visibility option

This is a string "public" (default) or "private".

class User < ActiveRecord::Base
  has_attached_file :avatar,
    :storage => :dropbox,
    :dropbox_credentials => Rails.root.join("config/dropbox.yml"),
    :dropbox_visibility => 'public'
end

The :path option

To change the path of the file, use Paperclip's :path option:

:path => ":style/:id_:filename"  # Defaults to ":filename"

In "Full Dropbox" mode with "public" visibility it will automatically be searched in the Public folder, so there is not need to specify it.

URL options

If you want to force the browser to always download the file, you can use the :download option.

user.avatar.url(:download => true)

Check if the file exists

It may happen that a file on Dropbox gets deleted remotely. To check this in your application, you can use the #exists? method:

user.avatar.exists?
# user.avatar.exists?(style)

License

MIT License