Module: Shrine::Plugins::Lambda::AttacherMethods

Defined in:
lib/shrine/plugins/shrine-lambda.rb

Instance Method Summary collapse

Instance Method Details

#lambda_process(data) ⇒ 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`.

Raises:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/shrine/plugins/shrine-lambda.rb', line 140

def lambda_process(data)
  cached_file = uploaded_file(data['attachment'])
  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] = data.except('attachment', 'action', 'phase')
  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

  swap(cached_file) || _set(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 ‘data_attribute` of the Shrine attacher’s record.

Chooses the ‘save_method` either for the ActiveRecord or for Sequel, and saves the record.

Parameters:

  • result (Hash)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/shrine/plugins/shrine-lambda.rb', line 167

def lambda_save(result)
  versions = result['versions']
  attr_content = if versions
                   tmp_hash = versions.inject(:merge!)
                   tmp_hash.dig('original', 'metadata')&.delete('key')
                   tmp_hash.to_json
                 else
                   result['metadata']&.delete('key')
                   result.to_json
                 end

  record.__send__(:"#{data_attribute}=", attr_content)
  save_method = if record.is_a?(ActiveRecord::Base)
                  :save
                elsif record.is_a?(::Sequel::Model)
                  :save_changes
                end
  record.__send__(save_method, validate: false)
end