Module: Shrine::Plugins::AwsLambda::AttacherMethods
- Defined in:
- lib/shrine/plugins/aws_lambda.rb
Instance Method Summary collapse
-
#lambda_process ⇒ Object
Triggers AWS Lambda processing defined by the user in the uploader’s ‘Shrine#lambda_process`, first checking if the specified Lambda function is available (raising an error if not).
-
#lambda_save(result) ⇒ Object
Receives the ‘result` hash after Lambda request was authorized.
Instance Method Details
#lambda_process ⇒ Object
Triggers AWS Lambda processing defined by the user in the uploader’s ‘Shrine#lambda_process`, first checking if the specified Lambda function is available (raising an error if not).
Generates a random key, stores the key into the cached file metadata, and passes the key to the Lambda function for signing the request.
Stores the DB record class and name, attacher data atribute and uploader class names, into the context attribute of the Lambda function invokation payload. Also stores the cached file hash object and the generated path into the payload.
After the AWS Lambda function invocation, a ‘Shrine::Error` will be raised if the response is containing errors. No more response analysis is performed, because Lambda is invoked asynchronously (note the `invocation_type`: ’Event’ in the ‘invoke` call). The results will be sent by Lambda by HTTP requests to the specified `callbackUrl`.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/shrine/plugins/aws_lambda.rb', line 160 def lambda_process cached_file = uploaded_file(file) assembly = lambda_default_values assembly.merge!(store.lambda_process_versions(cached_file, context)) function = assembly.delete(:function) raise Error, 'No Lambda function specified!' unless function raise Error, "Function #{function} not available on Lambda!" unless function_available?(function) prepare_assembly(assembly, cached_file, context) assembly[:context] = { 'record' => [record.class.name, record.id], 'name' => name, 'shrine_class' => self.class.name } response = lambda_client.invoke(function_name: function, invocation_type: 'Event', payload: assembly.to_json) raise Error, "#{response.function_error}: #{response.payload.read}" if response.function_error set(cached_file) atomic_persist(cached_file) end |
#lambda_save(result) ⇒ Object
Receives the ‘result` hash after Lambda request was authorized. The result could contain an array of processed file versions data hashes, or a single file data hash, if there were no versions and the original attached file was just moved to the target storage bucket.
Deletes the signing key, if it is present in the original file’s metadata, converts the result to a JSON string, and writes this string into the ‘attribute` of the Shrine attacher’s record.
Chooses the ‘save_method` either for the ActiveRecord or for Sequel, and saves the record.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/shrine/plugins/aws_lambda.rb', line 190 def lambda_save(result) versions = result['versions'] attr_content = if versions tmp_hash = versions.reduce(:merge!) tmp_hash.dig('original', 'metadata')&.delete('key') tmp_hash.to_json else result['metadata']&.delete('key') result.to_json end record.__send__(:"#{attribute}=", attr_content) save_method = case record when ActiveRecord::Base :save when ::Sequel::Model :save_changes end record.__send__(save_method, validate: false) end |