Class: Service::OpenStackService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container:, credentials:, connection_options: {}) ⇒ OpenStackService

Returns a new instance of OpenStackService.



7
8
9
10
11
12
# File 'lib/active_storage/service/open_stack_service.rb', line 7

def initialize(container:, credentials:, connection_options: {})
  settings = credentials.reverse_merge(connection_options: connection_options)

  @client = Fog::OpenStack::Storage.new(settings)
  @container = Fog::OpenStack.escape(container)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/active_storage/service/open_stack_service.rb', line 5

def client
  @client
end

#containerObject (readonly)

Returns the value of attribute container.



5
6
7
# File 'lib/active_storage/service/open_stack_service.rb', line 5

def container
  @container
end

Instance Method Details

#delete(key) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/active_storage/service/open_stack_service.rb', line 59

def delete(key)
  instrument :delete, key: key do
    client.delete_object(container, key)
  rescue Fog::OpenStack::Storage::NotFound
    false
  end
end

#delete_prefixed(prefix) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/active_storage/service/open_stack_service.rb', line 67

def delete_prefixed(prefix)
  instrument :delete, prefix: prefix do
    directory = client.directories.get(container)
    filtered_files = client.files(directory: directory, prefix: prefix)
    filtered_files = filtered_files.map(&:key)

    client.delete_multiple_objects(container, filtered_files)
  end
end

#download(key, &block) ⇒ Object



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

def download(key, &block)
  if block_given?
    instrument :streaming_download, key: key do
      object_for(key, &block)
    end
  else
    instrument :download, key: key do
      object_for(key).body
    end
  end
rescue Fog::OpenStack::Storage::NotFound
  raise ActiveStorage::FileNotFoundError if defined?(ActiveStorage::FileNotFoundError)
end

#download_chunk(key, range) ⇒ Object



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

def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    chunk_buffer = []

    object_for(key) do |chunk|
      chunk_buffer << chunk
    end

    chunk_buffer.join[range]
  rescue Fog::OpenStack::Storage::NotFound
    raise ActiveStorage::FileNotFoundError if defined?(ActiveStorage::FileNotFoundError)
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/active_storage/service/open_stack_service.rb', line 77

def exist?(key)
  instrument :exist, key: key do |payload|
    answer = head_for(key)
    payload[:exist] = answer.present?
  rescue Fog::OpenStack::Storage::NotFound
    payload[:exist] = false
  end
end

#headers_for_direct_upload(_key, content_type:, checksum:) ⇒ Object



121
122
123
124
125
126
# File 'lib/active_storage/service/open_stack_service.rb', line 121

def headers_for_direct_upload(_key, content_type:, checksum:, **)
  {
    'Content-Type' => content_type,
    'ETag' => convert_base64digest_to_hexdigest(checksum)
  }
end

#update_metadata(key, content_type:, disposition: nil, filename: nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_storage/service/open_stack_service.rb', line 128

def (key, content_type:, disposition: nil, filename: nil, **)
  instrument :update_metadata, key: key, content_type: content_type, disposition: disposition do
    params = { 'Content-Type' => content_type }
    if disposition && filename
      params['Content-Disposition'] =
        content_disposition_with(type: disposition, filename: ActiveStorage::Filename.wrap(filename))
    end
    client.post_object(container,
                       key,
                       params)
    true
  rescue Fog::OpenStack::Storage::NotFound
    raise ActiveStorage::FileNotFoundError if defined?(ActiveStorage::FileNotFoundError)
  end
end

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_storage/service/open_stack_service.rb', line 14

def upload(key, io, checksum: nil, disposition: nil, content_type: nil, filename: nil, **)
  instrument :upload, key: key, checksum: checksum do
    params = { 'Content-Type' => content_type || guess_content_type(io) }
    params['ETag'] = convert_base64digest_to_hexdigest(checksum) if checksum
    if disposition && filename
      params['Content-Disposition'] =
        content_disposition_with(type: disposition, filename: filename)
    end

    begin
      client.put_object(container, key, io, params)
    rescue Excon::Error::UnprocessableEntity
      raise ActiveStorage::IntegrityError
    end
  end
end

#url(key, expires_in:, disposition:, filename:) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_storage/service/open_stack_service.rb', line 86

def url(key, expires_in:, disposition:, filename:, **)
  instrument :url, key: key do |payload|
    expire_at = unix_timestamp_expires_at(expires_in)
    generated_url =
      client.get_object_https_url(
        container,
        key,
        expire_at,
        filename: filename
      )
    generated_url += '&inline' if disposition.to_s != 'attachment'
    # unfortunately OpenStack Swift cannot overwrite the content type of an object via a temp url
    # so we just ignore the content_type argument here
    payload[:url] = generated_url

    generated_url
  end
end

#url_for_direct_upload(key, expires_in:) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/active_storage/service/open_stack_service.rb', line 105

def url_for_direct_upload(key, expires_in:, **)
  instrument :url, key: key do |payload|
    expire_at = unix_timestamp_expires_at(expires_in)
    generated_url = client.create_temp_url(container,
                                           key,
                                           expire_at,
                                           'PUT',
                                           port: 443,
                                           scheme: 'https')

    payload[:url] = generated_url

    generated_url
  end
end