Module: Lockbox::ActiveStorageExtensions::Attachment
- Defined in:
- lib/lockbox/active_storage_extensions.rb
Instance Method Summary collapse
- #download ⇒ Object
- #download_chunk ⇒ Object
- #open(**options) ⇒ Object
- #preview ⇒ Object
- #transform_variants_later ⇒ Object
- #variant ⇒ Object
Instance Method Details
#download ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lockbox/active_storage_extensions.rb', line 82 def download result = super(&nil) = Utils.(record, name) # only trust the metadata when migrating # as earlier versions of Lockbox won't have it # and it's not a good practice to trust modifiable data encrypted = && (![:migrating] || blob.["encrypted"]) if encrypted result = Utils.decrypt_result(record, name, , result) end if block_given? io = StringIO.new(result) chunk_size = 5.megabytes while (chunk = io.read(chunk_size)) yield chunk end else result end end |
#download_chunk ⇒ Object
105 106 107 108 109 |
# File 'lib/lockbox/active_storage_extensions.rb', line 105 def download_chunk(...) # TODO raise error in 3.0 warn "[lockbox] WARNING: download_chunk not supported for encrypted files" if Utils.(record, name) super end |
#open(**options) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lockbox/active_storage_extensions.rb', line 128 def open(**) blob.open(**) do |file| = Utils.(record, name) # only trust the metadata when migrating # as earlier versions of Lockbox won't have it # and it's not a good practice to trust modifiable data encrypted = && (![:migrating] || blob.["encrypted"]) if encrypted result = Utils.decrypt_result(record, name, , file.read) file.rewind # truncate may not be available on all platforms # according to the Ruby docs # may need to create a new temp file instead file.truncate(0) file.write(result) file.rewind end yield file end end |
#preview ⇒ Object
116 117 118 119 |
# File 'lib/lockbox/active_storage_extensions.rb', line 116 def preview(...) raise Lockbox::Error, "Preview not supported for encrypted files" if Utils.(record, name) super end |
#transform_variants_later ⇒ Object
122 123 124 125 |
# File 'lib/lockbox/active_storage_extensions.rb', line 122 def transform_variants_later blob.instance_variable_set(:@lockbox_encrypted, true) if Utils.(record, name) super end |