Created
November 18, 2014 12:06
-
-
Save brunofrank/24cacd14ffac4687ccf1 to your computer and use it in GitHub Desktop.
Dropbox class for CarrierWave
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'dropbox_sdk' | |
class CarrierWave::Uploader::Base | |
add_config :dropbox_app_key | |
add_config :dropbox_app_secret | |
add_config :dropbox_access_token | |
add_config :dropbox_access_token_secret | |
add_config :dropbox_user_id | |
add_config :dropbox_access_type | |
configure do |config| | |
config.storage_engines[:dropbox] = 'CarrierWave::Storage::Dropbox' | |
end | |
end | |
module CarrierWave | |
module Storage | |
class Dropbox < Abstract | |
# Stubs we must implement to create and save | |
# files (here on Dropbox) | |
# Store a single file | |
def store!(file) | |
#location = (config[:access_type] == "dropbox") ? "/#{location}" : uploader.store_path | |
location = uploader.store_path | |
dropbox_client.put_file(location, file.to_file) | |
end | |
# Retrieve a single file | |
def retrieve!(file) | |
CarrierWave::Storage::Dropbox::File.new(uploader, config, uploader.store_path(file), dropbox_client) | |
end | |
def dropbox_client | |
@dropbox_client ||= begin | |
session = DropboxSession.new(config[:app_key], config[:app_secret]) | |
session.set_access_token(config[:access_token], config[:access_token_secret]) | |
DropboxClient.new(session, config[:access_type]) | |
end | |
end | |
private | |
def config | |
@config ||= {} | |
@config[:app_key] ||= uploader.dropbox_app_key | |
@config[:app_secret] ||= uploader.dropbox_app_secret | |
@config[:access_token] ||= uploader.dropbox_access_token | |
@config[:access_token_secret] ||= uploader.dropbox_access_token_secret | |
@config[:access_type] ||= uploader.dropbox_access_type || "dropbox" | |
@config[:user_id] ||= uploader.dropbox_user_id | |
@config | |
end | |
class File | |
include CarrierWave::Utilities::Uri | |
attr_reader :path | |
def initialize(uploader, config, path, client) | |
@uploader, @config, @path, @client = uploader, config, path, client | |
end | |
def url | |
file_dl_path = @path.gsub('Public/', '') | |
"https://dl.dropboxusercontent.com/u/#{@config[:user_id]}/#{file_dl_path}" | |
end | |
def delete | |
path = @path | |
path = "/#{path}" if @config[:access_type] == "dropbox" | |
begin | |
# @client.file_delete(path) | |
rescue DropboxError | |
end | |
end | |
end | |
end | |
end | |
end | |
CarrierWave.configure do |config| | |
config.dropbox_app_key = '' | |
config.dropbox_app_secret = '' | |
config.dropbox_access_token = '' | |
config.dropbox_access_token_secret = '' | |
config.dropbox_user_id = | |
config.dropbox_access_type = "dropbox" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment