Class: Service::DropboxService

Inherits:
Service
  • Object
show all
Defined in:
lib/active_storage/service/dropbox_service.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(**config) ⇒ DropboxService

Returns a new instance of DropboxService.



9
10
11
# File 'lib/active_storage/service/dropbox_service.rb', line 9

def initialize(**config)
  @config = config
end

Instance Method Details

#delete(key) ⇒ Object



35
36
37
38
39
40
41
# 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) ⇒ Object



43
44
45
46
47
48
49
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_storage/service/dropbox_service.rb', line 51

def exist?(key)
  instrument :exist, key: key do |payload|
    begin
      answer = client.("/"+key).present?
    rescue DropboxApi::Errors::NotFoundError
      answer = false
    end
    payload[:exist] = answer
    answer
  end
end

#upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/active_storage/service/dropbox_service.rb', line 13

def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {})
  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:) ⇒ Object



63
64
65
66
67
68
69
# 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