class ActiveStorage::Service::DropboxService

Wraps the Dropbox Storage as an Active Storage service. See ActiveStorage::Service for the generic API documentation that applies to all services.

Attributes

config[R]

Public Class Methods

new(**config) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 9
def initialize(**config)
  @config = config
end

Public Instance Methods

delete(key) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 35
def delete(key)
  instrument :delete, key: key do
    client.delete("/"+key)
  rescue DropboxApi::Errors::NotFoundError
    # Ignore files already deleted
  end
end
delete_prefixed(prefix) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 43
def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    client.delete("/"+prefix[0..-2])
  rescue DropboxApi::Errors::NotFoundError
    # Ignore files already deleted
  end
end
download(key, &block) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 21
def download(key, &block)
  if block_given?
    instrument :streaming_download, key: key do
      stream(key, &block)
    end
  else
    instrument :download, key: key do 
      download_for(key)
    rescue DropboxApi::Errors::NotFoundError
      raise ActiveStorage::FileNotFoundError
    end
  end
end
exist?(key) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 51
def exist?(key)
  instrument :exist, key: key do |payload|
    begin
      answer = client.get_metadata("/"+key).present?
    rescue DropboxApi::Errors::NotFoundError
      answer = false
    end
    payload[:exist] = answer
    answer
  end
end
upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 13
def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil)
  instrument :upload, key: key, checksum: checksum do
    client.upload_by_chunks "/"+key, io
  rescue DropboxApi::Errors::UploadError
    raise ActiveStorage::IntegrityError
  end
end
url(key, expires_in:, filename:, disposition:, content_type:) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 63
def url(key, expires_in:, filename:, disposition:, content_type:)
  instrument :url, key: key do |payload|
    generated_url = file_for(key).link 
    payload[:url] = generated_url
    generated_url
  end
end

Private Instance Methods

client() click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 96
def client
  @client ||= DropboxApi::Client.new(config.fetch(:access_token))
end
download_for(key) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 79
def download_for(key)
  client.download("/"+key) do |chunk|
    return chunk.force_encoding(Encoding::BINARY)
  end
end
file_for(key) click to toggle source
# File lib/active_storage/service/dropbox_service.rb, line 75
def file_for(key)
  client.get_temporary_link("/"+key)
end
stream(key) { |chunk| ... } click to toggle source

Reads the file for the given key in chunks, yielding each to the block.

# File lib/active_storage/service/dropbox_service.rb, line 86
def stream(key)
  begin
    file = client.download("/"+key) do |chunk|
      yield chunk
    end
  rescue DropboxApi::Errors::NotFoundError
    raise ActiveStorage::FileNotFoundError
  end
end