Module: ActiveStorageEncryption::Overrides::EncryptedBlobInstanceMethods

Defined in:
lib/active_storage_encryption/overrides.rb

Instance Method Summary collapse

Instance Method Details

#compose(keys, source_encryption_keys: []) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/active_storage_encryption/overrides.rb', line 140

def compose(keys, source_encryption_keys: [])
  if service_encrypted?
    ensure_encryption_key_set!

    self.composed = true
    service.compose(keys, key, encryption_key: encryption_key, source_encryption_keys: source_encryption_keys, **)
  else
    super(keys)
  end
end

#download(&block) ⇒ Object

Downloads the file associated with this blob. If no block is given, the entire file is read into memory and returned. That’ll use a lot of RAM for very large files. If a block is given, then the download is streamed and yielded in chunks.



120
121
122
123
124
125
126
127
128
# File 'lib/active_storage_encryption/overrides.rb', line 120

def download(&block)
  if service_encrypted?
    ensure_encryption_key_set!

    service.download(key, encryption_key: encryption_key, &block)
  else
    super
  end
end

#download_chunk(range) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/active_storage_encryption/overrides.rb', line 130

def download_chunk(range)
  if service_encrypted?
    ensure_encryption_key_set!

    service.download_chunk(key, range, encryption_key: encryption_key)
  else
    super
  end
end

#open(tmpdir: nil, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_storage_encryption/overrides.rb', line 84

def open(tmpdir: nil, &block)
  ensure_encryption_key_set! if service_encrypted?

  service.open(
    key,
    encryption_key: encryption_key,
    checksum: checksum,
    verify: !composed,
    name: ["ActiveStorage-#{id}-", filename.extension_with_delimiter],
    tmpdir: tmpdir,
    &block
  )
end

#serializable_hash(options = nil) ⇒ Object

The encryption_key can be in binary and not serializabe to UTF-8 by to_json, thus we always want to leave it out. This is also to better mimic how native ActiveStorage handles it.



169
170
171
172
173
174
175
176
# File 'lib/active_storage_encryption/overrides.rb', line 169

def serializable_hash(options = nil)
  options = if options
    options.merge(except: Array.wrap(options[:except]).concat([:encryption_key]).uniq)
  else
    {except: [:encryption_key]}
  end
  super
end

#service_encrypted?Boolean



70
71
72
# File 'lib/active_storage_encryption/overrides.rb', line 70

def service_encrypted?
  !!service&.try(:encrypted?)
end

#service_headers_for_direct_uploadObject



98
99
100
101
102
103
104
105
106
# File 'lib/active_storage_encryption/overrides.rb', line 98

def service_headers_for_direct_upload
  if service_encrypted?
    ensure_encryption_key_set!

    service.headers_for_direct_upload(key, filename: filename, content_type: content_type, content_length: byte_size, checksum: checksum, custom_metadata: , encryption_key: encryption_key)
  else
    super
  end
end

#service_url_for_direct_upload(expires_in: ActiveStorage.service_urls_expire_in) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/active_storage_encryption/overrides.rb', line 74

def service_url_for_direct_upload(expires_in: ActiveStorage.service_urls_expire_in)
  if service_encrypted?
    ensure_encryption_key_set!

    service.url_for_direct_upload(key, expires_in: expires_in, content_type: content_type, content_length: byte_size, checksum: checksum, custom_metadata: , encryption_key: encryption_key)
  else
    super
  end
end

#upload_without_unfurling(io) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/active_storage_encryption/overrides.rb', line 108

def upload_without_unfurling(io)
  if service_encrypted?
    ensure_encryption_key_set!

    service.upload(key, io, checksum: checksum, encryption_key: encryption_key, **)
  else
    super
  end
end

#url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline, filename: nil, **options) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/active_storage_encryption/overrides.rb', line 151

def url(expires_in: ActiveStorage.service_urls_expire_in, disposition: :inline, filename: nil, **options)
  if service_encrypted?
    ensure_encryption_key_set!

    service.url(
      key, expires_in: expires_in, filename: ActiveStorage::Filename.wrap(filename || self.filename),
      encryption_key: encryption_key,
      content_type: content_type_for_serving, disposition: forced_disposition_for_serving || disposition,
      blob_byte_size: byte_size,
      **options
    )
  else
    super
  end
end