Class: ActiveStorage::Service::UploadcareService

Inherits:
ActiveStorage::Service show all
Includes:
Uploadcare::Rails::Internal::ActiveStorageServiceHelpers, Uploadcare::Rails::Internal::ActiveStorageUuidMapping
Defined in:
lib/active_storage/service/uploadcare_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key:, secret_key:, public: true, **options) ⇒ UploadcareService

Returns a new instance of UploadcareService.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_storage/service/uploadcare_service.rb', line 20

def initialize(public_key:, secret_key:, public: true, **options)
  super()
  @public = public
  @cdn_hostname = options.delete(:cdn_hostname) || options.delete(:cdn_cname)
  @http_open_timeout = options.delete(:open_timeout) || 5
  @http_read_timeout = options.delete(:read_timeout) || 30
  @http_write_timeout = options.delete(:write_timeout) || 30
  @key_uuid_map = {}
  @key_uuid_map_mutex = Mutex.new
  @client = Uploadcare::Client.new(public_key: public_key, secret_key: secret_key, **options)
end

Instance Attribute Details

#cdn_hostnameObject (readonly)

Returns the value of attribute cdn_hostname.



18
19
20
# File 'lib/active_storage/service/uploadcare_service.rb', line 18

def cdn_hostname
  @cdn_hostname
end

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/active_storage/service/uploadcare_service.rb', line 18

def client
  @client
end

Instance Method Details

#delete(key) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_storage/service/uploadcare_service.rb', line 69

def delete(key)
  uuid = uuid_for(key)
  return unless uuid

  instrument :delete, key: key do
    @client.files.batch_delete(uuids: [ uuid ])
    delete_uuid_mapping(key)
  end
rescue Uploadcare::Exception::NotFoundError
  delete_uuid_mapping(key)
  nil
end

#delete_prefixed(prefix) ⇒ Object



82
83
84
85
86
# File 'lib/active_storage/service/uploadcare_service.rb', line 82

def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    keys_for_prefix(prefix).each { |key| delete(key) }
  end
end

#download(key, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_storage/service/uploadcare_service.rb', line 46

def download(key, &block)
  uuid = uuid_for!(key)
  download_url = file_download_url(uuid)
  if block_given?
    stream_download(key, download_url, &block)
  else
    instrument(:download, key: key) { request(download_url, &:body) }
  end
rescue Uploadcare::Exception::NotFoundError
  raise ActiveStorage::FileNotFoundError
end

#download_chunk(key, range) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/active_storage/service/uploadcare_service.rb', line 58

def download_chunk(key, range)
  uuid = uuid_for!(key)
  download_url = file_download_url(uuid)

  instrument :download_chunk, key: key, range: range do
    request(download_url, range: range, &:body)
  end
rescue Uploadcare::Exception::NotFoundError
  raise ActiveStorage::FileNotFoundError
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/active_storage/service/uploadcare_service.rb', line 88

def exist?(key)
  instrument :exist, key: key do |payload|
    payload[:exist] = file_exists?(key)
  rescue Uploadcare::Exception::NotFoundError
    payload[:exist] = false
  end
end

#headers_for_direct_uploadObject



101
102
103
# File 'lib/active_storage/service/uploadcare_service.rb', line 101

def headers_for_direct_upload(*)
  {}
end

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



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_storage/service/uploadcare_service.rb', line 32

def upload(key, io, checksum: nil, custom_metadata: {}, **)
  uploaded_file = nil

  instrument :upload, key: key, checksum: checksum do
    uploaded_file = @client.uploads.upload(io, store: true, metadata: )
    ensure_integrity(io, checksum) if checksum
    persist_uuid_mapping(key, uploaded_file.uuid)
  end
rescue ::ActiveStorage::IntegrityError
  @client.files.batch_delete(uuids: [ uploaded_file.uuid ]) if uploaded_file&.uuid.present?
  delete_uuid_mapping(key)
  raise
end

#url_for_direct_uploadObject

Raises:

  • (NotImplementedError)


96
97
98
99
# File 'lib/active_storage/service/uploadcare_service.rb', line 96

def url_for_direct_upload(*)
  raise NotImplementedError,
        "Active Storage direct uploads are not supported by UploadcareService; use uploadcare_file_field/uploadcare_files_field instead"
end