Class: Service::SolidStorageService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public: false, cache_control: nil, **options) ⇒ SolidStorageService

Returns a new instance of SolidStorageService.



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

def initialize(public: false, cache_control: nil, **options)
  @public = public
  @cache_control = cache_control
end

Instance Attribute Details

#cache_controlObject (readonly)

Returns the value of attribute cache_control.



10
11
12
# File 'lib/active_storage/service/solid_storage_service.rb', line 10

def cache_control
  @cache_control
end

Instance Method Details

#compose(source_keys, destination_key) ⇒ Object



54
55
56
57
58
59
# File 'lib/active_storage/service/solid_storage_service.rb', line 54

def compose(source_keys, destination_key, **)
  SolidStorage::File.create(
    key: destination_key,
    data: SolidStorage::File.where(key: source_keys).in_order_of(:key, source_keys).map(&:data).join
  )
end

#delete(key) ⇒ Object



69
70
71
# File 'lib/active_storage/service/solid_storage_service.rb', line 69

def delete(key)
  instrument(:delete, key:) { SolidStorage::File.where(key:).destroy_all }
end

#delete_prefixed(prefix) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/active_storage/service/solid_storage_service.rb', line 61

def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: do
    SolidStorage::File.where(
      SolidStorage::File.arel_table[:key].matches("#{prefix}%").to_sql
    ).destroy_all
  end
end

#download(key, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_storage/service/solid_storage_service.rb', line 24

def download(key, &block)
  io = SolidStorage::File.find_by!(key:).io
  if block
    instrument(:streaming_download, key:) do
      while data = io.read(5.megabytes)
        yield data
      end
    end
  else
    instrument(:download, key:) { io.read }
  end
rescue ActiveRecord::RecordNotFound
  raise ActiveStorage::FileNotFoundError
end

#download_chunk(key, range) ⇒ Object



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

def download_chunk(key, range)
  instrument(:download_chunk, key:, range:) do
    args =
      if sqlite? then "data, #{range.begin + 1}, #{range.size}"
      else
       "data FROM #{range.begin + 1} FOR #{range.size}"
      end

    SolidStorage::File.select("SUBSTRING(#{args}) as chunk").
      find_by!(key:).chunk
  rescue ActiveRecord::RecordNotFound
    raise ActiveStorage::FileNotFoundError
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/active_storage/service/solid_storage_service.rb', line 73

def exist?(key)
  instrument(:exist, key:) do |payload|
    payload[:exist] = SolidStorage::File.exists?(key:)
  end
end

#find(key) ⇒ Object



12
13
14
# File 'lib/active_storage/service/solid_storage_service.rb', line 12

def find(key)
  SolidStorage::File.find_by!(key:)
end

#headers_for_direct_upload(key, content_type:) ⇒ Object



92
93
94
# File 'lib/active_storage/service/solid_storage_service.rb', line 92

def headers_for_direct_upload(key, content_type:, **)
  { "Content-Type" => content_type }
end

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



16
17
18
19
20
21
22
# File 'lib/active_storage/service/solid_storage_service.rb', line 16

def upload(key, io, checksum: nil, **)
  instrument(:upload, key:, checksum:) do
    file = SolidStorage::File.create!(key:, data: io.read)
    ensure_integrity_of(key, checksum) if checksum
    file
  end
end

#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_storage/service/solid_storage_service.rb', line 79

def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {})
  instrument :url, key: do |payload|
    verified_token_with_expiration = ActiveStorage.verifier.generate(
      { key:, content_type:, content_length:, checksum:,
        service_name: name }, expires_in:, purpose: :blob_token
    )

    payload[:url] = url_helpers.update_rails_solid_storage_service_url(
      verified_token_with_expiration, url_options
    )
  end
end