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

#change_content_type(key, content_type) ⇒ Object

Non-standard method to change the content type of an existing object



128
129
130
131
132
133
134
135
# File 'lib/active_storage/service/open_stack_service.rb', line 128

def change_content_type(key, content_type)
  client.post_object(container,
                     key,
                     'Content-Type' => content_type)
  true
rescue Fog::OpenStack::Storage::NotFound
  raise ActiveStorage::FileNotFoundError
end

#delete(key) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/active_storage/service/open_stack_service.rb', line 54

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

#delete_prefixed(prefix) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/active_storage/service/open_stack_service.rb', line 64

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



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_storage/service/open_stack_service.rb', line 27

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
end

#download_chunk(key, range) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_storage/service/open_stack_service.rb', line 41

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
  end
end

#exist?(key) ⇒ Boolean

Returns:



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

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

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



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

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

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



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

def upload(key, io, checksum: nil, **)
  instrument :upload, key: key, checksum: checksum do
    params = { 'Content-Type' => guess_content_type(io) }
    params['ETag'] = convert_base64digest_to_hexdigest(checksum) if checksum

    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



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

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



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

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